Java Common Design Patterns - Prototype Patterns (1)

introduce

        Prototype Pattern (Prototype Pattern): Use prototype instances to specify the types of objects to be created, and create new objects by copying these prototypes.

        The Prototype pattern is used to create duplicate objects while maintaining performance. This mode is used when the cost of directly creating the object is relatively high. (For example, an object needs to be created after an expensive database operation, combined with a cache mechanism that returns its clone on the next request, and updates the database when needed, thus reducing database calls).

The core of the         prototype pattern is the prototype class . The prototype class needs to meet the following two conditions :

        · Implement the Cloneable interface.

        The Cloneable interface in Java has only one function: to inform the virtual machine at runtime that it is safe to use the clone() method on a class that implements this interface. In the JVM, only classes that implement this interface can be copied, otherwise a CloneNotSupportedException will be thrown at runtime.

        · Override the clone() method in the Object class.

        The clone() method in the Object class is used to return a copy of the object, but the scope of the protected type cannot be called by general classes. Therefore, the Prototype class needs to change the scope of the clone() method to public.

deep copy vs shallow copy

"Java Common Design Patterns --- Prototype Patterns (2) Deep Copy and Shallow Copy"

Application scenarios

        Using the prototype mode to create an object is much better in performance than directly newing an object, because the clone method of the Object class is a local method, which directly manipulates the binary stream in memory, especially when copying large objects, the performance difference is very obvious.
        Another benefit of using the prototype pattern is to simplify the creation of objects, making object creation as easy as copy-paste when editing a document.
        Because of the above advantages, you can consider using the prototype pattern when you need to create similar objects repeatedly. For example, an object needs to be created in a loop. If the object creation process is complex or the number of loops is large, using the prototype mode can not only simplify the creation process, but also improve the overall performance of the system.

Advantages and disadvantages

advantage:

1. The product creation process is encapsulated, and the client does not need to know the specific creation process of the product.

2. Improve performance, escape the constraints of constructors, especially when those objects are very complex.

3. New products can be added without modifying other codes, in line with the "open-closed principle".

shortcoming:

1. Equipping the clone method requires a comprehensive consideration of the function of the class, which is not necessarily easy for new classes, especially when a class references indirect objects that do not support serialization, or when the reference contains a cyclic structure.

2. Must implement a specific interface.

3. Each class must have a clone method. If the composition of the class is not too complicated, it is better. If the composition of the class is very complicated, it is more difficult to implement deep copying.

Code

Step 1: Create a prototype class

package design.pattern.prototype;
/**
 * Prototype pattern implementation class
 *
 * <br>类名:ConcretePrototype<br>
 * Author: mht<br>
 * Date: March 31, 2018 - 10:14:00am<br>
 */
public class ConcretePrototype extends Prototype{

    public void show() {
        System.out.println("Prototype mode implementation class...");
    }
}

Step 2: Implement prototypal class inheritance

package design.pattern.prototype;
/**
 * prototype class
 * <br>类名:Prototype<br>
 * Author: mht<br>
 * Date: March 31, 2018 - 10:12:48am<br>
 */
public abstract class Prototype implements Cloneable{

    @Override
    protected Object clone() {
        try {
            return super.clone();
        } catch (Exception e) {
            e.printStackTrace ();
        }
        return null;
    }
}

test

package design.pattern.prototype;

public class Client {

    public static void main(String[] args) {
        ConcretePrototype cp = new ConcretePrototype();
        for (int i = 0; i < 10; i++) {
            ConcretePrototype c = (ConcretePrototype) cp.clone();
            c.show();
        }
    }
}

Output result:

Prototype pattern implementation class...
Prototype pattern implementation class...
Prototype pattern implementation class...
Prototype pattern implementation class...
Prototype pattern implementation class...
Prototype pattern implementation class...
Prototype pattern implementation class...
Prototype pattern implementation class...
Prototype pattern implementation class...
Prototype pattern implementation class...

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325532047&siteId=291194637