Design Pattern (GoF)
Hoa Nguyen
Factory
Definition
Define an interface for creating an object, but let subclasses decide which class to
instantiate. Factory Method lets a class defer instantiation to subclasses
Factory
Definition
Define an interface for creating an object, but let subclasses decide which class to
instantiate. Factory Method lets a class defer instantiation to subclasses
Factory
When and Where?
Class constructors allow client create instances of a class. However, some
situations, client does not, or should not know which one of several candidate
classes to instantiate. Factory Method (FM) allows client to use an interface
for creating an object while still retaining control over which class instantiate
Key objective of FM is extensibility.
Frequently used in applications that manage, maintain, or manipulate collections
of objects that are different but at the same time have many characteristics in
common
Frequently used in ‘manager’ type components such as Equipment Manager,
Controller Manager, Document Manager, …
Factory
Example
Factory
How to know a FM ???
The method create a new object
The method returns an abstract class or interface
The abstract class or interface is implemented by several classes.
Abstract Factory
Definition
Provide an interface for
creating families of related or
dependent objects without
specifying their concrete
classes
Abstract Factory
Definition
Abstract Factory
When and Where?
Provide a class that create objects that are related by a common theme.
Ex: GUI component factory which create UI controls for different window
system
Simpler notion which is the creation of individual object instances
Create objects using another class???
Creation object involves object caching, sharing or re-using of objects, and
applications that need to maintain object and type counts
The client does not know exactly what type to construct. It is easier to code
against a base type or an interface and then let a factory make this decision for
the client
Constructor do not communicate their intention very well because they must be
named after their class (or Sub New in Vb). Having numerous overloaded
constructors may make it hard for the client to decide which constructor to
use
Abstract Factory
Example
Builder
Definition
Separate the construction of a complex object from its representation so that the
same construction process can create different representations
Builder
Definition
Builder
When and Where?
Allow client constructs a complex object by specifying the type and
content only. Construction details are hidden from client entirely
Builders frequently encapsulate construction of Composite objects
because the procedures involved are often repetitive and complex
Builder is creational pattern just like the Factory pattern. However,
Builder gives you more control in that each step in construction process
can be customized. Factory patterns create objects in a single step
Builder
How to know a Builder???
When you know exactly steps by steps to create an object and you don’t want user
do these steps in constructor.
Example: Build TM control (CX5, CX6, CX7, CX8,…) that know exactly location of
image, type.
Prototype
Definition
Specify the kind of objects to create using a prototypical instance, and create new
object by copying this prototype
Prototype
Definition
Prototype
When and Where???
Clone a pre-existing sample object
Should use built-in ICloneable interface
There are 2 types: Shallow and Deep Copy
Singleton
Definition
Ensure a class has only one instance and provide a global point of access to it