Introduction to Design Patterns
- Design Patterns
- Creational
- Structural
- Behavioral
The creation pattern abstracts the process of creating objects, which can be understood as encapsulating the process of creating objects, so that the client program only needs to use the objects, and does not care about the logic of the process of creating objects. the inevitable trend. Since the creation of objects consumes a lot of resources in the system, it is important to study the creation of objects separately so that they can be created efficiently.
In briefing:
Creational patterns are focused towards how to instantiate an object or group of related objects
- Simple Factory
- Factory Method
- Abstract Factory
- Builder
- Prototype
- Singleton
In brief:
For example, if you want to build a house and need a door, call a building material supplier to provide you with a door, you don't need to know anything about building doors yourself.
Motivation:
Repeatedly create complex objects.
Implementation:
return instances to the client through the interface, without exposing any instantiation logic to the client.
Class diagram:
Code example
In brief:
Like a company hiring supervisor, who can't interview all applicants himself, will delegate interviews to different people in charge depending on job openings such as technical, sales, management, etc.
Motivation:
Repeated creation of complex objects and uncertainty about the exact type of subclasses.
Implementation:
Also return instances to the client through the interface, without exposing any instantiation logic to the client, but instantiating the interface through subclasses.
Class diagram:
Code example
In brief:
Or renovate the door, according to the demand not only need wood doors also need iron doors, in addition to wood doors need special carpenter master, iron doors need welders.
Motivation:
repeatedly create complex objects, and need to rely on certain characteristics of the product, but do not want to be associated with a specific product class.
Implementation:
Factory of factories, encapsulating products with the same characteristics into a single factory.
Class diagram:
code example
In brief:
For example, if you want a custom Big Mac burger at a membership restaurant, what kind of filling 🐷 meat or 🐂 meat, what kind of sauce 🍅 or salad, etc.
Motivation:
Create a lot of complex objects at different stages.
Implementation:
Create complex objects in steps, using the same code to generate different types and forms of objects.
Class diagram:
code example
In brief:
Dolly Cloning sheep.
Motivation:
Generate new objects by cloning with existing objects. Optimize the tedious process of creating complex objects.
Implementation:
Provide cloning interface to create new objects from existing objects.
Class diagram:
code example
In brief:
A country has only one president.
Motivation:
only one instance of a class, providing a global access node.
Implementation:
privatization construct, providing return static local objects.
Class diagram:
code example
After solving the problem of object creation, the composition of objects and the dependencies between objects become the focus of developers, because how to design the structure of objects, inheritance and dependencies will affect the maintainability of subsequent programs, code robustness, coupling, etc.
In brief:
How to use between objects
- Adapter
- Bridge
- Composite
- Decorator
- Facade
- Flyweight
- Proxy
In brief:
The Hong Kong version of the switch power supply needs an adapter for use in mainland China.
Motivation:
Do not modify the original adapter class, the new object is compatible with the old interface.
Implementation:
Inherit the adapter class, extend the old interface.
Class diagram:
code example
In brief:
A bridge, with abstraction and implementation at both ends.
Motivation:
Separate the abstraction layer from the implementation layer, replace inheritance with combination, optimize m*n classes into m+n classes.
Implementation:
Implementation layer defines interfaces to associate abstract objects (bridge).
Class diagram:
code example
In brief:
traverse the abstract tree.
Motivation:
Let the client handle each object in a uniform way.
Implementation:
Abstract the common properties, and store them in a tree data structure.
Class diagram:
code example
In brief:
Russian nesting dolls🪆.
Motivation:
Add new behavior dynamically without affecting the original object.
Implementation:
Wrap the object in a decorator class.
Class diagram:
code example
The difference between decoration and adaptation:
The decorator pattern is transparent to the user of the decorated class, and the interface is available both before and after decoration. Adaptation is to adapt two mismatched interfaces and make them available.
In brief:
Computer one-button start button, ready to use.
Motivation:
Provide a simple interface for external interaction, hiding the internal complexity.
Implementation:
Provide a high-level interface that encapsulates subsystem behavior.
Class diagram:
code example
In brief:
Similar to thread pools, process pools.
Motivation:
Reuse existing objects of the same kind, or create new objects if no match is found.
Implementation:
With factory encapsulated key-value pair data structure.
Class diagram:
code example
In brief:
intermediary.
Motivation:
Proxy of one object to another object if the target object is not convenient for direct manipulation.
Implementation:
Forwarding to the target object through the proxy to achieve the purpose.
Class diagram:
code example
After the structure of the object and the creation of the object are solved, the behavior of the object remains, if the behavior of the object is well designed, then the behavior of the object will be clearer, and the efficiency of collaboration between them will increase.
In brief: How objects communicate with each other
- Chain of Responsibility
- Command
- Iterator
- Mediator
- Memento
- Observer
- State
- Strategy
- Template Method
- Visitor
In brief:
Similar to budget approval, from Team Leader -> Finance -> Director -> CFO.
Motivation:
Want data to be processed along the chain of responsibility like an approval workflow.
Implementation:
Build an acyclic chain of responsibility, decoupling senders and receivers.
Class diagram:
code example
In brief:
Remote control TV.
Motivation:
decouple sender and receiver completely, no direct reference between sender and receiver.
Implementation:
Encapsulate receiver's behavior by abstract command layer.
Class diagram:
code example
In brief:
similar to stl container in C++.
Motivation:
Provide a way to iterate over objects without exposing the underlying implementation.
Implementation:
Store data by data structure, abstract iteration method.
Class diagram:
code example
In brief:
Before the establishment of WTO, all countries traded with each other in a complicated way, after the establishment of WTO, all countries traded through WTO.
Motivation:
No coupling between classes and no direct interaction through the intermediate layer for data interaction.
Implementation:
Abstract intermediary class for two-way data interaction.
Class diagram:
code example
Difference between Mediator and proxy:
Proxy is A proxy B one-way. Mediator is many-to-many and data interaction is bidirectional.
In brief:
Game archive.
Motivation:
Want objects to support snapshot capabilities.
Implementation:
Implement snapshot generation interface, and manage snapshots through snapshot management class.
Class diagram:
code example
In brief:
Recruiting software, receive notifications when there are job updates.
Motivation:
Implement one-to-many asynchronous communication.
Implementation:
Subject maintains a list of observers, and automatically notifies observers when the subject is modified.
Class diagram:
code example
In brief:
Low-cost finite state machine, state machine State, Transition corresponds to directed graph Node, Link, state pattern is borrowed from the concept of state machine State, Transition.
Motivation:
Change the object behavior when the object state changes.
Implementation:
Based on the concept of Node and Link in directed graph, abstract State and Transition.
Class diagram:
code example
In brief:
Travel, airplane, train, car choose the appropriate mode based on mileage, price.
Motivation:
The same behavior performs different actions in different situations.
Implementation:
Abstract policy behavior, through the delegate class to complete the policy switch and behavior execution.
Class diagram:
code example
In brief:
Build the program architecture.
Motivation:
Fixed templates for process-fixed interfaces.
Implementation:
abstract template methods, implementation in subclasses.
Class diagram:
code example
In brief:
Follow a tour group on a cross-border trip, following a fixed route.
Motivation:
When the underlying operation is too complex, constrain the behavior through the guest pattern.
Implementation:
Abstract visitor interface, visitor interface, implement coordinator combination element.
Class diagram:
code example
design-patterns-for-humans java-design-patterns
MIT © 2023 calvinhxx