This is the 24th day of my participation in the More text Challenge. For more details, see more text Challenge


👉 Design mode directory

preface

The prototype pattern is one of the creation patterns, and I also introduced the singleton pattern, the builder pattern, and the factory pattern earlier. This is the last of the creation patterns. After the prototype pattern is over, I quickly move on to the structural pattern.

What is a Prototype?
concept

Stereotype instances are used to specify the types of objects to create, and new objects are created by copying these stereotypes.

— Baidu Encyclopedia

In simple terms, you take an object and copy it to a new object that looks exactly like it.

The most vivid metaphor is that the Monkey King after plucking gently blow to generate a lot of dopples.

advantages
  1. High performance. If you use Java’s built-in Clone method, you can skip the constructor and create it directly by copying the binary stream in memory for better performance.
  2. Simplify the process of creating objects. To create an identical object, just call the Clone method, and you don’t need to worry about the data inside the object or the constructor of the class.
  3. Save preconditions for creating objects. Some objects may need to query the database for some parameters before they are created. If you want to make a backup, you only need to clone them.
disadvantages

There are advantages as well as disadvantages.

  1. The Cloneable interface must be implemented.
  2. It violates the principle of openness and closure. To invoke the Clone method, you need to modify the existing class, first implementing the Cloneable interface and then modifying the Clone method. If it’s a completely new class, it won’t.
  3. Implementation complexity. When one of the object’s member properties is a reference object or has a circular reference, it can be very complicated to implement. The Clone method provided by Java is not able to copy the reference object.

The principle of

“+” means compliance, and “-” means noncompliance or irrelevant

The principle of Open the closed Single responsibility Di milt Replacement on the Richter scale Dependency inversion Interface segregation Synthesis of reuse
+ +
Applicable scenario
  1. Object initialization takes a lot of resources.
  2. Creating an object using new requires a lot of data preparation, and the current object can be used repeatedly.
  3. Situations that require a large number of backups.

The prototype pattern can be used in conjunction with the factory pattern. Java already provides the underlying method (Clone) for the prototype pattern, which is very convenient to use.

How to implement

I’m just going to show you a little bit about how to implement the prototype pattern, Clone Java Object and I want to talk a little bit more about that in another article, but I’ll also talk about shallow and deep copies, so I’ll talk about that as well.

To implement the prototype pattern, you need three things:

  1. Abstract prototype class or prototype interface. The prototype implementation class must be inherited or implemented, using the Clone method of Object, but must implement the Cloneable interface
  2. Concrete prototype implementation class. You must implement an abstract prototype class or prototype interface, and then implement or override the Clone method
  3. Access to the class. Access the class to call the clone method of the prototype implementation class.

Above class structure diagram.

In the code

Concrete prototype implementation class: PrototypeEntity

/**
 * Created on 2021/5/28.
 *
 * @author xuxiaobai
 */
public class PrototypeEntity implements Cloneable {
    private int a;
    private int b;
    
    public PrototypeEntity(int a,int b){
        this.a=a;
        this.b=b;
    }

    @Override
    public Object clone(a) throws CloneNotSupportedException {
        // The clone object is a clone object, and the clone object is a clone object
        return super.clone();
    }

    /** * Get and set methods omit */
}
Copy the code

Access class: PrototypeTest

/**
 * Created on 2021/5/28.
 *
 * @author xuxiaobai
 */
public class PrototypeTest {
    public static void main(String[] args) throws CloneNotSupportedException {
        PrototypeEntity p1=newPrototypeEntity(); Object clone = p1.clone(); }}Copy the code

conclusion

The prototype pattern is also a relatively simple design pattern, in the actual development, I have not yet used, you can refer to the use of scenarios, you do not have to use the design pattern, other patterns are the same.

The archetypal pattern violates the open and closed principle and the Richter substitution principle. Adding a Clone method to an existing class does not comply with the open and closed principle. If a subclass modifies a method already written by its parent class, it does not comply with the Richter substitution principle.

The archetypal pattern follows the single duty principle, Demeter’s law. This method is only responsible for cloning the object. Using the prototype pattern, you can enclose the internal member properties and provide only one Clone method.

All other principles are extensible, and there is no violation.

Now that we’re done creating patterns, we’ll move on to structural patterns.

Don’t give me a “like” when you see this?

— — — — — — — — — — — — — — —

The more you know, the more you don’t know.

If you have questions about this article, please comment directly or send me a personal message. If you think my writing is good, you can also support me by clicking “like”

Without permission, shall not be reproduced!