Design Pattern note 4 (prototype)

Source: Internet
Author: User

Prototype

The prototype mode is used as the prototype when multiple instances need to be created. Other instances copy/clone the prototype to obtain similar instances.

Our lab recently studied this model because of market reasons. due to long-standing customs and policies on the market, the demand for women is quite high. All of us will expand the production line for women, however, for financial reasons, we cannot invest hardware costs, but we can only improve our approach.

First, we need to analyze the composition of women:

class NvRen {    private String name;    private String head;    private String body;    private String type;    public NvRen(String name, String head, String body, String type) {        this.name = name;        this.head = head;        this.body = body;        this.type = type;    }}

When we used to produce women, we passed in the predefined names, headers, body types, and types when creating women (well, I admit that women are hard to understand, but we tried it in spring and autumn ~~).

Public class prototype {public static void main (string [] ARGs) {nvren nvren1 = new nvren ("Xiao xianyuan", "melon face", "Huludao ", "elegant temperament"); nvren nvren2 = new nvren ("songtaifeng", "Yuanyuan", "Huludao", "neighboring Yujie "); nvren nvren3 = new nvren ("rice to Love", "melon face", "gourd", "cute"); nvren nvren4 = new nvren ("Luo Yufeng ", "noodle face", "barrel", "disgusting ");}}

After a long research, our marketing department manager finally came up with the idea: as the rich and handsome customers do not need our products, the customers who need our products are basically ugly and poor, they do not have much requirements for our products. As long as they have different names, I think we can produce women in batches and set different names for them at the factory, so we changed our female:

class NvRen implements Cloneable {    private String name;    private String head;    private String body;    private String type;    public NvRen(String name, String head, String body, String type) {        this.name = name;        this.head = head;        this.body = body;        this.type = type;    }    public void setName(String name) {        this.name = name;    }    @Override    protected NvRen clone() {        NvRen nr = null;        try {            nr = (NvRen) super.clone();        } catch (CloneNotSupportedException e) {            nr = new NvRen(this.name, this.head, this.body, this.type);        }        return nr;    }}

Now we can have a very happy batch production.

Public class prototype {public static void main (string [] ARGs) {nvren nvren1 = new nvren ("Xiao xianyuan", "melon face", "Huludao ", "elegant temperament"); nvren nvren2 = nvren1.clone (); nvren2.setname ("songtaofeng"); nvren nvren3 = nvren1.clone (); nvren3.setname (" "); nvren nvren4 = nvren1.clone (); // nvren4.setname ("Luo Yufeng"); // This is really hard to sell, even if only the name is different from other ones, nvren4.setname ("Cang ");}}

When our products were put on the market, they were broken down because they complained that only one of the women we produced was sold with different names.

We can only study it again, and finally find that one of our engineers has made some changes to our female for more convenient production:

class RouTi {    private String head;    private String body;    private String type;    public RouTi(String head, String body, String type) {        this.head = head;        this.body = body;        this.type = type;    }}class NvRen implements Cloneable {    private String name;    private RouTi routi;    public NvRen(String name, String head, String body, String type) {        this.name = name;        this.routi = new RouTi(head, body, type);    }    private NvRen(String name, RouTi routi) {        this.name = name;        this.routi = routi;    }    public void setName(String name) {        this.name = name;    }    public RouTi getRouTi() {        return this.routi;    }    @Override    protected NvRen clone() {        NvRen nr = null;        try {            nr = (NvRen) super.clone();        } catch (CloneNotSupportedException e) {            nr = new NvRen(this.name, this.routi);        }        return nr;    }}

We did some tests and found that although these women have different names, they share the same body.

Public class prototype {public static void main (string [] ARGs) {nvren nvren1 = new nvren ("Xiao xianyuan", "melon face", "Huludao ", "elegant temperament"); system. out. println (nvren1.getroui (). tostring (); nvren nvren2 = nvren1.clone (); nvren2.setname ("Matsushima Feng"); system. out. println (nvren2.getroui (). tostring (); nvren nvren3 = nvren1.clone (); nvren3.setname (""); system. out. println (nvren3.getroui (). tostring (); nvren nvren4 = nvren1.clone (); // nvren4.setname ("Luo Yufeng"); // This is really hard to sell, even if only the name is different from others, nvren4.setname ("Cang "); system. out. println (nvren4.getroui (). tostring () ;}// ------> output // COM. pattern. now. roui@ 1fc4bec // COM. pattern. now. roui@ 1fc4bec // COM. pattern. now. roui@ 1fc4bec // COM. pattern. now. roui@ 1fc4bec

The study found that:When we use the clone method, we only perform the shortest clone. The shortest clone only copies the attribute of the value type, and the property of the reference type only copies its reference (that is, the pointer) instead of copying the entire referenced object.

So when we want to clone a woman, We Have To clone the body:

class RouTi implements Cloneable {    private String head;    private String body;    private String type;    public RouTi(String head, String body, String type) {        this.head = head;        this.body = body;        this.type = type;    }    @Override    protected RouTi clone() {        RouTi rt = null;        try {            rt = (RouTi) super.clone();        } catch (CloneNotSupportedException e) {            rt = new RouTi(this.head, this.body, this.type);        }        return rt;    }}class NvRen implements Cloneable {    private String name;    private RouTi routi;    public NvRen(String name, String head, String body, String type) {        this.name = name;        this.routi = new RouTi(head, body, type);    }    private NvRen(String name, RouTi routi) {        this.name = name;        this.routi = routi;    }    public void setName(String name) {        this.name = name;    }    public RouTi getRouTi() {        return this.routi;    }    @Override    protected NvRen clone() {        NvRen nr = new NvRen(this.name, this.routi.clone());        return nr;    }}

Then we can get the right result: different women have different bodies

//--->output//com.pattern.now.RouTi@1fc4bec//com.pattern.now.RouTi@dc8569//com.pattern.now.RouTi@1bab50a//com.pattern.now.RouTi@c3c749

This is a question raised by one of our otaku engineers: when our physical classes Add a new attribute called likewho, and the value is female:

private NvRen likeWho;

Brother can only say: in this case, either your female has a design problem, or you have to give up the prototype.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.