Design Patterns GoF
Design Patterns GoF
Design patterns are a powerful tool for software developers. However, they should not
be seen as prescriptive specifications for software
This section gives a high-level description of the twenty-three design patterns described
by the Gang of Four (1.Erich Gamma, 2. Richard Helm, 3.Ralph Johnson and 4. John
Vlissides)
All the 23 patterns divided were into 3 groups namely 1.Creational Pattern, 2.Structural
and 3. Behavioral patterns
Factory
Name: Factory
Problem: Who should be responsible for creating objects when there are
special considerations, such as complex creation logic, a desire to
separate the creation responsibilities for better cohesion, and so
forth?
Solution (advice): Create a Pure Fabrication object called a Factory that handles the creation.
ADAPTER
Adapter Pattern translates one interface for a class into a compatible interface.
An adapter allows classes to work together that normally could not because of
incompatible interfaces
Adapter pattern works as a bridge between two incompatible interfaces. This type of
design pattern comes under structural pattern as this pattern combines the capability of
two independent interfaces.
This pattern involves a single class which is responsible to join functionalities of
independent or incompatible interfaces. A real life example could be a case of card
reader which acts as an adapter between memory card and a laptop. You plugins the
memory card into card reader and card reader into the laptop so that memory card can
be read via laptop.
Template
Name: Adapter
Problem: How to resolve incompatible interfaces, or provide a stable interface to similar
components with different interfaces?
Solution (advice): Convert the original interface of a component into another interface, through
an intermediate adapter object.
BRIDGE
The bridge pattern is a Gang of Four design pattern. This is a structural pattern as it defines a
manner for creating relationships between classes or entities.
The bridge pattern is a design pattern used in software engineering which is meant
to "decouple an abstraction from its implementation so that the two can vary independently.
The bridge uses encapsulation, aggregation, and can use inheritance to separate responsibilities
into different classes.
This pattern involves an interface which acts as a bridge which makes the functionality
of concrete classesindependent from interface implementer classes. Both types of
classes can be altered structurally without affecting each other.
The bridge pattern is used to separate the abstract elements of a class from the
implementation details.
For example, the abstract elements may be the business logic of an application. They
can be created without any knowledge of the implementation details of their data
access or interoperability with the operating system. The pattern provides the means to
replace the implementation details without modifying the abstraction. This permits, for
example, changing operating systems, databases, etc. with no impact to the business
logic.
When the bridge pattern is not used, you may find that implementation details are
included within the same classes as abstract elements. The way in which
implementation details are changed is then generally be through inheritance, with
subclasses providing different implementations.
Another benefit of the bridge pattern is that it introduces the possibility of changing the
implementation details at run-time. This could permit the user to switch
implementations to determine how the software interoperates with other systems. For
example, allowing the user to decide whether to store information in a database, XML
file or using another storage mechanism.
The UML class diagram above describes an implementation of the bridge design pattern. The
items in the diagram are described below:
Abstraction: This class contains members that define an abstract business object and its
functionality. It also acts as the base class for other, more refined, abstractions.
RefinedAbstraction: Many refined abstractions may inherit from the Abstraction class.
Each provides a more specific variation upon the abstraction but still contains no
implementation details other that those in the Implementation object reference that they
hold.
ImplementationBase: This abstract class is the base class for all classes that provide
implementation details for the associated abstractions. The class provides a fixed interface
that can be utilised by the abstractions. The interface need not be similar to that of the
abstraction. This class may be implemented as an interface if it provides no real
functionality for its subclasses.
Example
An interface DrawAPI interface which is acting as a bridge implementer and concret
classes RedCircle, GreenCircle implementing the DrawAPI interface. Shape is an
abstract class and will use object of DrawAPI. BridgePatternDemo, our demo class
will use Shapeclass to draw different colored circle.
Template:
Name: Bridge
Observer Pattern
The observer pattern is a software design pattern in which an object, called the
subject, maintains a list of its dependents, called observers, and notifies them
automatically of any state changes, usually by calling one of their methods
Define a one-to-many dependency between objects so that when one object changes state,
all its dependents are notified and updated automatically.
Example:
The Observer defines a one-to-many relationship so that when one object changes state,
the others are notified and updated automatically. Some auctions demonstrate this pattern.
Each bidder possesses a numbered paddle that is used to indicate a bid. The auctioneer
starts the bidding, and "observes" when a paddle is raised to accept the bid. The acceptance
of the bid changes the bid price which is broadcast to all of the bidders in the form of a new
bid.
Template:
The strategy pattern (also known as the policy pattern) is a software design pattern that
enables an algorithm's behaviour to be selected at runtime.
Template:
Name: Strategy
Problem: How to design for varying, but related, algorithms or policies? How
to design for the ability to change these algorithms or policies?
Solution (advice): Define each algorithm/policy/strategy in a separate class, with a
common interface.