网站首页 > 技术文章 正文
1.什么是原型模式?
通过给出一个一个原型对象来指明所要创建的对象的类型,然后用复制这个原型对象的办法创建出更多同类型的对象。
2.简单形式的原始模型
- 客户(Client)角色:客户类提出创建对象的请求。
 - 抽象(Prototype)原型角色:这是一个抽象角色,通常由一个Java接口或Java抽象类实现。此角色给出所有的具体原型类所需的接口。
 - 具体原型(Concrete Prototype)角色:被复制的对象。此角色需要实现抽象的原型角色所要求的接口。
 
public interface Prototype extends Cloneable{
	public Object clone();
}
public class Client1 {
	private Prototype prototype;
	
	public void operation(Prototype example) {
		Prototype p = (Prototype)example.clone();
	}
}
public class ConcretePrototype implements Prototype{
	@Override
	protected Object clone() {
		try {
		return super.clone();
		}catch (Exception e) {
			// TODO: handle exception
			return null;
		}
	}
	
}
public interface Prototype extends Cloneable{
	Prototype clone();
}
3.登记形式的原始模型模式
- 客户端(Client)角色:客户端类向管理员提出创建对象的请求。
 - 抽象原型(Prototype)角色:这是一个抽象角色,通常由一个接口或抽象类实现。此角色给出所有的具体原型类所需的接口。
 - 具体原型(Concrete Prototype)角色:被复制的对象。需要实现抽象的原型角色所要求的接口。
 - 原型管理器(Prototype Manager)角色:创建具体原型类的对象,并记录每一个被创建的对象。
 
public interface Prototype extends Cloneable{
	public Object clone();
}
public class ConcretePrototype implements Prototype{
	@Override
	public synchronized Object clone() {
		
		Prototype temp = null;
		try {
			temp = (Prototype) super.clone();
			return temp;
		} catch (CloneNotSupportedException e) {
			e.printStackTrace();
		}finally {
			return temp;
		}
	}
}
import java.util.Vector;
public class PrototypeManager {
	@SuppressWarnings("rawtypes")
	private Vector objects=new Vector();
	
	@SuppressWarnings("unchecked")
	public void add(Prototype object) {
		objects.add(object);
	}
	
	public Prototype get(int i) {
		
		return (Prototype) objects.get(i);
	}
	
	public int getSize() {
		
		return objects.size();
	}
}
public class Client {
	private PrototypeManager mgr;
	private Prototype prototype;
	public void registerPrototype() {
		
		prototype = new ConcretePrototype();
		Prototype copytype = (Prototype) prototype.clone();
		mgr.add(copytype);
	}
}
4.浅复制(浅克隆)
被复制对象的所有变量都含有与原来的对象相同的值,而所有的对其他对象的引用都仍然指向原来的对象。换言之,浅复制仅仅复制所考虑的对象,而不复制它所引用的对象。
public class GoldRingedStaff {
	private float height=100.0F;
	private float diameter=10.0F;
	
	public GoldRingedStaff() {
	}
	
	public void grow() {
		this.diameter*=2.0;
		this.height*=2;
	}
	
	public void shrink() {
		this.diameter/=2;
		this.height/=2;
	}
	
	public void move() {
		
	}
	public float getHeight() {
		return height;
	}
	public void setHeight(float height) {
		this.height = height;
	}
	public float getDiameter() {
		return diameter;
	}
	public void setDiameter(float diameter) {
		this.diameter = diameter;
	}
	
}
import java.util.Date;
public class Monkey implements Cloneable{
	private int height;
	private int weight;
	private GoldRingedStaff staff;
	private Date birthDate;
	
	public Monkey() {
		super();
	}
	public Monkey(Date birthDate) {
		super();
		this.birthDate = birthDate;
	}
	
	@SuppressWarnings("finally")
	public Object clone() {
		Monkey temp=null;
		
		try {
			temp = (Monkey) super.clone();
		} catch (CloneNotSupportedException e) {
			e.printStackTrace();
		}finally {
			return temp;
		}
	}
	public int getHeight() {
		return height;
	}
	public void setHeight(int height) {
		this.height = height;
	}
	public int getWeight() {
		return weight;
	}
	public void setWeight(int weight) {
		this.weight = weight;
	}
	public Date getBirthDate() {
		return birthDate;
	}
	public void setBirthDate(Date birthDate) {
		this.birthDate = birthDate;
	}
	public GoldRingedStaff getStaff() {
		return staff;
	}
	public void setStaff(GoldRingedStaff staff) {
		this.staff = staff;
	}
	
}
import java.util.Date;
public class TheGreatestSage {
	private Monkey monkey = new Monkey(new Date());
	
	public void change() {
		
		Monkey copyMonkey;
		for(int i=0;i<1000;i++) {}
		copyMonkey=(Monkey) monkey.clone();
		System.out.println(monkey.getBirthDate());
		System.out.println(copyMonkey.getBirthDate());
		System.out.println(monkey==copyMonkey);
		System.out.println(monkey.getStaff()==copyMonkey.getStaff());
	}
	
	public static void main(String[] args) {
		
		TheGreatestSage sage = new TheGreatestSage();
		sage.change();
	}
	
}
5.深复制(深克隆)
被复制对象的所有的变量都含有与原来的对象的相同的值,除去那些引用其他对象的变量。那些引用其他对象的变量将指向被复制过的新对象,而不再是原有的那些被引用的对象。换言之,深复制把要复制的对象所引用的对象都复制一遍,而这种对被引用的对象的复制叫间接复制。
import java.io.Serializable;
public class GoldRingedStaff implements Cloneable,Serializable{
	private float height=100.0F;
	private float diameter=10.0F;
	
	public GoldRingedStaff() {
	}
	
	public void grow() {
		this.diameter*=2.0;
		this.height*=2;
	}
	
	public void shrink() {
		this.diameter/=2;
		this.height/=2;
	}
	
	public void move() {
		
	}
	public float getHeight() {
		return height;
	}
	public void setHeight(float height) {
		this.height = height;
	}
	public float getDiameter() {
		return diameter;
	}
	public void setDiameter(float diameter) {
		this.diameter = diameter;
	}
	
	
}
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Date;
public class Monkey implements Cloneable,Serializable{
	private int height;
	private int weight;
	private GoldRingedStaff staff;
	private Date birthDate;
	
	public Monkey() {
		this.birthDate = new Date();
		this.staff = new GoldRingedStaff();
	}
	public Object deepClone() throws IOException, ClassNotFoundException {
		
		ByteArrayOutputStream bo = new ByteArrayOutputStream();
		ObjectOutputStream oo = new ObjectOutputStream(bo);
		oo.writeObject(this);
		
		ByteArrayInputStream bi = new ByteArrayInputStream(bo.toByteArray());
		ObjectInputStream oi = new ObjectInputStream(bi);
		return oi.readObject();
	}
	
	
	@SuppressWarnings("finally")
	public Object clone() {
		Monkey temp=null;
		
		try {
			temp = (Monkey) super.clone();
		} catch (CloneNotSupportedException e) {
			e.printStackTrace();
		}finally {
			return temp;
		}
	}
	public int getHeight() {
		return height;
	}
	public void setHeight(int height) {
		this.height = height;
	}
	public int getWeight() {
		return weight;
	}
	public void setWeight(int weight) {
		this.weight = weight;
	}
	public Date getBirthDate() {
		return birthDate;
	}
	public void setBirthDate(Date birthDate) {
		this.birthDate = birthDate;
	}
	public GoldRingedStaff getStaff() {
		return staff;
	}
	public void setStaff(GoldRingedStaff staff) {
		this.staff = staff;
	}
	
}
import java.io.IOException;
import java.util.Date;
public class TheGreatestSage {
	private Monkey monkey = new Monkey();
	
	public void change() throws ClassNotFoundException, IOException {
		
		Monkey copyMonkey;
		for(int i=0;i<1000;i++) {}
		copyMonkey=(Monkey) monkey.deepClone();
		System.out.println(monkey.getBirthDate());
		System.out.println(copyMonkey.getBirthDate());
		System.out.println(monkey==copyMonkey);
		System.out.println(monkey.getStaff()==copyMonkey.getStaff());
	}
	
	public static void main(String[] args) {
		
		TheGreatestSage sage = new TheGreatestSage();
		try {
			sage.change();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
}
猜你喜欢
- 2024-10-02 Maven 的这 7 个问题你思考过没有?
 - 2024-10-02 220、Class.forName 和 ClassLoader 有什么区别?
 - 2024-10-02 Alluxio使用贴士:客户端显示找不到FileSystem类问题分析与解决
 - 2024-10-02 异常还不知道是什么?一文教会你异常是什么,如何优雅处理
 - 2024-10-02 十年架构师干货总结:Java 的类加载机制
 - 2024-10-02 线上故障排查全套路盘点,运维大哥请自查
 - 2024-10-02 一文读懂 Java 异常处理(java异常处理的方法)
 - 2024-10-02 反射必杀技:深入了解Class类,让你一通百通
 - 2024-10-02 Java异常的面试问题及答案-Part 2
 - 2024-10-02 Python异常处理(python异常处理的关键字)
 
- 最近发表
 - 
- 聊一下 gRPC 的 C++ 异步编程_grpc 异步流模式
 - [原创首发]安全日志管理中心实战(3)——开源NIDS之suricata部署
 - 超详细手把手搭建在ubuntu系统的FFmpeg环境
 - Nginx运维之路(Docker多段构建新版本并增加第三方模
 - 92.1K小星星,一款开源免费的远程桌面,让你告别付费远程控制!
 - Go 人脸识别教程_piwigo人脸识别
 - 安卓手机安装Termux——搭建移动服务器
 - ubuntu 安装开发环境(c/c++ 15)_ubuntu安装c++编译器
 - Rust开发环境搭建指南:从安装到镜像配置的零坑实践
 - Windows系统安装VirtualBox构造本地Linux开发环境
 
 
- 标签列表
 - 
- cmd/c (90)
 - c++中::是什么意思 (84)
 - 标签用于 (71)
 - 主键只能有一个吗 (77)
 - c#console.writeline不显示 (95)
 - pythoncase语句 (88)
 - es6includes (74)
 - sqlset (76)
 - apt-getinstall-y (100)
 - node_modules怎么生成 (87)
 - chromepost (71)
 - flexdirection (73)
 - c++int转char (80)
 - mysqlany_value (79)
 - static函数和普通函数 (84)
 - el-date-picker开始日期早于结束日期 (76)
 - js判断是否是json字符串 (75)
 - c语言min函数头文件 (77)
 - asynccallback (87)
 - localstorage.removeitem (77)
 - vector线程安全吗 (73)
 - java (73)
 - js数组插入 (83)
 - mac安装java (72)
 - 无效的列索引 (74)
 
 
