1. 程式人生 > >java設計模式精講 Debug 方式+記憶體分析 第9章 原型模式

java設計模式精講 Debug 方式+記憶體分析 第9章 原型模式

原型模式

9-1 原型模式講解

在這裡插入圖片描述


在這裡插入圖片描述


在這裡插入圖片描述


在這裡插入圖片描述


在這裡插入圖片描述


9-2 原型模式coding

有一個Mail類:

public class Mail {
    private String name;
    private String emailAddress;
    private String content;

    public Mail() {
System.out.println("Mail Class Constructor"); } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEmailAddress() { return emailAddress; } public void setEmailAddress(String emailAddress)
{ this.emailAddress = emailAddress; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } @Override public String toString() { return "Mail{" + "name='" + name +
'\'' + ", emailAddress='" + emailAddress + '\'' + ", content='" + content + '\'' + '}'; } }

還有一個傳送郵件的類:
注:這裡面的MessageFormat.format方法是用來拼接字串的;

public class MailUtil {
    public static void sendMail(Mail mail) {
        String outputContent = "向{0}同學,郵件地址:{1},郵件內容:{2}傳送郵件成功!";
        System.out.println(MessageFormat.format(outputContent, mail.getName(), mail.getEmailAddress(), mail.getContent()));
    }

    public static void saveOriginMailRecord(Mail mail) {
        System.out.println("儲存OriginMail記錄,OriginMail:"+mail.getContent());
    }
}

測試:

public class Test {
    public static void main(String[]args){
        Mail mail = new Mail();
        mail.setContent("初始化的模板");
        for (int i = 0; i < 10; i++) {
            mail.setName("姓名"+i);
            mail.setEmailAddress("姓名" + i + "@126.com");
            mail.setContent("恭喜您,此次活動中獎了!");
            MailUtil.sendMail(mail);
        }
        MailUtil.saveOriginMailRecord(mail);
    }
}

測試結果:

Mail Class Constructor
向姓名0同學,郵件地址:姓名[email protected],郵件內容:恭喜您,此次活動中獎了!傳送郵件成功!
向姓名1同學,郵件地址:姓名[email protected],郵件內容:恭喜您,此次活動中獎了!傳送郵件成功!
向姓名2同學,郵件地址:姓名[email protected],郵件內容:恭喜您,此次活動中獎了!傳送郵件成功!
向姓名3同學,郵件地址:姓名[email protected],郵件內容:恭喜您,此次活動中獎了!傳送郵件成功!
向姓名4同學,郵件地址:姓名[email protected],郵件內容:恭喜您,此次活動中獎了!傳送郵件成功!
向姓名5同學,郵件地址:姓名[email protected],郵件內容:恭喜您,此次活動中獎了!傳送郵件成功!
向姓名6同學,郵件地址:姓名[email protected],郵件內容:恭喜您,此次活動中獎了!傳送郵件成功!
向姓名7同學,郵件地址:姓名[email protected],郵件內容:恭喜您,此次活動中獎了!傳送郵件成功!
向姓名8同學,郵件地址:姓名[email protected],郵件內容:恭喜您,此次活動中獎了!傳送郵件成功!
向姓名9同學,郵件地址:姓名[email protected],郵件內容:恭喜您,此次活動中獎了!傳送郵件成功!
儲存OriginMail記錄,OriginMail:恭喜您,此次活動中獎了!


現在,我們來用原型模式來寫:
我們讓Mail擁有克隆的功能,實現Cloneable介面:

public class Mail implements Cloneable{
    private String name;
    private String emailAddress;
    private String content;

    public Mail() {
        System.out.println("Mail Class Constructor");
    }

    public String getName() {
        return name;
    }

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

    public String getEmailAddress() {
        return emailAddress;
    }

    public void setEmailAddress(String emailAddress) {
        this.emailAddress = emailAddress;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    @Override
    public String toString() {
        return "Mail{" +
                "name='" + name + '\'' +
                ", emailAddress='" + emailAddress + '\'' +
                ", content='" + content + '\'' +
                '}';
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        System.out.println("Clone mail object");
        return super.clone();
    }
}

測試:

public class Test {
    public static void main(String[]args) throws CloneNotSupportedException {
        Mail mail = new Mail();
        mail.setContent("初始化的模板");
        for (int i = 0; i < 10; i++) {
            /** 使用克隆出來的Mail */
            Mail mailTemp = (Mail) mail.clone();
            mailTemp.setName("姓名"+i);
            mailTemp.setEmailAddress("姓名" + i + "@126.com");
            mailTemp.setContent("恭喜您,此次活動中獎了!");
            MailUtil.sendMail(mailTemp);
        }
        MailUtil.saveOriginMailRecord(mail);
    }
}

測試結果:

Mail Class Constructor
Clone mail object
向姓名0同學,郵件地址:姓名[email protected],郵件內容:恭喜您,此次活動中獎了!傳送郵件成功!
Clone mail object
向姓名1同學,郵件地址:姓名[email protected],郵件內容:恭喜您,此次活動中獎了!傳送郵件成功!
Clone mail object
向姓名2同學,郵件地址:姓名[email protected],郵件內容:恭喜您,此次活動中獎了!傳送郵件成功!
Clone mail object
向姓名3同學,郵件地址:姓名[email protected],郵件內容:恭喜您,此次活動中獎了!傳送郵件成功!
Clone mail object
向姓名4同學,郵件地址:姓名[email protected],郵件內容:恭喜您,此次活動中獎了!傳送郵件成功!
Clone mail object
向姓名5同學,郵件地址:姓名[email protected],郵件內容:恭喜您,此次活動中獎了!傳送郵件成功!
Clone mail object
向姓名6同學,郵件地址:姓名[email protected],郵件內容:恭喜您,此次活動中獎了!傳送郵件成功!
Clone mail object
向姓名7同學,郵件地址:姓名[email protected],郵件內容:恭喜您,此次活動中獎了!傳送郵件成功!
Clone mail object
向姓名8同學,郵件地址:姓名[email protected],郵件內容:恭喜您,此次活動中獎了!傳送郵件成功!
Clone mail object
向姓名9同學,郵件地址:姓名[email protected],郵件內容:恭喜您,此次活動中獎了!傳送郵件成功!
Disconnected from the target VM, address: ‘127.0.0.1:61704’, transport: ‘socket’
儲存OriginMail記錄,OriginMail:初始化的模板


在原型模式中,還有一種常見的使用方法:通過抽象類來實現原型模式
父類實現了克隆的介面:

public abstract class A implements Cloneable {
    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}

子類也具有克隆的方法:

public class B extends A {
    public static void main(String[]args) throws CloneNotSupportedException {
        B b = new B();
        b.clone();
    }
}

在實際的開發當中,直接讓目標類實現Cloneable介面的方式用的比較多;


我們來說說克隆:深拷貝和淺拷貝
有一個Pig類:

public class Pig implements Cloneable{
    private String name;
    private Date birthday;

    public Pig(String name, Date birthday) {
        this.name = name;
        this.birthday = birthday;
    }

    public String getName() {
        return name;
    }

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

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

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

    @Override
    public String toString() {
        return "Pig{" +
                "name='" + name + '\'' +
                ", birthday=" + birthday +
                '}'+super.toString();
    }
}

測試:

public class Test {
    public static void main(String[]args) throws CloneNotSupportedException {
        Date birthday = new Date(0L);
        Pig pig1 = new Pig("佩奇", birthday);
        Pig pig2 = (Pig) pig1.clone();
        System.out.println(pig1);
        System.out.println(pig2);
    }
}

測試結果:這是兩個不同的物件

Pig{name=‘佩奇’, birthday=Thu Jan 01 08:00:00 CST 1970}[email protected]
Pig{name=‘佩奇’, birthday=Thu Jan 01 08:00:00 CST 1970}[email protected]


現在,我們來修改pig1的生日:
我們預期是隻修改pig1的時間:

public class Test {
    public static void main(String[]args) throws CloneNotSupportedException {
        Date birthday = new Date(0L);
        Pig pig1 = new Pig("佩奇", birthday);
        Pig pig2 = (Pig) pig1.clone();
        System.out.println(pig1);
        System.out.println(pig2);

        pig1.getBirthday().setTime(666666666L);
        System.out.println(pig1);
        System.out.println(pig2);
    }
}

但是實際的結果是把兩個物件的時間都進行了修改,這個就是和我們的預期就是不一樣了:

Pig{name=‘佩奇’, birthday=Thu Jan 01 08:00:00 CST 1970}[email protected]
Pig{name=‘佩奇’, birthday=Thu Jan 01 08:00:00 CST 1970}[email protected]
Pig{name=‘佩奇’, birthday=Fri Jan 09 01:11:06 CST 1970}[email protected]
Pig{name=‘佩奇’, birthday=Fri Jan 09 01:11:06 CST 1970}[email protected]


在這裡插入圖片描述
這種預設的克隆方式就是一個淺克隆的方式;


現在,我們來實現一個深克隆:
在Pig的這個類裡面實現這個方法:

    @Override
    protected Object clone() throws CloneNotSupportedException {
        Pig pig = (Pig) super.clone();
        /** 深克隆 */
        pig.birthday = (Date) pig.birthday.clone();
        return pig;
    }

Pig的這個類如下:

public class Pig implements Cloneable{
    private String name;
    private Date birthday;

    public Pig(String name, Date birthday) {
        this.name = name;
        this.birthday = birthday;
    }

    public String getName() {
        return name;
    }

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

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        Pig pig = (Pig) super.clone();
        /** 深克隆 */
        pig.birthday = (Date) pig.birthday.clone();
        return pig;
    }

    @Override
    public String toString() {
        return "Pig{" +
                "name='" + name + '\'' +
                ", birthday=" + birthday +
                '}'+super.toString();
    }
}

這個時候,日期物件就不是同一個物件了:
在這裡插入圖片描述
這個時候,輸出就符合了我們的預期了:只修改了Pig1

Pig{name=‘佩奇’, birthday=Thu Jan 01 08:00:00 CST 1970}[email protected]
Pig{name=‘佩奇’, birthday=Thu Jan 01 08:00:00 CST 1970}[email protected]
Pig{name=‘佩奇’, birthday=Fri Jan 09 01:11:06 CST 1970}[email protected]
Pig{name=‘佩奇’, birthday=Thu Jan 01 08:00:00 CST 1970}[email protected]


總結:對於引用型別的變數,我們一定是注意是否要深克隆它,對於引用型別,我們還是建議克隆出來為好,否則就是留了一個隱患;


9-3 原型模式coding-克隆破壞單例

讓之前寫的單例模式實現Cloneable介面:

public class HungrySingleton implements Serializable,Cloneable{
    private final static HungrySingleton hungrySingleton;
    static {
        hungrySingleton = new HungrySingleton();
    }
    private HungrySingleton() {
        if (hungrySingleton != null) {
            throw new RuntimeException("單例構造器禁止反射呼叫");
        }
    }
    public static HungrySingleton getInstance() {
        return hungrySingleton;
    }
    /** 我們加上這樣的一個方法 */
    private Object readResolve() {
        return hungrySingleton;
    }

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

測試:

public class Test {
    public static void main(String[]args) throws CloneNotSupportedException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        HungrySingleton hungrySingleton = HungrySingleton.getInstance();
        Method method = hungrySingleton.getClass().getDeclaredMethod("clone");
        method.setAccessible(true);
        HungrySingleton cloneHungrySingleton = (HungrySingleton) method.invoke(hungrySingleton);
        System.out.println(hungrySingleton);
        System.out.println(cloneHungrySingleton);
    }
}

執行結果:

[email protected]5dca
[email protected]8049


如果我們想要克隆不破壞單例,那麼我們可以這樣做:

public class HungrySingleton implements Serializable,Cloneable{
    private final static HungrySingleton hungrySingleton;
    static {
        hungrySingleton = new HungrySingleton();
    }
    private HungrySingleton() {
        if (hungrySingleton != null) {
            throw new RuntimeException("單例構造器禁止反射呼叫");
        }
    }
    public static HungrySingleton getInstance() {
        return hungrySingleton;
    }
    /** 我們加上這樣的一個方法 */
    private Object readResolve() {
        return hungrySingleton;
    }

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

現在的執行結果:這個時候,是同一個物件,還是單例的

[email protected]5dca
[email protected]5dca


如何防止克隆不破壞單例模式:
1.要麼不實現Cloneable介面
2.要麼實現了Cloneable介面,但是在重寫的方法clone方法裡面獲得物件的例項

這樣的話,就不怕克隆破壞單例模式了;


9-4 原型模式原始碼解析

在這裡插入圖片描述


在這裡插入圖片描述


我們檢視一下它的實現:
在這裡插入圖片描述


在這裡插入圖片描述
那它是如何重寫的呢?
在這裡插入圖片描述