欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

原型模式学习笔记

程序员文章站 2022-06-12 22:30:15
...

简历的基本代码

package com.work;

import java.io.Console;

public class Main{
	public static void main(String[] args) {

	}
}
//简历
class Resume{
	private String name;
	private String sex;
	private String age;
	private String timeArea;
	private String company;
	public Resume(String name) {
		this.name = name;
	}
	//设置个人信息
	public void SetPersonalInfo(String sex,String age) {
		this.sex = sex;
		this.age = age;
	}
	//设置个人经历
	public void SetWorkExperience(String timeArea,String company) {
		this.timeArea = timeArea;
		this.company = company;
	}
	//显示
	public void Display() {
		System.out.println("{0} {1} {2}"+" "+name+" "+sex+" "+age);
		System.out.println("工作经历: {0} {1} "+ timeArea+" "+ company);
	}
}

基本代码的客户端调用代码

static void Main(String[] args) {
	Resume a = new Resume("大鸟");
	a.SetPersonalInfo("男", "29");
	a.SetWorkExperience("1998-2000", "xx公司");
	
	Resume b = new Resume("大鸟");
	b.SetPersonalInfo("男", "29");
	b.SetWorkExperience("1998-2000", "xx公司");

	Resume c= new Resume("大鸟");
	c.SetPersonalInfo("男", "29");
	c.SetWorkExperience("1998-2000", "xx公司");
	a.Display();
	b.Display();
	c.Display();
}

结果显示:

大鸟 男 29
工作经历 1998-2000 XX公司
大鸟 男 29
工作经历 1998-2000 XX公司
大鸟 男 29
工作经历 1998-2000 XX公司

这样的基本代码过于麻烦,下面是传引用的方法

static void Main(String[] args) {
	Resume a = new Resume("大鸟");
	a.SetPersonalInfo("男", "29");
	a.SetWorkExperience("1998-2000", "xx公司");
	
	Resume b = a;
	
	Resume c = a;
	
	a.Display();
	b.Display();
	c.Display();
}

下面为原型模式的具体实现:
原型类:

class Prototype implements Cloneable{
	private String id;
	public Prototype(String id) {
		this.id = id;
	}
	public String getId(){
		return id;
	}
	protected Object clone() throws CloneNotSupportedException {
		return (Prototype)super.clone();
	}
}

具体原型类:

class ConcretePrototype1 implements Cloneable{
	Prototype a ;
	public ConcretePrototype1(Prototype  a) {
		this.a = a;}
	protected Object clone() throws CloneNotSupportedException {
		return ( ConcretePrototype1 )super.clone();}
}

客户端代码

public class Main {
	public static void main(String[] args) throws CloneNotSupportedException {
		
		 ConcretePrototype1 p1 = new  ConcretePrototype1 (new Prototype("123"));
		 ConcretePrototype1 p2 = (ConcretePrototype1) p1.clone();
		 System.out.println(p1);
		 System.out.println(p2);
	}
}

简历类的原型实现:

package com.work;



class Resume implements Cloneable{
	private String name;
	private String sex;
	private String age;
	private String timeArea;
	private String company;
	
	public Resume(String name) {
		this.name = name;
	}
	//设置个人信息
	public void SetPersonalInfo(String sex,String age) {
		this.sex = sex;
		this.age = age;
	}
	//设置工作经历
	public void SetWorkExperience(String timeArea,String company) {
		this.timeArea = timeArea;
		this.company = company;
	}
	//显示
	public void Display() {
		System.out.println(name+" "+sex+" "+age);
		System.out.println(timeArea+" "+company);
	}
	protected Object clone() throws CloneNotSupportedException {
		return super.clone();
	}
}

新的客户端代码

//客户端调用代码
public class Main{
	public static void main(String[] args) throws CloneNotSupportedException {
		Resume a = new Resume("大鸟");
		a.SetPersonalInfo("男", "29");
		a.SetWorkExperience("1998-2000", "xx公司");
		Resume b = (Resume) a.clone();
		b.SetWorkExperience("1998-2006", "YY企业");
		Resume c = (Resume)a.clone();
		c.SetPersonalInfo("男", "24");
		a.Display();
		b.Display();
		c.Display();
	}
}

输出结果:

大鸟 男 29
工作经历 1998-2000 XX公司
大鸟 男 29
工作经历 1998-2006 YY企业
大鸟 男 24 
工作经历 1998-2000 XX公司