One day a prototype model design pattern --Prototype

First, the pattern description

   Read more information on prototype models write more complex, personal understanding is to copy the model to create a new class directly from an existing class, rather than the class constructor call.

Second, the prototype pattern class diagram

Third, the prototype model role

  • Prototype (Protype) Role: Responsible for the definition of a method to generate a new instance of duplicating an existing instance of
  • Specific prototype (ConcretePrototype) Role: Responsible for realization method to generate a new instance of duplicating an existing instance of
  • User (Client) Role: responsible for using the copy method to generate a new instance of the instance

Fourth, the sample code

1.Product categories:

package com.designpattern.cn.protptypepattern.patternframework;

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

  Product class above is very simple, but inherited java.lang.Clonable interface, which was not required to implement any method, which is a marker interface, the interface is the marked classes can call Clone method to clone class instance. Note that, Clone Clonable method is not defined in the interface, but rather is defined in java.lang.Object, the additional need to mention that, Clone implemented method is shallow copy.

2.Manager categories:

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();
    }
}
View Code

  The above Manager class provides methods register, will be saved to showcase the string and Product registration interfaces, and now can not know what the specific Product category is, but it is certain that specific class is an implementation of the Product interface, so you can call this class use and a method of creating a clone method createClone instance.

  Next, create a different Product specific subclass of several, each sub-classes implement the Product Interface:

3-1.MessageBox message box categories:

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;
    }
}
View Code

3-2UnderlinePen categories:

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;
    }
}
View Code

4. test class and run results:

Prototype model also has applications in JavaScript, the follow-up to add content in this area, you can compare different look in Java and Javascript implementation of the prototype model.

---------------------2019-06-27 00:13 比较晚了,今天加班到10点回家,原型模式还是比较重要的,这篇随笔后续继续补充-----------------------------

Guess you like

Origin www.cnblogs.com/zheng-hong-bo/p/11094496.html