Software Design Patterns - State Pattern, Singleton Pattern, Factory Method Pattern, Facade Pattern
Benefits of Design Patterns
Creational Patterns: a Catalog
Purpose: Deals with initialization of objects and selection among classes
Structural Patterns: a Catalog
Purpose: Describe ways to construct and compose objects to realize new functionality
Behavioral Patterns: a Catalog
Purpose: Distribute the responsibility among objects and define the protocol for object and class interactions.
Design Pattern: State and Strategy
- Allow an object to alter its behavior when its internal state changes. The object will appear to change its class.
- An object’s behavior depends on its state
- According to different classifications at run-time
- Ability to change to another state with ease
- Provides different behaviors/actions for different members
- Customers can be treated differently due to memberships
- Customers can be easily changed to another membership
- Implemented by concrete classes
- Different Handle() functions

- Maintains a State
- Aggregation link
- Different Request() functions
- state.handle()
- Implemented by different ConcreteState
- state object can be changed.
=>
=>
=>
Example 2 – Library Members:
- Senior, Adult, Child
- A Child will grow up
- A Adult will grow old
- Needs to change membership
Solution:
Summary:
- Consolidate
- Puts all behavior associated with a state into 1 object
- Consistent
- Allows state transition logic into a state rather than many if/then switch statements for different logics
- Allows state changes
- Avoids inconsistent states changes to occur and complexity involved
- The number of objects increased.
- Might need to use with Singleton pattern to save the number of objects being created
Design Pattern: Strategy
(Similar to State Pattern, but it has only 1 function in each state.)
- Define a family of algorithms, encapsulate each one, and make them interchangeable.
- Provides more independence for clients to use it.
- An object’s behavior depends on its choice
- Need different variants of an algorithm
- Difference to State Pattern
- Only 1 function in State
Example:

- Strategy Pattern handles a single and specific task (sort ())
- State Pattern handles many tasks (discount(), deposit()..etc)
- Strategies are passed to the context object (e.g..to SortArray as parameters)
- States are created by the context object
- Both solutions are very similar.
Design Pattern: Singleton
- Need to have ONLY one(1) instance for a class.
- Singleton instantiates itself, and only one(1) of such.
- Ensure that only 1 object instance is ever created.
- Provide a global point of access.
- Save the number of duplicated objects being created. Such as in State Pattern.
- Variable instance (refers to itself, initialized before constructor is called).
- Constructor > private(-) Constructor (non-accessible)
- Method > getInstance() (returns the single instance)
class Singleton { public: static Singleton* getInstance(); protected: Singleton(); Singleton(const Singleton&); Singleton& operator = (const Singleton&); private: static Singleton* instance; }; Singleton *p2 = Singleton::getInstance();
- Singleton.getInstance().method_x();
- Ensure that only one(1) instance is created.
- Constructor is private, not accessible.
- Only way of instantiating the class is through the method getInstance();
- Provides a global point of access.
Example - President:
- To enforce that: There is Only 1 President at any time.
- There are many (n) Ministers need to contact the president.
- President p1 = President.getInstance (); //refer to the same object
- President p2 = President.getInstance (); //refer to the same object
- We have 1 president and many Minsters calling this 1 president object.
- Use a counter to keep track how many the “same” one object being called.
Singleton vs Non_Singleton:
Summary:
- Used to ensure that a class has only one instance. For example, one printer spooler object, one file system, one window manager, etc.
- One may use a global variable to access an object but it does not prevent one from creating more than one instance, Singleton prevents creating more than 1 object instance.
- Instead the class itself is made responsible for keeping track of its instance. It can thus ensure that no more than one instance is created. This is the singleton pattern.
Design Pattern: Factory Method
- Also known as “Virtual Constructor”
- Defines an interface for creating an object
- Leaves the “choice” of its type to the subclasses
- Creation of object to be determined at run-time.
- Defines an interface for creating object
- Let subclasses to decide which class to create.
- Provides the newly created object through a common interface
- Product (interface) ConcreteProduct implements the Product interface
- Factory creates/generate Product object via factoryMethod()
- ConcreteFactory implements and produces ConcreteProduct object.
- Document class that needs to be created is specific to the application at run-time by user. So it doesn’t know which class to use in advance.
- Factory Method design pattern solves this problem
Participants and Communication


Summary:
- To generate required object based upon request
- Subject to user’s inputs etc.
- One of the most commonly used Design Patterns in SE
- Provides an mechanism to produce different objects.
Design Pattern: Façade
- Exterior (outside part) of a building
- Usually the front
- French, literately meaning “frontage” or “face”
- A simplified interface (front-end) to other code.
- Such as a class library.
- Reduce dependencies of outside code base
- Provides a more simplified interface to access complex backend classes.
- Hiding the backend
Structure