Professional Documents
Culture Documents
Design Ptterns
Design Ptterns
Design Ptterns
You can’t just find a pattern and copy it into your program, the way you can with off-the-
shelf functions or libraries. The pattern is not a specific piece of code, but a general
concept for solving a particular problem. You can follow the pattern details and
implement a solution that suits the realities of your own program.
Patterns are often confused with algorithms, because both concepts describe typical
solutions to some known problems. While an algorithm always defines a clear set of
actions that can achieve some goal, a pattern is a more high-level description of a
solution. The code of the same pattern applied to two different programs may be
different.
An analogy to an algorithm is a cooking recipe: both have clear steps to achieve a goal.
On the other hand, a pattern is more like a blueprint: you can see what the result and its
features are, but the exact order of implementation is up to you.
Some pattern catalogs list other useful details, such as applicability of the pattern,
implementation steps and relations with other patterns.
Design patterns define a common language that you and your teammates can
use to communicate more efficiently. You can say, “Oh, just use a Singleton for
that,” and everyone will understand the idea behind your suggestion. No need to
explain what a singleton is if you know the pattern and its name.
Criticism of patterns
Kludges for a weak programming language
Usually, the need for patterns arises when people choose a programming language or a
technology that lacks the necessary level of abstraction. In this case, patterns become a
kludge that gives the language much-needed super-abilities.
Inefficient solutions
Patterns try to systematize approaches that are already widely used. This unification is
viewed by many as a dogma and they implement patterns “to the point”, without
adapting them to the context of their project.
Unjustified use
If all you have is a hammer, everything looks like a nail.
This is the problem that haunts many novices who have just familiarized themselves
with patterns. Having learned about patterns, they try to apply them everywhere, even in
situations where simpler code would do just fine.
Classification of patterns
Design patterns differ by their complexity, level of detail and scale of applicability to the
entire system being designed. I like the analogy to road construction: you can make an
intersection safer by either installing some traffic lights or building an entire multi-level
interchange with underground passages for pedestrians.
The most basic and low-level patterns are often called idioms. They usually apply only
to a single programming language.
The most universal and high-level patterns are architectural patterns. Developers can
implement these patterns in virtually any language. Unlike other patterns, they can be
used to design the architecture of an entire application.
In addition, all patterns can be categorized by their intent, or purpose. This book covers
three main groups of patterns:
Often, people only understand how to apply certain software design techniques to
certain problems. These techniques are difficult to apply to a broader range of
problems. Design patterns provide general solutions, documented in a format that
doesn't require specifics tied to a particular problem.
Abstract Factory
Creates an instance of several families of classes
Builder
Separates object construction from its representation
Factory Method
Creates an instance of several derived classes
Object Pool
Avoid expensive acquisition and release of resources by recycling objects that
are no longer in use
Prototype
A fully initialized instance to be copied or cloned
Singleton
A class of which only a single instance can exist
Structural design 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
Match interfaces of different classes
Bridge
Separates an object’s interface from its implementation
Composite
A tree structure of simple and composite objects
Decorator
Add responsibilities to objects dynamically
Facade
A single class that represents an entire subsystem
Flyweight
A fine-grained instance used for efficient sharing
Private Class Data
Restricts accessor/mutator access
Proxy
An object representing another object
Chain of responsibility
A way of passing a request between a chain of objects
Command
Encapsulate a command request as an object
Interpreter
A way to include language elements in a program
Iterator
Sequentially access the elements of a collection
Mediator
Defines simplified communication between classes
Memento
Capture and restore an object's internal state
Null Object
Designed to act as a default value of an object
Observer
A way of notifying change to a number of classes
State
Alter an object's behavior when its state changes
Strategy
Encapsulates an algorithm inside a class
Template method
Defer the exact steps of an algorithm to a subclass
Visitor
Defines a new operation to a class without change
Criticism
The concept of design patterns has been criticized by some in the field of computer
science.
Code Samples
Interface/Abstract Class
Interface which contains the abstract method to use by subclasses.
Concrete Class
Concrete subclasses which implement the interface with full implementation.
Factory / Creator
Create a Factory to generate an object of concrete class based on given information.
Factory pattern encapsulates object creation logic, which makes it easy to change it
later when you change how the object gets created, or you can even introduce a new
object with just change in one class.
Client
The application which requests the object to fulfill the decoration requirement