设计模式:16、原型模式

目录

0、定义

1、原型模式的两种角色

2、原型模式的UML类图

3、示例代码


0、定义

用原型实例指定创建对象的种类,并且通过复制这些原型创建新的对象。

1、原型模式的两种角色

  • 抽象原型(Prototype):一个接口,负责定义对象复制自身方法。
  • 具体原型(ConcretePrototype):实现Prototype的接口的类。具体原型 实现抽象原型中的方法,以便所创建的对象调用该方法复制自己。

2、原型模式的UML类图

3、示例代码

抽象原型

java 复制代码
package xyz.jangle.design.prototype;

public interface Prototype {
	
	public Object cloneSelf() throws CloneNotSupportedException;

}

具体原型1

java 复制代码
package xyz.jangle.design.prototype;

public class ConcretePrototype1 implements Prototype ,Cloneable{

	int a,b,c;
	
	public ConcretePrototype1(int a, int b, int c) {
		super();
		this.a = a;
		this.b = b;
		this.c = c;
	}

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

}

具体原型2

java 复制代码
package xyz.jangle.design.prototype;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class ConcretePrototype2 implements Prototype, Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1905246759025489290L;
	
	private String str;

	public ConcretePrototype2(String str) {
		super();
		this.str = str;
	}

	@Override
	public Object cloneSelf() throws CloneNotSupportedException {
		Object object = null;
		try {
			ByteArrayOutputStream out1 = new ByteArrayOutputStream();
			ObjectOutputStream out2 = new ObjectOutputStream(out1);
			out2.writeObject(this);
			ByteArrayInputStream in1 = new ByteArrayInputStream(out1.toByteArray());
			ObjectInputStream in2 = new ObjectInputStream(in1);
			object = in2.readObject();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return object;
	}

	public String getStr() {
		return str;
	}

	public void setStr(String str) {
		this.str = str;
	}
	
	

}

具体原型3

java 复制代码
package xyz.jangle.design.prototype;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class ConcretePrototype3 implements Prototype, Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 7486786403960307872L;
	private AAA aaa;

	public AAA getAaa() {
		return aaa;
	}

	public void setAaa(AAA aaa) {
		this.aaa = aaa;
	}
	
	@Override
	public Object cloneSelf() throws CloneNotSupportedException {
		Object object = null;
		try {
			ByteArrayOutputStream out1 = new ByteArrayOutputStream();
			ObjectOutputStream out2 = new ObjectOutputStream(out1);
			out2.writeObject(this);
			ByteArrayInputStream in1 = new ByteArrayInputStream(out1.toByteArray());
			ObjectInputStream in2 = new ObjectInputStream(in1);
			object = in2.readObject();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return object;
	}

}


class AAA implements Serializable{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 963011062357603854L;
	private int a,b;

	public int getA() {
		return a;
	}

	public void setA(int a) {
		this.a = a;
	}

	public int getB() {
		return b;
	}

	public void setB(int b) {
		this.b = b;
	}
	
	
	
}

客户端(使用)

java 复制代码
package xyz.jangle.design.prototype;

public class AppMain16 {

	public static void main(String[] args) {
		
		try {
			// 基本数据类型克隆
			ConcretePrototype1 prototype1 = new ConcretePrototype1(1, 2, 3);
			ConcretePrototype1 self = (ConcretePrototype1) prototype1.cloneSelf();
			System.out.println("a:"+self.a+",b:"+self.b+",c:"+self.c);
			
			// 字符串克隆
			ConcretePrototype2 prototype2 = new ConcretePrototype2("123");
			ConcretePrototype2 self2  = (ConcretePrototype2) prototype2.cloneSelf();
			System.out.println(self2.getStr());
			self2.setStr("456");
			System.out.println(prototype2.getStr());
			
			// 对象克隆
			ConcretePrototype3 prototype3 = new ConcretePrototype3();
			AAA aaa = new AAA();
			aaa.setA(1);
			aaa.setB(2);
			prototype3.setAaa(aaa);
			ConcretePrototype3 self3 = (ConcretePrototype3) prototype3.cloneSelf();
			AAA aaa2 = self3.getAaa();
			System.out.println(aaa2.getA()+","+aaa2.getB());
			System.out.println(aaa==aaa2);	//false   克隆出的2个引用不一样
			aaa2.setA(7);
			aaa2.setB(8);
			System.out.println(aaa.getA()+","+aaa.getB());	// 1,2
			
		} catch (CloneNotSupportedException e) {
			e.printStackTrace();
		}

	}

}

输出结果:

bash 复制代码
a:1,b:2,c:3
123
123
1,2
false
1,2
相关推荐
Camel卡蒙28 分钟前
DDD架构——充血模型、领域模型
java·设计模式·架构
rongqing20191 小时前
Google 智能体设计模式:目标设定与监控
设计模式
weixin_445476682 小时前
一天一个设计模式——开闭原则
服务器·设计模式·开闭原则
李广坤2 小时前
模板方法模式(Template Method Pattern)
设计模式
冰糖雪梨dd3 小时前
JS中new的过程发生了什么
开发语言·javascript·原型模式
Asort8 小时前
JavaScript设计模式(十二)——代理模式 (Proxy)
前端·javascript·设计模式
简小瑞8 小时前
VSCode源码解密:Event<T> - 类型安全的事件系统
前端·设计模式·visual studio code
Mr_WangAndy8 小时前
C++设计模式_结构型模式_外观模式Facade
c++·设计模式·外观模式
李广坤8 小时前
策略模式(Strategy Pattern)
设计模式
努力努力再努力wz10 小时前
【C++进阶系列】:万字详解特殊类以及设计模式
java·linux·运维·开发语言·数据结构·c++·设计模式