Explain the builder pattern and prototype pattern in Java design pattern programming

Builder pattern definition is also called generator pattern. It can abstract the construction process of complex objects (abstract categories), so that different implementation methods of this abstract process can construct objects with different representations (properties).

When the algorithm for creating a complex object should be independent of the components of the object, and the construction process must allow the constructed object to have different representations. We can consider using the builder model.

realization

1. Builder specifies the abstract interface for each part of creating a product object. It usually contains abstract methods for creating and returning products, or concrete methods, and puts the creation process into the concretebuilder class. 2. Concretebuilder implements the interface of builder to construct and assemble various parts of the product. 3. The director is responsible for calling the appropriate builder to build the product. The director class generally does not depend on the product class, and the builder class directly interacts with the director class. 4. Product represents the constructed complex object. Concreatebuilder creates an internal representation of the product and defines its assembly process.

The customer creates a director object and configures it with the builder object it wants. The director obtains the customer's request, creates the product, and finally obtains the product.

Advantages 1 The process of constructing objects can be finely controlled to produce different product objects. 2. It is easy to expand. When there are new products, it can be realized by adding a new concretebuilder.

The related pattern abstract factory pattern is similar to the generator because it can also create complex objects. The main difference is that the generator pattern focuses on building a complex object step by step. The abstract factory pattern focuses on multiple series of product objects (simple or complex). The generator returns the product in the last step, while for the abstract factory, the product is returned immediately.

Prototype pattern definition prototype pattern is a kind of creation pattern, which is characterized by "copying" an existing instance to return a new instance instead of creating a new instance. The copied instance is what we call the "prototype", which is customizable. Prototype mode is mostly used to create complex or time-consuming instances, because in this case, copying an existing instance makes the program run more efficiently; Or create similar data with equal values but different names.

realization

1. Client - create a new object and get another object through clone. 2. Prototype - defines an abstract method of clone itself. 3. Concreteprototype - implements the clone method.

Example 1 Many elements in the game are repeated. We can copy the same elements using prototype mode. 2. When making data charts, we need to read data from the database and save it to the object for the first time. When we need to make other charts with the same data, using the prototype mode can avoid re reading the database.

Related problems and implementation 1 If the number of prototypes to be created is not fixed, you can create a prototype manager. Before copying prototype objects, the client first checks whether there are prototype objects that meet the conditions in the prototype manager. If there are, it can be used directly. If not, clone one. This is called the prototype mode of registration. 2. There are two kinds of replication: deep replication and shallow replication. In shallow copy, the copy object and the prototype object share all internal variables of the object, and the two objects have the same memory space and life cycle. Modifications to the prototype object also modify its copy, and vice versa.

As long as the clonable interface is implemented in Java, you can call the clone method of object class to realize shallow replication:

Output:

It can be seen that shallow copy copies the reference of the object. When the value of the object is changed, the copied object will also change, and the basic type of Java is the copied value.

Let's implement deep replication:

Output:

In the above replication method, we re create an object and re create a reference to achieve deep replication.

Advantages 1 Replication performs better than new. 2. Simplify or hide the details of the created object and copy it directly.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>