Introduction of Design Pattern
TOC
Design Pattern for Software
- In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design. Wiki
The goal of Design Pattern is to make software easy to maintain. In this concept, Design Pattern is tightly related to Refactoring. If Design Pattern is where the target is, Refactoring is how to go. So, "Design Pattern" reflects the output structure of Refactoring.
5 Principles of OOP (SOLID)
- "S"ingle Responsibility Principle(SRP)
- One module has only own Responsibility
- "O"pen Close Responsibility Principle(OCP)
- A module is closed to modify, but opened to extend
- "L"iskov Substitution Principle(LSP)
- A child class can be replaced by a parent class
- Common members of children are come from a parent class
- "I"nterface Segregation Principle(ISP)
- A specified interface is better than a common interface
- "D"ependency Inversion Principle(DIP)
- A client is not dependent on a concrete class, but an interface or an abstract class.
Overview
C++ Idioms
- CRTP: Curiously Recurring Template Pattern
- Thin Template: Fast and low memory consuming container
- RAII: Resource Acquisition Is Initialization
- Function Object: Overloaded () operation to use as a function
- Smart pointer: Class acting as a pointer
- NVI: Non Virtual Interface
- RTTI: Run Time Type Information
Design Pattern
Create
- Singleton: Have only one instance, and there is only one way to access the instance
- Abstract Factory: Have an interface for a factory to create instances which are in the same group.
- Factory Method: Determine which group is to create
- Prototype: Create a new instance which is the same as the previous instance.
- Builder: Different instances with the same procedure
Structure
- Adapter: Adopt middle layer to avoid interface conflict
- Proxy: Wraps sub system with middle layer class
- Bridge: Divide implementation and abstracts
- Facade: Replace complicated system with simple classes
- Composite: Make complex object with recursive compound
- Decorator: Add dynamic functionalities with recursive compound
- Flyweight: Share an instance among sibling instances which has the same arguments.
Execute
- Iterator: Enumerate elements in container and complex objects
- Visitor: Execute function to elements in container and complex objects
- Observer: Broadcast an event
- Mediator: Handle one request in all instances
- Chain of Responsibility: Send an event in order
- Template Method: Changes algorithms or policies
- Strategy: Capsulate algorithm
- State: Change behavior with state
- Command: Capsulate command
- Memento: Store and restore the state of the instance
- Interpreter:
Classification
- Event and Callback: Observer, Mediator
- Common & uncommon
- Virtual function: Template method, Factory method
- Interface: Strategy, State, Builder
- Middle layers: Adapter, Proxy, Bridge, Facade, Mediator
- Containers or Complex objects: Iterator, Visitor
- State and command: Command, Memento
- Event propagation: Observer, Chain of responsibility
COMMENTS