Design Pattern Introduction
Design Pattern
10 Articles
In this article, let's learn about Design Pattern
in .NET.
Table of Contents
- Introduction
- Gang of Four
- Why use Design Pattern?
- Design Pattern Types
- Object Oriented Principles
- Summary
Introduction
Design patterns
are a critical part of software design. They are reusable solutions to common problems encountered in software design
within a given context. In other words, they provide a template that you can use to solve typical issues you'll encounter during software development. The beauty of design
patterns is that they help create flexible and easily maintainable
applications. They are considered best practice approaches to common
software development problems. However, it's important to remember that you should see these patterns as a starting point, not as a set of rules written in stone. Implementing
a pattern can typically be done in multiple ways, depending on the language you're using and the problem you're trying to solve.
Gang of Four
One of the best-known design pattern categories is the Gang of Four (GoF)
design patterns. This group comprises Erich Gamma
,
Richard Helm
, Ralph Johnson
, and John Vlissides
. In 1994,
they published a book called "Design Patterns: Elements of Reusable Object-Oriented Software"
, which has become the de facto standard
work on software design patterns. The book consists of 23 patterns
that are still commonly used today, even though they were written
almost three decades ago. These patterns are still commonly used today and are often built into the framework we use.
While the book was written in 1994 and uses C++ 2.0, the patterns are still relevant today. However, the implementations have been adjusted to modern-day possibilities using current, real-life examples you might encounter in your day-to-day life as a developer. The three types of patterns defined by the Gang of Four will also be covered in the article. Instead of memorizing the exact implementation of each pattern, it's more important to understand which problem they solve and how they relate to the type of pattern.
Why Use Design Pattern?
Some of the problems that design patterns solve include,
- how to ensure that only a single instance of a class exists?
- how to make two objects with a different interface work together?
- how to extend an object interface without changing the underlying object?
- how to enable support for undo functionality? and many more
Design Pattern Types
There are three types of design patterns.
Creational patterns
- These patterns help with creating objects in a flexible and independent way. They make it easier to add new objects to a system without tightly coupling them to the code. This means you won't need to use a lot of new statements in your code.
Structural patterns
- These patterns define how objects and classes are related and connected. They help with creating larger structures in a system.
Behavioral patterns
- They help with communication between objects and classes. These patterns can make it easier to understand how different parts of a system work together
Throughout the design pattern
article series, you will also
learn about two important principles of object-oriented programming.
Type | Patterns |
---|---|
Creational - Create Objects | Singleton, Factory Method, Abstract Factory, Builder, Prototype |
Structural - Combine Objects | Adapter, Bridge, Decorator, Composite, Facade, Proxy, Flyweight |
Behavioral - Control Flow | Template Method, Strategy, Command, Memento, Mediator, Chain of Responsibility, Observer, State, Iterator, Visitor, Interpreter |
Object Oriented Principles
The Gang of Four follow two important principles in their design patterns,
Program to an interface, not an implementation
- Programmed to an interface means clients remain unaware of the specific types of objects they use as long as the objects adhere to the interface that clients expect. This allows for loose coupling.
Favor object composition over class inheritance
- Object composition obtains new functionality by assembling objects, while class inheritance defines the implementation of one class in terms of another. The Gang of Four favored object composition over class inheritance as it leads to more reusable and simpler designs.
Patterns that adhere to these principles often follow the SOLID
coding principles of open/closed
and single responsibility
. It's important to note that these patterns can be implemented in various ways, using
abstract
classes or interfaces
.
Summary
In this article we learn't what is Design Pattern
. A design pattern is a helpful solution to a common problem in software design. It's
like a template that you can use to solve problems that you'll encounter while designing software. They help make software easy to maintain and flexible
.
The Gang of Four
patterns are a set of 23 patterns
that are commonly used today. They are grouped
into three categories: Creational
, Structural
, and Behavioral
patterns. Creational patterns help with creating objects
, Structural patterns help with combining objects
,
and Behavioral patterns help with control flow
. Two important principles to remember are to program to an interface
and favor object composition over class inheritance
. In the next article, we'll learn about the Singleton
pattern, which is a common and easy pattern to understand.