构造型模式

Builder(生成器)模式

Factory Method(工厂方法)模式

意图:用于创建对象的接口,同时控制对哪个类进行实例化。


Abstract Factory(抽象工厂)模式

意图:创建一系列相互关联或相互依赖的对象。

Prototype(原型)模式

prototype模式不通过实例化类来创建一个新的未初始化的实例,而是通过复制一个现有对象来生成新的对象。

构造型模式_第1张图片
浅拷贝
public class PackageInfo implements Cloneable {
    private Long id;
    private String receiverName;
    private String receiverAddress;
    private String senderName;
    private String senderPhoneNo;

    //other fields, getters, setters and other methods...


    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getReceiverName() {
        return receiverName;
    }

    public void setReceiverName(String receiverName) {
        this.receiverName = receiverName;
    }

    public String getReceiverAddress() {
        return receiverAddress;
    }

    public void setReceiverAddress(String receiverAddress) {
        this.receiverAddress = receiverAddress;
    }

    public String getSenderName() {
        return senderName;
    }

    public void setSenderName(String senderName) {
        this.senderName = senderName;
    }

    public String getSenderPhoneNo() {
        return senderPhoneNo;
    }

    public void setSenderPhoneNo(String senderPhoneNo) {
        this.senderPhoneNo = senderPhoneNo;
    }

    public PackageInfo clone() {
        try {
            return (PackageInfo)super.clone();
        } catch (CloneNotSupportedException e) {
            System.out.println("Cloning not allowed.");
            return null;
        }
    }


    public static PackageInfo clonePackage(String userName) {
        //load package as prototype data from db...
        PackageInfo prototype = loadPackageInfo(userName);

        //clone information...
        prototype = prototype.clone();

        //initialize copied data...
        prototype.setId(null);

        return prototype;
    }

    //Simulate loading data from database
    private static PackageInfo loadPackageInfo(String userName) {
        PackageInfo packageInfo = new PackageInfo();

        //create a String object in jvm heap not jvm string pool
        packageInfo.setId(100l);
        packageInfo.setReceiverName("John");
        packageInfo.setReceiverAddress("People Square,Shanghai");
        packageInfo.setSenderName("William");
        packageInfo.setSenderPhoneNo("12345678901");
        return packageInfo;
    }
}
public class PackageInfoTestDrive {
    public static void main(String[] args) {
        PackageInfo currentInfo = PackageInfo.clonePackage("John");
        System.out.println("Original package information:");
        display(currentInfo);

        currentInfo.setId(10000l);
        currentInfo.setReceiverName("Ryan");
        currentInfo.setReceiverAddress("People Square, Shanghai");

        System.out.println("\nNew package information:");
        display(currentInfo);
    }

    private static void display(PackageInfo currentInfo) {
        System.out.println("Package id: " + currentInfo.getId());
        System.out.println("Receiver name: " + currentInfo.getReceiverName());
        System.out.println("Receiver address: " + currentInfo.getReceiverAddress());
        System.out.println("Sender name: " + currentInfo.getSenderName());
        System.out.println("Sender Phone No.: " + currentInfo.getSenderPhoneNo());
    }
}

深拷贝(Deep Copy)
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
public class DeepCopyBean implements Serializable {
    private String objectField;
    private int primitiveField;

    public String getObjectField() {
        return objectField;
    }

    public void setObjectField(String objectField) {
        this.objectField = objectField;
    }

    public int getPrimitiveField() {
        return primitiveField;
    }

    public void setPrimitiveField(int primitiveField) {
        this.primitiveField = primitiveField;
    }

    public DeepCopyBean deepCopy() {
        try {
            ByteArrayOutputStream buf = new ByteArrayOutputStream();
            ObjectOutputStream o = new ObjectOutputStream(buf);
            o.writeObject(this);

            ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(buf.toByteArray()));
            return (DeepCopyBean) in.readObject();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }
}
public class DeepCopyBeanTestDrive {
    public static void main(String[] args) {
        DeepCopyBean originalBean = new DeepCopyBean();

        //create a String object in jvm heap not jvm string pool
        originalBean.setObjectField(new String("123456"));
        originalBean.setPrimitiveField(2);

        //clone this bean
        DeepCopyBean newBean = originalBean.deepCopy();

        System.out.println("Primitive ==? " + (newBean.getPrimitiveField() == originalBean.getPrimitiveField()));
        System.out.println("Object ==? " + (newBean.getObjectField() == originalBean.getObjectField()));
        System.out.println("Object equal? " + (newBean.getObjectField().equals(originalBean.getObjectField())));
    }
}



Memento(备忘录)模式

意图:为对象提供状态存储和状态恢复功能。

你可能感兴趣的:(模式)