Prototype of Design Pattern

Source: Internet
Author: User

Prototype of Design Pattern

The object created by the factory method is a new object, and its attributes are generally default.

What I understand is that the prototype operation method creates a clone body, which can be considered as two steps: 1. creates an object. 2. copy the attributes of the created object as the cloned object.

 

For example, the story of Sun Wukong and six-ears macaques ..

Apart from being a new monkey, the six-ear macaques are all copied by Sun Wukong.

 

This involves the concept of a shortest clone and a deep clone:

Semi-cloning: if other objects are referenced in the attributes of the cloned object, the reference still uses the previous one. For example, the six-ear macaques are replaced, master or Tang Miao, younger brother or BA Jie

Deep cloning: the attributes of the cloned object are new. In the above example, the whole world is changed to two copies, with six-ear macaques plus Tang Seng 2, BA Jie 2, and tianting 2, buddha 2 .,, everything has nothing to do with the cloned copy.

 

First let's start with a shortest clone.

 

Package prototype; import java. io. serializable; public class Monkey implements Cloneable, Serializable {private static final long serialVersionUID = 1L; private TangSanzang teacher; public void check (Monkey ref) {if (ref! = Null & ref = this) {System. out. println (this is the same monkey);} else {System. out. println (this is not the same monkey);} teacher. check (ref. getTeacher ();} public TangSanzang getTeacher () {return teacher;} public void setTeacher (TangSanzang teacher) {this. teacher = teacher;} @ Overridepublic Monkey clone () throws CloneNotSupportedException {return (Monkey) super. clone ();}}

Package prototype; import java. io. Serializable; public class TangSanzang implements Serializable {private static final long serialVersionUID = 1L; public void check (TangSanzang ref) {if (ref! = Null & ref = this) {System. out. println (this is the same Tang sanzang); return;} System. out. println (this is not the same Tang sanzang );}}

Review builder mode, and pretend that monkey creation is complicated.

 

 

package prototype;public class MonkeyBuilder {private Monkey monkey = new Monkey();public MonkeyBuilder setTeacher(TangSanzang teacher) {monkey.setTeacher(teacher);return this;}public Monkey build() {return monkey;}}

@Testpublic void testShallow() {TangSanzang teacher = new TangSanzang();Monkey monkey = new MonkeyBuilder().setTeacher(teacher).build();try {Monkey m2 = monkey.clone();monkey.check(m2);} catch (CloneNotSupportedException e) {e.printStackTrace();}}

Result:

 

This is not the same monkey.

 

Let's look at the deep clone:

 

@Testpublic void testDeep() throws Exception {TangSanzang teacher = new TangSanzang();Monkey monkey = new MonkeyBuilder().setTeacher(teacher).build();ByteArrayOutputStream baos = new ByteArrayOutputStream();ObjectOutputStream oos = new ObjectOutputStream(baos);oos.writeObject(monkey);ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));Object o = ois.readObject();Monkey m2=(Monkey) o;monkey.check(m2);}

 

Result:

This is not the same monkey.

 

 

 

The shortest clone only copies itself, and the others are the original ones. The deep clone copies the entire (related) World,

When to use deep cloning and when to use light cloning, specific analysis is required

 

 

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.