"Illustrates a design mode" mode study notes 3-2 Prototype

Origin Prototype (Prototype) mode

Create an instance, you can keyword new creation. But sometimes, we need to create an instance without specifying the class name of the premise, such as:

  • Objects need to deal with a wide range, it can not be integrated into a single class, if separately as a class, the number of classes and too much.

  • Difficult to generate through code examples: for example, the user painted in graphic drawing tools example, if the code is created, then it is very difficult. We can first be saved, by the time required to copy generated instances.

  • When I want decoupling frame and generated instance: you do not specify the class name to create an instance, but advance registration a prototype instance, to create a new instance by copying the example. Such as printers, you do not need to know the specific content of the document, the time required to copy on the line, how many times no problem copying.

Class Diagram

Code

Product interfaces, inheritance Cloneable interface, implementation class Product interface can be copied using the clone instance method.
The method of use indicates how to use, how to use specific, implemented by subclasses.
createClone is an example of a method for copying.

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

Manager classes create methods, need to generate an object class can call a replicated instance of the class Manager

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

To copy specific objects of class

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

    @Override
    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("");
    }

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

public class UnderlinePen implements Product {
    private char ulchar;
    public UnderlinePen(char ulchar) {
        this.ulchar = ulchar;
    }
    @Override
    public void use(String s) {
        int length = s.getBytes().length;
        System.out.println("\"" + s + "\"");
        System.out.print(" ");
        for (int i = 0; i < length; i++) {
            System.out.print(ulchar);
        }
        System.out.println("");
    }

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

how to use

public static void main(String[] args) {
        Manager manager = new Manager();
        UnderlinePen upen = new UnderlinePen('~');
        MessageBox mbox = new MessageBox('*');
        MessageBox sbox = new MessageBox('/');

        manager.register("Strong Message", upen);
        manager.register("Warning box", mbox);
        manager.register("Slash box", sbox);


        Product p1 = manager.create("Strong Message");
        p1.use("hello");

        Product p2 = manager.create("Warning box");
        p2.use("hello");

        Product p3 = manager.create("Slash box");
        p3.use("hello");
}
    //结果
    "hello"
    ~~~~~
    *********
    * hello *
    *********
    /////////
    / hello /
    /////////

Character

Prototype (Prototype)

This role is responsible for copying the new method instance defined by the existing instance . This embodiment corresponds to the interface of the Product.

ConcretePrototype (specific prototype)

Responsible way to achieve the prototype . It corresponds to the present embodiment and the MessageBox UnderlinePen

Client (users)

Responsible method to generate a new copy of the prototype instance .

FIG Mode Class

I understand

  说实话,由于经验所限,我不太能体会的到这个模式的好处。
  看看示例代码:代码实现的功能类似于一个文本编辑器,可以对文本进行下划线(UnderlinePen)和提示(MessageBox)两个操作。在一篇文章里面,可能有很多很多个下划线和提示操作。每一个都创建对象,创建过程简单还好,复杂的话那代码量就太恐怖了。所以搞一个原型对象,用到了就复制就行了。
  这只是我能理解的其中一个好处,更多内容可以看看这篇文章https://www.cnblogs.com/chenssy/p/3313339.html


另外原型模式涉及到了对象的深拷贝和浅拷贝。区别就在于:复制对象时,是否复制对象中的引用指向的内存。那篇文章里面有解释。

Guess you like

Origin www.cnblogs.com/qianbixin/p/10992895.html