大话设计模式_原型模式

以复制简历为例子.

package com.wzs.design;

/**
 * 大话设计模式--page88 原型模式
 * 
 * @author Administrator
 * 
 */
public class PrototypePattern {
	public static void main(String[] args) throws CloneNotSupportedException {
		WorkExperience workExperience = new WorkExperience("2011-2-2", "2013-3-3", "中国微软");
		Resume a = new Resume("大鸟", "男", "23", workExperience);

		Resume b = (Resume) a.clone();
		WorkExperience workExperience2 = new WorkExperience("2011-2-2", "2013-3-3", "中国谷歌");
		b.setWorkExperience(workExperience2);

		Resume c = (Resume) a.clone();
		c.setAge("99");

		a.display();
		b.display();
		c.display();
	}
}

/*
 * 简历
 */
class Resume implements Cloneable {
	private String name;
	private String sex;
	private String age;
	private WorkExperience workExperience;

	public Resume(String name, String sex, String age, WorkExperience workExperience) {
		this.name = name;
		this.sex = sex;
		this.age = age;
		this.workExperience = workExperience;
	}

	public void display() {
		System.out.println("name:" + name + " sex:" + sex + " age:" + age + " " + workExperience);
	}

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

	public String getName() {
		return name;
	}

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

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public String getAge() {
		return age;
	}

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

	public WorkExperience getWorkExperience() {
		return workExperience;
	}

	public void setWorkExperience(WorkExperience workExperience) {
		this.workExperience = workExperience;
	}

}

/*
 * 工作经历
 */
class WorkExperience {
	private String workDataStart;// 工作开始时间
	private String workDataEnd;// 工作结束时间
	private String company;// 公司

	public WorkExperience(String workDataStart, String workDataEnd, String company) {
		this.workDataStart = workDataStart;
		this.workDataEnd = workDataEnd;
		this.company = company;
	}

	public String getWorkDataStart() {
		return workDataStart;
	}

	public void setWorkDataStart(String workDataStart) {
		this.workDataStart = workDataStart;
	}

	public String getWorkDataEnd() {
		return workDataEnd;
	}

	public void setWorkDataEnd(String workDataEnd) {
		this.workDataEnd = workDataEnd;
	}

	public String getCompany() {
		return company;
	}

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

	@Override
	public String toString() {
		return " 开始时间:" + workDataStart + " 结束时间:" + workDataEnd + " 公司名称:" + company;
	}

}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值