Creation Design Patterns :
Abstract Factory :
-
Purpose: Provides an interface to create a series of related or interdependent objects without specifying a concrete class.
-
Scenes:
- A system is created independently of its product.
- A system is configured by one of several product families.
- Emphasizes the design of a series of related product objects for joint use.
- Provide a library of product classes, but only expose their interfaces rather than implementations
-
Structure Diagram: Create a series of related objects
-
Known applications: Collection, Map in JDK
-
Factory Method :
-
Purpose: Defines an interface for creating objects and lets subclasses decide which class to instantiate.
-
Scenes:
- A class does not know the class of the objects it creates.
- A class expects its subclasses to specify the objects it creates.
- It is mostly used in the framework code, oriented to interface programming.
-
Structure Diagram: Create an Object
-
Known Applications:
- Blocking queue of ThreadPoolExecutor in JDK
- AbstractExecutorService.submit method, returns different Futures in ScheduledThreadPoolExecutor
-
Builder :
-
Purpose: To separate the construction of a complex object from its representation, so that the same construction process can create different representations.
-
Scenes:
- When an algorithm for creating a complex object should be independent of what that object is made of and how they are assembled.
- When the construction process must allow different representations of the constructed object.
-
Structure diagram: defines a series of steps to create a class of products
-
known application
- StringBuilder in JDK
- BeanDefinitionBuilder in Spring
singleton pattern
-
Purpose: Relates to a single class that is responsible for creating its own objects while ensuring that only a single object is created. This class provides a way to access its only object, directly, without instantiating an object of the class.
-
Scenes:
- A production unique serial number is required.
- The counter in the WEB does not need to be added to the database every time it is refreshed, but is cached first with a singleton.
- An object created needs to consume too many resources, such as I/O connection to the database.
-
Structure diagram:
prototype mode
-
Purpose: Implements a prototype interface that is used to create a clone of the current object. This mode is used when the cost of directly creating the object is relatively large
-
Scenes:
- Resource optimization scenarios.
- Class initialization needs to digest a lot of resources, including data, hardware resources, etc.
- Scenarios with performance and security requirements.
- To generate an object through new requires very tedious data preparation or access rights, you can use the prototype pattern.
- Scenarios with multiple modifiers for one object.
-
Note: Instead of constructing a new object by instantiating a class, the prototype pattern creates a new object by copying an existing object. Shallow copy implements Cloneable, rewrite, deep copy is to read binary stream by implementing Serializable.
-
Structure diagram: clone() method