Current location - Training Enrollment Network - Education and training - Summary of Common Design Patterns of College Students' Gospel Java
Summary of Common Design Patterns of College Students' Gospel Java
Take a look at the common Java design patterns.

1, create factory method mode:

(1) factory method mode:

Create a factory class and define an interface to create a product class that implements the same interface.

(2) the factory method mode:

The factory method model is an improvement of the factory method model, but the defect of a simple factory is that it does not conform to the "open closed principle"

(3) Static factory method mode:

Static factory mode is to set the method in factory method mode to static, and it can be called directly without creating an instance.

2. Creative abstract factory:

Abstract factories are mainly used to create series of related objects. When a product family needs to be designed to work together, the abstract factory can ensure that the client always only uses the objects in the same product family. By isolating the generation of a specific class, the client does not need to explicitly specify a specific generation class. All concrete factories have realized the common interface defined in the abstract factory, so as long as the instance of the concrete factory is changed, the behavior of the whole software system can be changed to some extent.

However, the disadvantage of this model is that it is more troublesome to add new behaviors. If you need to add a new product family object, you need to change the interface and all its subclasses, which will inevitably bring great trouble.

3. Create the generator mode:

The builder model decomposes the creation steps of complex products into different methods, which makes the creation process clearer and thus controls the generation process of complex objects more accurately. By isolating the construction and use of complex objects, that is, separating the creation of products from the product itself, different objects can be created in the same construction process; And each concrete builder is independent of each other, so it is convenient to replace the concrete builder or add a new concrete builder, and users can get different product objects by using different concrete builders.

4. Creative singleton pattern

Singleton mode can guarantee that there is only one instance of a class in the system, and the class instantiates itself and provides the public access point of this instance to the whole system. This instance cannot be accessed in any other way except this public access point. The advantage of singleton mode is that there is only one instance object in the system, and there is no need to create and destroy objects frequently, which saves system resources. Improving the performance of the system can strictly control the way and time for customers to access singleton objects. There are several ways to write singleton mode, mainly three: lazy single case, hungry single case and registration single case.

5, create-prototype mode:

In Java, the core of the prototype pattern is the prototype class prototype, and the prototype class needs to meet the following two conditions: to implement a clonable interface:

Override the clone () method in the Object class to return a copy of the object; By default, the clone () method in the Object class is shallow copy. If you want to copy objects deeply, you need to customize your own copy logic in the clone () method. Shallow copy: after copying an object, variables with basic data types will be recreated, and the reference type points to the memory address pointed by the original object. Deep copy: After copying an object, both the basic data type and the reference type are re-created.

Using prototype mode to create an object not only simplifies the steps of creating an object, but also has much better performance than the new method, because the clone () method of the object class is a local method, and the performance difference is very obvious when the binary stream is directly operated in memory, especially when copying large objects.

6. Structure adapter mode

Adapter mode is mainly used to convert a class or interface into the format that the client wants, so that the originally incompatible classes can work together and decouple the target class and adapter class; At the same time, it also conforms to open closed principle, and new adapter classes can be added without modifying the original code. Encapsulating the specific implementation in the adapter class is transparent to the client class, which improves the reusability of the adapter, but the disadvantage is that the implementation process of replacing the adapter is more complicated.

Therefore, the adapter mode is more suitable for the following scenarios:

(1) The system needs to use existing classes, and the interfaces of these classes do not meet the requirements of the system.

(2) Using a third-party component, the definition of the component interface is different from your own, and you don't want to modify your own interface, but you need to use the functions of the third-party component interface.

7. Structural decorator pattern:

The Decorator mode can dynamically add some additional responsibilities to the object to realize the function expansion, and choose different decorators to realize different behaviors at runtime; It is more flexible than using inheritance. By arranging and combining different decorative classes, many different behaviors are created and more powerful objects are obtained. In the spirit of "open closed principle", the modified category and the modified category change independently. Users can add new decoration categories and decoration categories as needed, and then combine them when using them. No need to change the original code.

However, the decorator model also has disadvantages. First of all, it will produce many small objects, which will increase the complexity of the system. Secondly, debugging is difficult. For objects that have been decorated many times, it may be cumbersome to find errors step by step during debugging.

8, structure-agent mode:

The design motivation of proxy mode is to access real objects through proxy objects. By establishing the object proxy class, the proxy object controls the reference to the original object, thus realizing the operation on the real object. In the proxy mode, the proxy object mainly acts as an intermediary to coordinate the connection between the caller (that is, the client) and the callee (that is, the target object), which reduces the coupling degree of the system to a certain extent and protects the target object. However, the disadvantage is that a proxy object is added between the caller and the callee, which may slow down the processing speed of the request.

9, structural bridge mode:

The bridge mode separates and decouples the abstract part and the implementation part of the system, so that they can change independently. In order to make the abstract part and the implementation part change independently, the bridge mode replaces the inheritance relationship with the combination relationship, and the abstract part has the interface object of the implementation part, so that the function of the concrete implementation part can be called through this interface object. In other words, the bridge in the bridge mode is a one-way relationship, and only some objects can be abstracted to use the bonus part, but not the other way around.

The bridging mode conforms to "open closed principle" and improves the scalability of the system. It does not need to modify the original system to arbitrarily expand one of the two change dimensions. Moreover, the implementation details are opaque to customers, so the implementation details can be hidden. However, because the aggregation relationship is based on the abstraction layer, developers need to program for the abstraction, which increases the difficulty of understanding and designing the system.

10, structure-appearance mode:

View mode provides a unified interface for clients to access a set of interfaces in the subsystem. Using appearance mode has the following advantages:

(1) is easier to use: it makes the subsystem easier to use, and the client no longer needs to know the internal implementation of the subsystem, nor does it need to interact with many internal modules of the subsystem, but only needs to interact with the appearance class;

(2) Loose coupling: Decoupling the client from the subsystem, making the modules in the subsystem easier to expand and maintain.

(3) Better classification of access levels: Through rational use of Facade, access levels can be better classified, some methods are used outside the system and some methods are used inside the system. The functions that need to be made public are concentrated in the facade, which is convenient for the client to use and hides the internal details well.

1 1, structure-combination mode:

The combination mode combines leaf objects and container objects recursively to form a tree structure to represent a "part-whole" hierarchical structure, so that users can use single objects and combined objects consistently and treat combined objects indiscriminately like leaf objects, thus enabling users to decouple their programs from the internal structure of complex elements.

The key point of composite pattern is that leaf objects and composite objects realize the same abstract construction class, which can represent both leaf objects and container objects. The client only needs to write this abstract construction class, which is why the composite pattern can handle leaf nodes and object nodes consistently.

12, structure-enjoyment mode:

Meta-sharing mode effectively supports fine-grained object reuse through * * * sharing technology, and the state changes little. When there are multiple identical objects in the system, only one object needs to be shared, which greatly reduces the number of objects in the system and saves resources.

The core of the enjoyment mode is the enjoyment factory class, which maintains an object storage pool. When the client needs an object, it first gets it from the enjoyment pool, and returns it directly if there is an object instance in the enjoyment pool. If there is no object instance in the enjoyment pool, it will create a new enjoyment object instance and return it to the user, and save the newly added object in the enjoyment pool, which means something like singleton.

Factory classes usually use collection types to store objects, such as HashMap, Hashtable, Vector, etc. In Java, database connection pool and thread pool are both applications using meta-schema.