Creational
Patterns
|
Example
Uses
|
Singleton
|
§ A class of which only a single instance can exist
§ Default constructor is
marked as private. A special function “GetInstance” in implanted to get
object reference.
§ Initialization can be done
either as early (Thread Safe) or Lazy
way
§ Double checked locking is
used for ensuring thread safety during instance access and creation.
§ Best
way to create singleton is to use Type Constructor with Lazy loading
§ If
multiple Class Loaders are involved then use Kernel level synchronization
object to achieve singleton effect.
§ Never
lock entire singleton object (Lock this) this could lead to deadlock, instead
use a dedicated sync object
§ Singleton
have several side effect and use only if state are need to be maintained or
else need to be passed as parameters.
§ Singleton
restrict scalability, for example today we just need one connection manager
but in future we may need multiple for load balancing purpose so design need
to be chosen carefully
|
Connection
Manager,
Abstract
factory
|
Factory
Pattern
|
Simple Factory
|
§ Hides instantiation details from client.
§ Client uses concrete
factory to get an instance of product (object) and factory return product as
abstract instance.
|
|
Factory Method
|
§ Purpose is same as simple factory but it uses Inheritance
§ An standard factory
interface is defined and (sub classed) Concentrate Factory Implement it.
§ Client request to concrete
factory for object and factory newly created object through a common (product)interface
|
|
Abstract Factory
|
§ Purpose is same as factory method pattern this factory is used for
creating family of related object such as pizza ingredients
§ Client hold an reference
of abstract factory that is implemented by more a concrete factory
§ Concrete factory is capable of creating multiple objects
(products)
§ Concrete factory creates
several related product and return on common interface (Abstraction)
§ Each product family has
its own abstraction root and there is no common interface in between product
families.
|
|
Builder
|
§ Separates object construction from its representation
§ Client has director a
object that holds and a reference of builder interface
§ Builder interface
implement by a concrete builder.
Director request concrete builder (through interface) to compose final
product and supply necessary instruction for building final product.
§ Builder return final
product through a common product interface.
§ Client can itself work as
director.
|
Product
assembly, Menu Building.
|
Prototype
|
§ A fully initialized instance to be copied or cloned
§ Client instantiates first
instance of class (called prototype)
§ Then it clone it (via
object.clone interface memebr) and modified to create a final concrete
product
§ Best suited when large
number of object need to share some common basing information such as schema,
best example is XML document elements.
|
XmlDocument,
UI Elements
|
Object Pool
|
§ Purpose if to reuse and share objects that are expensive to create
§ When a client asks for a
Reusable object.
§ Search for an available
Reusable object and if it was found it will be returned to the client.
§ If no Reusable object was
found then it tries to create a new one.
§ If this actions succeeds
the new Reusable object will be returned to the client.
§ If the pool was unable to
create a new Reusable, the pool will wait until a reusable object will be
released and then will returned to client
§ The Client is responsible
to request the Reusable object as well to release it to the pool.
§ The clients are not aware
that they are sharing the Reusable object.
§ This design pattern also present the
challenges of Thread Safety, Resource
leak (From Pool) infinite client waiting and Synchronization issues that must be taken care during implementation.
|
Thread
Pool
|
Structural
Patterns
|
Example
Uses
|
Adapter
|
§ Match interfaces of different classes without code changing of
participating classes.
§ Convert the interface of a
class into another interface clients expect.
§ Adapter lets classes work
together, that could not otherwise because of incompatible interfaces.
|
|
Bridge
|
§ Decouple abstraction from implementation so that the two can vary
independently.
§ It is usually used when
command pattern is in place
§ Client has a reference of
concrete class that implement and abstraction (such as Remote Control)
§ Abstract “Remote Control”
holds an instance of another specialized abstraction (such as TV Remote
Control) that will do the same function but for different audience.
§ Specialized abstraction is
realized by concrete classes (such as
Sony TV Remote)
§ In this case same remote
can used for any derived class at run time because client interface is
talking only to implementation (Generic TV Remote) and client can change the
implementation at both end of aggregation at run time.
|
Extension
of command pattern,
TV
Remote,
Alternative
to Adapter if code change is allowed.
|
Composite
|
§ Implements tree structure (whole Part
relationship)
§ Client has a reference to
component interface that is realized by either leaf or composite (Node)
implementation.
§ Each node (Composite) can
have one or more references of component interface that will further hold the
references of either leaf or node object
§ Technically it is three
data structure and in most of the implementation node and leaf share same
class schema and differ only in terms of sibling references that is null in
case of leaf object.
|
File
System Object , Xml Document
|
Decorator
|
§ Add responsibilities to objects dynamically
§ Client holds an instance
of abstract component (Interface) such as ICoffee
§ Abstraction (ICofee) is
realized by Concrete Cofee(Such as Dark Coffee) and toppings such as (Cream, milk and
sugar)
§ Each toppings holds a
reference of Component (ICoffee) that in turn can either holds Concrete
Component such as (Dark Coffee ) or toppings (Decorator Milk etc), but client
is still holding just one reference of base abstraction.
§ This pattern actually
implement a link list of object that talk in a cyclic manner in a predefined
order and client just know one reference first object that may be concrete
object or decorated object (List Head).
|
Coffee
machine Simulator,
|
Facade
|
§ A single class that represents an entire subsystem
§ It usually created by
aggregation of all the component of a large system for example Music Theater
Controller.
§ In this pattern client
just talk to top level object (facade)and rest of the complexity is handled by facade
§ For improving facade
performance is lazy init (initialization on demand) and object recycling if
required.
|
|
Flyweight
|
§ A fine-grained instance used for efficient sharing of object.
§ Applicable when a large
number of object shares significant about of state information (called
intrinsic or shared) with minimum extrinsic (unshared) state information.
§ Client holds a “Flyweight
Factory” and request of object to “Flyweight Factory”
§ “Flyweight Factory” holds
a reference of Flyweight interface that in turn holds Flyweight object
created by “Flyweight Factory”.
§ Flyweight Interface is
implemented by Concrete flyweight such as Tree.
§ “Flyweight Factory”
maintains extrinsic state data and provide to client when ever required and
client is responsible for passing “extrinsic state data” to shared object
(Flyweight).
|
War
game simulator that need large number of solder object.
|
Proxy
|
§ An object representing another object
§ Mostly used for Remote
method Invocation or Controlling object access to protected resource as well
as logging
§ Client holds a reference
of IProxy interface that actually holds Proxy Object.
§ Both Proxy and Target
Object implement same IProxy
§ Client invokes IProxy
members that in turns invoke Target Object members hiding any additional
complexity
|
|
Behavioral
Patterns
|
Example
Uses
|
Chain of Resp.
|
§ A way of passing a request between a chain of handlers
§ It decouples request
sender handles (receiver).
§ Handlers are usually
linked list and request object become parts of a chain and the request is
sent from one object to another object across the chain until one of the
objects will handle it.
|
|
Command
|
§ Encapsulate a request in an object and allows the parameterization of
clients with different requests optionally saves the requests in a queue.
§ Client creates a concrete
command and pushes it to invoker.
§ Invoker call execute
function of command that activate Receiver (Target Object on which command
need to work on)
§ Once done command return a
final outcome to client via invoker.
§ The main advantage of the
command design pattern is that it decouples the object that invokes the
operation from the one that know how to perform it.
§ The invoker should be
aware only about the abstract command class.
§ Client can also work as
invoker in simple scenario.
|
|
Interpreter
|
§ A way to include language elements in a program
§ The implementation of the
Interpreter pattern is just the use of the composite pattern applied to
represent a grammar.
§ The Interpreter defines
the behavior while the composite defines only the structure.
Note This pattern is not frequently used.
|
|
Iterator
|
Sequentially access the elements of
a collection
|
|
Mediator
|
Defines
simplified communication between classes
|
|
Memento
|
Capture
and restore an object's internal state
|
|
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
|
|
|
|