One design pattern a day - Prototype prototype pattern

1. Model Description

After reading a lot of data and writing complex prototype patterns, my personal understanding is that model replication creates new classes directly from existing classes instead of invoking their constructors.

Class Diagram of Prototype Model

3. Role in Prototype Model

  • Protype role: Responsible for defining methods for replicating existing instances to generate new ones
  • ConcretePrototype role: responsible for implementing the method of replicating existing instances to generate new ones
  • Client role: responsible for generating new instances using the method of replicating instances

4. Code examples

1.Product class:

package com.designpattern.cn.protptypepattern.patternframework;

public interface Product extends Cloneable {
    public abstract void use(String s);
    public abstract Product createClone();
}

The roduct class above is simple, but inherits the java.lang.Clonable interface, which does not require any method to be implemented. This is a markup interface in which classes marked can call the Clone method to clone class instances.It is important to note that the Clone method is not defined in the Clonable interface, but in java.lang.Object. It is also important to note that the Clone method implements shallow replication.

2.Manager class:

package com.designpattern.cn.protptypepattern.patternframework;

import java.util.HashMap;

public class Manager {
    private HashMap showcase = new HashMap();
    public void register(String name, Product product){
        showcase.put(name, product);
    }
    public Product create(String protoname){
        Product p = (Product) showcase.get(protoname);
        return p.createClone();
    }
}

The Manager class above provides a register method that saves string and roduct interface registration to showcase. It is not yet known what the specific class of Product is, but it can be determined that this specific class implements the Product interface, so it can call the use method and the createClone method to create a clone of the instance.

Next, create several different specific subclasses of Products, each of which implements the Product interface:

3-1.MessageBox class:

package com.designpattern.cn.protptypepattern.patterndemostrate;

import com.designpattern.cn.protptypepattern.patternframework.Product;

public class MessageBox implements Product {
    private char decochar;
    public MessageBox(char decochar){
        this.decochar = decochar;
    }

    public void use(String s){
        int length = s.getBytes().length;
        for(int i = 0; i < length + 4; i++){
            System.out.print(decochar);
        }
        System.out.println("");
        System.out.println(decochar+" "+s+" "+decochar);
        for(int i = 0; i < length + 4; i++){
            System.out.print(decochar);
        }
        System.out.println("");
    }

    public Product createClone(){
        Product p = null;
        try {
            p = (Product) clone();
        }catch (CloneNotSupportedException e){
            e.printStackTrace();
        }
        return p;
    }
}

3-2UnderlinePen class:

package com.designpattern.cn.protptypepattern.patterndemostrate;

import com.designpattern.cn.protptypepattern.patternframework.Product;

public class UnderlinePen implements Product {
    private char ulchar;
    public UnderlinePen(char ulchar){
        this.ulchar = ulchar;
    }

    public void use(String s){
        int length = s.getBytes().length;
        System.out.println("\"" + s + "\"");
        System.out.println(" ");
        for(int i = 0; i < length + 4; i++){
            System.out.print(ulchar);
        }
        System.out.println("");
    }

    public Product createClone(){
        Product p = null;
        try {
            p = (Product) clone();
        }catch (CloneNotSupportedException e){
            e.printStackTrace();
        }
        return p;
    }
}

4. Test classes and run results:

Prototype schemas are also used in JavaScript, which can be supplemented later to compare the implementation of prototype schemas in Java and Javascript.

---------------------------2019-06-27 00:13 is late. Today I work overtime until 10 o'clock to go home, the prototype mode is still more important. This article will continue to add-----------------------------------------

Keywords: PHP Java Javascript

Added by spags on Wed, 26 Jun 2019 20:28:55 +0300