java 原型模式(大话设计模式)

原型模式:用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象。

  • 浅拷贝
/**
 * 原型模式(Prototype):浅拷贝
 * 工作经历类
 */
public class WorkExperience {

    private String workDate;

    private String company;

    public String getWorkDate() {
        return workDate;
    }

    public void setWorkDate(String workDate) {
        this.workDate = workDate;
    }

    public String getCompany() {
        return company;
    }

    public void setCompany(String company) {
        this.company = company;
    }

}

/**
 * 原型模式(Prototype):浅拷贝
 * 简历类
 */
public class Resume implements Cloneable {

    private String name;

    private String sex;

    private String age;

    private WorkExperience workExperience;

    public Resume(String name) {
        this.name = name;
        workExperience = new WorkExperience();
    }

    // 设置个人信息
    public void setPersonalInfo(String sex, String age) {
        this.sex = sex;
        this.age = age;
    }

    // 设置工作经历
    public void setWorkExperience(String workDate, String company) {
        workExperience.setWorkDate(workDate);
        workExperience.setCompany(company);
    }

    // 显示
    public void display() {
        System.out.println("姓名:" + name + ",性别:" + sex + ",年龄:" + age);
        System.out.println("工作经历:" + workExperience.getWorkDate() + "," + workExperience.getCompany());
    }

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

}
/**
 * 原型模式(Prototype):浅拷贝
 * 客户端main方法
 */
public class Client {

    public static void main(String[] args) throws CloneNotSupportedException {
        System.out.println("<-- 浅拷贝 -->");
        Resume r0 = new Resume("大鸟");
        r0.setPersonalInfo("男", "29");
        r0.setWorkExperience("1998-2000", "xx公司");

        Resume r1 = (Resume) r0.clone();
        r1.setWorkExperience("1998-2006", "yy企业");

        Resume r2 = (Resume) r0.clone();
        r2.setPersonalInfo("女", "18");
        r2.setWorkExperience("1998-2003", "zz企业");

        // 浅拷贝中工作经历的显示结果都是最后一次设置的值
        r0.display();
        r1.display();
        r2.display();

    }

}

  • 深拷贝
/**
 * 原型模式(Prototype):深拷贝
 * 工作经历类
 */
public class WorkExperience implements Cloneable {

    private String workDate;

    private String company;

    public String getWorkDate() {
        return workDate;
    }

    public void setWorkDate(String workDate) {
        this.workDate = workDate;
    }

    public String getCompany() {
        return company;
    }

    public void setCompany(String company) {
        this.company = company;
    }

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

}
/**
 * 原型模式(Prototype):深拷贝
 * 简历类
 */
public class Resume implements Cloneable {

    private String name;

    private String sex;

    private String age;

    private WorkExperience workExperience;

    public Resume(String name) {
        this.name = name;
        this.workExperience = new WorkExperience();
    }

    public Resume(WorkExperience workExperience) throws CloneNotSupportedException {
        this.workExperience = (WorkExperience) workExperience.clone();
    }

    // 设置个人信息
    public void setPersonalInfo(String sex, String age) {
        this.sex = sex;
        this.age = age;
    }

    // 设置工作经历
    public void setWorkExperience(String workDate, String company) {
        workExperience.setWorkDate(workDate);
        workExperience.setCompany(company);
    }

    // 显示
    public void display() {
        System.out.println("姓名:" + name + ",性别:" + sex + ",年龄:" + age);
        System.out.println("工作经历:" + workExperience.getWorkDate() + "," + workExperience.getCompany());
    }

    @Override
    public Object clone() throws CloneNotSupportedException {
        Resume resume = new Resume(this.workExperience);
        resume.name = this.name;
        resume.sex = this.sex;
        resume.age = this.age;
        return resume;
    }

}
/**
 * 原型模式(Prototype):深拷贝
 * 客户端main方法
 */
public class Client {

    public static void main(String[] args) throws CloneNotSupportedException {
        System.out.println("<-- 深拷贝 -->");
        Resume r0 = new Resume("大鸟");
        r0.setPersonalInfo("男", "29");
        r0.setWorkExperience("1998-2000", "xx公司");

        Resume r1 = (Resume) r0.clone();
        r1.setWorkExperience("1998-2006", "yy企业");

        Resume r2 = (Resume) r0.clone();
        r2.setPersonalInfo("女", "18");
        r2.setWorkExperience("1998-2003", "zz企业");

        // 深拷贝中工作经历的显示结果各不相同
        r0.display();
        r1.display();
        r2.display();

    }

}
  • 通过对象序列化,实现深度拷贝
/**
 * 工作经历类,为演示深度拷贝和浅度拷贝而用
 * 
 * @author liu yuning
 *
 */
public class WorkExperience implements Serializable {
    private static final long serialVersionUID = 1L;

    private String workDate;
    private String workCompany;

    public String getWorkDate() {
	return workDate;
    }

    public WorkExperience setWorkDate(String workDate) {
	this.workDate = workDate;

	return this;
    }

    public String getWorkCompany() {
	return workCompany;
    }

    public WorkExperience setWorkCompany(String workCompany) {
	this.workCompany = workCompany;

	return this;
    }

}
/**
 * 简历类
 * 
 * @author liu yuning
 *
 */
public class Resume implements Cloneable, Serializable {
    private static final long serialVersionUID = -4410449301166191440L;

    private String name;
    private String gender;
    private int age;

    // 引用“工作经历”对象,为演示深度拷贝和浅度拷贝而用
    private WorkExperience workExperience;

    public Resume() {
	// 在“简历”类实例化时,同时实例化“工作经历”类
	workExperience = new WorkExperience();
    }

    public void display() {
	System.out.println(this.getName() + " " + this.getGender() + " "
		+ this.getAge() + "\n工作经历: "
		+ this.getWorkExperience().getWorkDate() + " "
		+ this.getWorkExperience().getWorkCompany());
    }

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

    // 通过对象序列化,实现深度拷贝
    public Object deepClone() throws IOException, ClassNotFoundException {
	// 将对象写入流内
	ByteArrayOutputStream bos = new ByteArrayOutputStream();
	ObjectOutputStream oos = new ObjectOutputStream(bos);
	oos.writeObject(this);

	// 从流内读出对象
	ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(
		bos.toByteArray()));
	return ois.readObject();

    }

    public String getName() {
	return name;
    }

    public Resume setName(String name) {
	this.name = name;

	return this;
    }

    public String getGender() {
	return gender;
    }

    public Resume setGender(String gender) {
	this.gender = gender;

	return this;
    }

    public int getAge() {
	return age;
    }

    public Resume setAge(int age) {
	this.age = age;

	return this;
    }

    public WorkExperience getWorkExperience() {
	return workExperience;
    }

    public void setWorkExperience(String workDate, String workCompany) {
	workExperience.setWorkDate(workDate);
	workExperience.setWorkCompany(workCompany);
    }
}

/**
 * 原型模式客户端 演示深度拷贝和浅度拷贝
 * 
 * @Todo 考虑优化此处的重复代码
 * @author liu yuning
 *
 */
public class PrototypeClient {
    public static void shallowCopy() throws CloneNotSupportedException {
	Resume aResume = new Resume();
	aResume.setName("大鸟 ").setGender("男 ").setAge(25);
	aResume.setWorkExperience("1999-2002, ", "XX公司");

	Resume bResume = (Resume) aResume.clone();
	bResume.setWorkExperience("1999-2002, ", "YY公司");

	Resume cResume = (Resume) aResume.clone();
	cResume.setWorkExperience("1999-2002, ", "ZZ公司");

	System.out.println(">>>>>>浅度拷贝:");
	aResume.display();
	bResume.display();
	cResume.display();
    }

    public static void deepCopy() throws CloneNotSupportedException,
	    ClassNotFoundException, IOException {
	Resume aResume = new Resume();
	aResume.setName("大鸟 ").setGender("男 ").setAge(25);
	aResume.setWorkExperience("1999-2002, ", "XX公司");

	Resume bResume = (Resume) aResume.deepClone();
	bResume.setWorkExperience("1999-2002, ", "YY公司");

	Resume cResume = (Resume) aResume.deepClone();
	cResume.setWorkExperience("1999-2002, ", "ZZ公司");

	System.out.println(">>>>>>深度拷贝:");
	aResume.display();
	bResume.display();
	cResume.display();

    }

    public static void main(String[] args) throws CloneNotSupportedException,
	    ClassNotFoundException, IOException {

	// 浅度拷贝
	shallowCopy();

	System.out.println("==================================");

	// 深度拷贝
	deepCopy();

    }

}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值