Design Patterns commonly used in OOP
A design pattern is a general reusable solution to a commonly occurring problem within a given context in software design. It is a framework based on which applications can be made. They are widely used in Object Oriented Programming/Design. The intent of this post is to give general awareness on the various design patterns that are commonly used.
Strategy Pattern
According to the GoF book, Strategy design pattern should
“Define a family of algorithms, encapsulate each one, and make them
interchangeable. Strategy lets the algorithm vary independently from clients
that use it.” You encapsulate code in external algorithms for easy use
rather than spreading it around inside your core code and modifying it
throughout that code.
Decorator Pattern
According to the GoF book, Decorator Pattern should “Attach
additional responsibilities to an object dynamically. Decorators provide a
flexible alternative to subclassing for extending functionality.”
Factory Pattern
According to the GoF book, the
Factory Method design pattern should “Define an interface for creating an
object, but let subclasses decide which class to instantiate. Factory method
lets a class defer instantiation to subclasses.” The idea is that you have an
object that creates other objects.
Observer Pattern
The Gang of Four book says that
the Observer design pattern should “Define a one-to-many dependency between
objects so that when one object changes state, all its dependents are notified
and updated automatically.”
Chain of Responsibility Pattern
The GoF book says that the Chain
of Responsibility design pattern should “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.”
Singleton design pattern
The Gang of Four (GoF) book
(Design Patterns: Elements of Reusable Object-Oriented Software, 1995, Pearson
Education, Inc. Publishing as Pearson Addison Wesley) says that the Singleton
design pattern must: “Ensure a class only has oneinstance, and provide a global
point of access to it.”
Flyweight Design pattern
The GoF book says the Flyweight
pattern must, “Use sharing to support large numbers of fine-grained objects
efficiently.” They go on to say, “A flyweight is a shared object that can be
used in multiple contexts simultaneously. The flyweight acts as an independent
object in each context — it’s indistinguishable from an instance of the object
that’s not shared.”
Adapter Design Pattern
The Gang of Four (GoF) book
(Design Patterns: Elements of Reusable Object- Oriented Software, 1995, Pearson
Education, Inc. Publishing as Pearson Addison Wesley) says the Adapter pattern
lets you “Convert the interface of a class into another interface the client
expects. Adapter lets classes work together that couldn’t otherwise because of
incompatible interfaces.”
Facade Design pattern
The GoF book says the Facade
pattern should “Provide a unified interface to a set of interfaces in a system.
Facade defines a higher-level interface that makes the subsystem easier to
use.”
Template Method Pattern
According to the official Gang of
Four (GoF) phrasing, the Template Method will “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.”
Builder Design Pattern
The GoF says that the Builder
design patterns let you “Separate the construction of a complex object from its
representation so that the same construction processes can create different
representations.”
Iterator Design Pattern
According to the Gang of Four
(GoF), you can use the Iterator design pattern to “Provide a way to access the
elements of an aggregate object sequentially without exposing its underlying
representation.” (Design Patterns: Elements of Reusable Object-Oriented
Software, 1995, Pearson Education, Inc. Publishing as Pearson Addison Wesley).
Composite Design Patterns
The GoF says you use the Composite
design pattern to “Compose objects into tree structures to represent part-whole
hierarchies. Composite lets clients treat individual objects and compositions
of objects uniformly.”
State Design Pattern
The Gang of Four (GoF) book
(Design Patterns: Elements of Reusable Object- Oriented Software, 1995, Pearson
Education, Inc. Publishing as Pearson Addison Wesley) says that the State
design pattern will “Allow an object to alter its behavior when its internal
state changes. The object will appear to change its class.”
Proxy Design Pattern
The GoF book says you can use the
Proxy design pattern to, “Provide a surrogate or placeholder for another object
to control access to it.”
Command Design Pattern
The Gang of Four (GoF) book
(Design Patterns: Elements of Reusable Object- Oriented Software, 1995, Pearson
Education, Inc. Publishing as Pearson Addison Wesley) says you use the Command
pattern to, “Encapsulate a request as an object, thereby letting you
parameterize clients with different requests, queue or log requests, and
support undoable operations.”
Mediator Design Pattern
The Gang of Four book says
you can use the Mediator pattern to, “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.”
More Patterns and more details of the patterns will be done shortly
The contents are borrowed from Design Patterns for Dummies written by Steve Holzner. In depth details of the patterns are given in the book. This is a just a snapshot for those who want only definitions.
The contents are borrowed from Design Patterns for Dummies written by Steve Holzner. In depth details of the patterns are given in the book. This is a just a snapshot for those who want only definitions.
Comments
Post a Comment