What is a Design Pattern?
In software engineering, a software design pattern is a general, reusable solution to a commonly occurring problem within a given context in software design. It is not a finished design that can be transformed directly into source or machine code. Instead it is a description or template for how to solve a problem that can be used in many different situations.
Patterns originated as an architectural concept by Christopher Alexander as early as 1966. Design patterns gained popularity in computer science after the book Design Patterns: Elements of Reusable Object-Oriented Software was published in 1994 by the so-called “Gang of Four”
Design patterns make it easier to reuse successful designs and architectures. Expressing proven techniques as design patterns makes them more accessible to developers of new systems. Design patterns help you choose design alternatives that make a system reusable and avoid alternatives that compromise reusability. Design patterns can even improve the documentation and maintenance of existing systems by furnishing an explicit specification of class and object interactions and their underlying intent. Put simply, design patterns help a designer get a design “right” faster.
Elements of design Patterns
1. Patterns name : A word or two to describe a design problem, solution, consequences.
2. Problem : Explains the problem and its context, it might describe specific design problems, class or object structures.
3. Solution : Describes elements that make up the design, their relationships, responsibilities, and collaborations.
4. consequences : Are the results and trade-offs of applying the pattern.
The 3 categories of Design Patterns
Design patterns vary in their granularity and level of abstraction. Because there are many design patterns, we need a way to organize them. This section classifies design patterns so that we can refer to families of related patterns. The classification helps you learn the patterns in the catalog faster, and it can direct efforts to find new patterns as well.
Creational Design Patterns
Concern the process of object creation
Class Scope
- Factory Method
Object Scope
- Abstract Factory
- Builder
- Prototype
- Singleton
Structural Design patterns
Deal with the composition of classes or objects
Class Scope
- Adapter
Object Scope
- Adapter
- Bridge
- Composite
- Decorator
- Facade
- Proxy
Behavioral Design patterns
Characterize objects interactions and responsibility
Class Scope
- Interpreter
- Template Method
Object Scope
- Interpreter
- Template Method
- Chain of Responsibility
- Command
- Iterator
- Mediator
- Memento
- Flyweight
- Observer
- State
- Strategy
- Visitor
The scope specifies whether the pattern applies primarily to classes or to objects. Class patterns deal with relationships between classes and their subclasses. These relationships are established through inheritance, so they are static—fixed at compile-time. Object patterns deal with object relationships, which can be changed at run-time and are more dynamic.
Almost all patterns use inheritance to some extent.
Creational class patterns defer some part of object creation to subclasses, while Creational object patterns defer it to another object.
The Structural class patterns use inheritance to compose classes, while the Structural object patterns describe ways to assemble objects.
The Behavioral class patterns use inheritance to describe algorithms and flow of control, whereas the Behavioral object patterns describe how a group of objects cooperate to perform a task that no single object can carry out alone.
Creational patterns
In software engineering, creational design patterns are design patterns that deal with object creation mechanisms, trying to create objects in a manner suitable to the situation. The basic form of object creation could result in design problems or added complexity to the design. Creational design patterns solve this problem by somehow controlling this object creation.
Abstract Factory
Provide an interface for creating families of related or dependent objects without specifying their concrete classes.
Builder
Separate the construction of a complex object from its representation so that the same construction process can create different representations.Factory Method
Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.Prototype
Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.Singleton
Ensure a class only has one instance, and provide a global point of access to it.Object Pool
Avoid expensive acquisition and release of resources by recycling objects that are no longer in useStructural Patterns
These design patterns are all about Class and Object composition. Structural class-creation patterns use inheritance to compose interfaces. Structural object-patterns define ways to compose objects to obtain new functionality.Adapter
Provide an interface for creating families of related or dependent objects without specifying their concrete classes.Bridge
Decouple an abstraction from its implementation so that the two can vary independently.Composite
Compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly.Decorator
Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.Facade
Provide a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use.Flyweight
Use sharing to support large numbers of fine-grained objects efficiently.Proxy
Provide a surrogate or placeholder for another object to control access to it.Structural Patterns
These design patterns are all about Class’s objects communication. Behavioral patterns are those patterns that are most specifically concerned with communication between objects.Chain of Responsibility
Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. Chain the receiving objects and pass the request along the chain until an object handles it.Command
Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.Interpreter
Given a language, define a represention for its grammar along with an interpreter that uses the representation to interpret sentences in the language.Iterator
Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.Mediator
Define an object that encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently.Memento
Without violating encapsulation, capture and externalize an object’s internal state so that the object can be restored to this state later.Observer
Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.State
Allow an object to alter its behavior when its internal state changes. The object will appear to change its class.Strategy
Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.Template Method
Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm’s structure.Visitor
Represent an operation to be performed on the elements of an object structure. Visitor lets you define a new operation without changing the classes of the elements on which it operates.Design Patterns Solve Design Problems
Design patterns solve many of the day-to-day problems object-oriented designers face, and in many different ways. Here are several of these problems and how design patterns solve them.
Finding Appropriate Objects
Object-oriented programs are made up of objects. An object packages both data and the procedures that operate on that data. The procedures are typically called methods or operations. An object performs an operation when it receives a request (or message) from a client.
The hard part about object-oriented design is decomposing a system into objects. The task is difficult because many factors come into play: encapsulation, granularity, dependency, flexibility, performance, evolution, reusability, and on and on. They all influence the decomposition, often in conflicting ways.
Specifying Object Interfaces
An object’s interface characterizes the complete set of requests that can be sent to the object. Any request that matches a signature in the object’s interface may be sent to the object.
Design patterns help you define interfaces by identifying their key elements and the kinds of data that get sent across an interface. A design pattern might also tell you what not to put in the interface.
Specifying Object Implementations
Objects can vary tremendously in size and number. They can represent everything down to the hardware or all the way up to entire applications. How do we decide what should be an object?
Design patterns address this issue as well. The Facade pattern describes how to represent complete subsystems as objects, and the Flyweight pattern describes how to support huge numbers of objects at the finest granularities. Other design patterns describe specific ways of decomposing an object into smaller objects.