Prototype_Pattern

The Prototype_Pattern is the application of "template".

package prototype_pattern;

public abstract class Prototype implements Cloneable {
    protected String name;
    protected int age;
    protected Body body;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Body getBody() {
        return body;
    }

    public void setBody(Body body) {
        this.body = body;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        // The method of clone just copy the basic data type.
        Prototype person = (Prototype) super.clone();
        return person;
    }
}

package prototype_pattern;

public class ConcretePrototype extends Prototype {
    @Override
    protected Object clone() {//This method process the possible exception.
        Prototype person = null;
        try {
            // The method of clone just copy the basic data type.
            person = (ConcretePrototype) super.clone();
            person.name = new String(super.name);// The current is deep copy,without this sentence is shallow copy.
            person.body = (Body) body.clone();// The reference type need deep copy.
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return person;
    }
}

package prototype_pattern;

public class Body implements Cloneable{
    private double weight;
    private Physique physique;
    public double getWeight() {
        return weight;
    }
    public void setWeight(double weight) {
        this.weight = weight;
    }
    public Physique getPhysique() {
        return physique;
    }
    public void setPhysique(Physique pyysique) {
        this.physique = pyysique;
    }
    @Override
    protected Object clone() throws CloneNotSupportedException{
        Body body =(Body) super.clone();
        body.physique = (Physique)physique.clone();//deep copy
        return body;
    }
}

package prototype_pattern;

public class Physique implements Cloneable{
    private boolean health;
    
    public boolean isHealth() {
        return health;
    }

    public void setHealth(boolean health) {
        this.health = health;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException{
        return super.clone();
    }
}

package prototype_pattern;

public class Main {
    public static void main(String args[]){
        Prototype person = new ConcretePrototype();
        person.setAge(11);
        person.setName("sam");
        Physique physique = new Physique();
        physique.setHealth(true);
        Body body = new Body();
        body.setPhysique(physique);
        body.setWeight(65.5);
        person.setBody(body);
        Prototype newPerson = null;
        try {
            newPerson = (ConcretePrototype)person.clone();
        } catch (CloneNotSupportedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println(person.age == newPerson.age);
        System.out.println(person.name == newPerson.name);
        System.out.println(person.body == newPerson.body);
        System.out.println(person.body.getPhysique() == newPerson.body.getPhysique());
    }
}
 

The illustration of the "clone" method:

package prototype_pattern;

public class Prototype {
    private int age;

    public static void main(String args[]) {
        Prototype p = new Prototype();
        p.age = 1;
        try {
            Prototype q = (Prototype) p.clone();
            System.out.println(q.age);
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
    }
}
If we don't implement the interface of clonable,the running results will be listed as follows:

java.lang.CloneNotSupportedException: prototype_pattern.Prototype
    at java.lang.Object.clone(Native Method)
    at prototype_pattern.Prototype.main(Prototype.java:36)

The official website explains that:

Object.class

protected native Object clone() throws CloneNotSupportedException;

    /**
     * Returns a string representation of the object. In general, the
     * {@code toString} method returns a string that
     * "textually represents" this object. The result should
     * be a concise but informative representation that is easy for a
     * person to read.
     * It is recommended that all subclasses override this method.
     * <p>
     * The {@code toString} method for class {@code Object}
     * returns a string consisting of the name of the class of which the
     * object is an instance, the at-sign character `{@code @}', and
     * the unsigned hexadecimal representation of the hash code of the
     * object. In other words, this method returns a string equal to the
     * value of:
     * <blockquote>
     * <pre>
     * getClass().getName() + '@' + Integer.toHexString(hashCode())
     * </pre></blockquote>
     *
     * @return  a string representation of the object.
     */

Cloneable.class

/*
 * Copyright (c) 1995, 2004, Oracle and/or its affiliates. All rights reserved.
 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

package java.lang;

/**
 * A class implements the <code>Cloneable</code> interface to
 * indicate to the {@link java.lang.Object#clone()} method that it
 * is legal for that method to make a
 * field-for-field copy of instances of that class.
 * <p>
 * Invoking Object's clone method on an instance that does not implement the
 * <code>Cloneable</code> interface results in the exception
 * <code>CloneNotSupportedException</code> being thrown.
 * <p>
 * By convention, classes that implement this interface should override
 * <tt>Object.clone</tt> (which is protected) with a public method.
 * See {@link java.lang.Object#clone()} for details on overriding this
 * method.
 * <p>
 * Note that this interface does <i>not</i> contain the <tt>clone</tt> method.
 * Therefore, it is not possible to clone an object merely by virtue of the
 * fact that it implements this interface.  Even if the clone method is invoked
 * reflectively, there is no guarantee that it will succeed.
 *
 * @author  unascribed
 * @see     java.lang.CloneNotSupportedException
 * @see     java.lang.Object#clone()
 * @since   JDK1.0
 */
public interface Cloneable {
}

This is a general introduction to the 23 design patterns in chinese:
https://blog.csdn.net/GZHarryAnonymous/article/details/81567214

See more source code:[GZHarryAnonymous](https://github.com/GZHarryAnonymous/Design_Pattern)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值