现在的位置: 首页 > 综合 > 正文

大话设计模式_原型模式

2013年06月10日 ⁄ 综合 ⁄ 共 2091字 ⁄ 字号 评论关闭

以复制简历为例子.

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;
	}

}

抱歉!评论已关闭.