Basic mode:
1. Interface pattern: When multiple classes provide similar services, through the interface pattern, the common services provided by different service provider classes can be abstracted and declared as an independent interface. This allows client objects to use different service provider classes in a seamless manner without any changes.
2. Abstract class pattern: can be used to design a framework that provides a consistent implementation of the common functions of a set of related classes. Unlike interfaces, methods in interfaces must be implemented in subclasses. The necessary methods can be implemented in abstract classes, and subclasses only need to inherit, thus reducing redundant method implementations.
3. Private method pattern: Some methods can only be used internally by other methods or internal classes in the same object. This pattern recommends designing such methods as private methods.
4. Accessor pattern: In an object, all instance variables are declared private, and public accessor methods are provided to access the public state of the object; customers can change an object from one state to another by using accessors. is another state; the object has direct access to its private variables. The javaBean in JSP uses this pattern.
5. Constant data manager: Very suitable for designing an effective storage mechanism to manage constant data used by different objects in the application. This pattern suggests that data whose values are permanent in an application should be stored in a separate object for access by other objects in the application, rather than being allowed to appear in different objects. This type of separation provides an easy-to-maintain centralized repository for the constant data in your application.
6. Immutable objects: Set all instance variables in the constructor, and do not provide any other methods that can change the state of the object; declare the class as a fanal type to prevent the possibility of changing the state by overriding class methods. ; All instance variables are written as fanal type, so that it can only be assigned through the constructor method; if any of the instance variables contains a reference to an object, then the corresponding get method inggai returns a copy of the object instead of Really the object itself. The above mechanism can ensure thread safety and automatically eliminate all problems related to concurrent access.
7. Monitor: It acquires a lock on such an object to ensure that only one thread is allowed to execute any method of the object at any one time. This can be done in Java by using the synchronized keyword in the declaration of the object method.
Create mode:
Role: Handles one of the most commonly performed tasks in OO applications - creating objects; supports the use of consistent, simple constrained mechanisms to create objects; allows encapsulation of details about which classes and how to create instances; supports the use of interfaces to reduce The generation of coupling.
1. Factory method: If the client object does not know which class to instantiate, it can use the factory method to create an instance of the corresponding class in the class hierarchy or related class group. Factory methods may be designed as part of the client itself, or they may be designed in individual classes. Among them, the class containing the factory method or any of its subclasses determines which class is selected and how to instantiate it.
2. Singleton: Provides a constrained object creation mechanism to ensure that there is only one instance of a specified class.
3. Abstract Factory: Allows the creation of instances of classes within a set of related classes without having the client object specify the actual concrete class to be instantiated.
4. Prototype: Provides a simple way to create objects, that is, copying objects from existing objects.
5. Generator: Allows the creation of complex objects, can provide only the type and content information of the object, and makes the details about object creation transparent to the client. This approach allows the same construction process to generate different object representations.