设计模式: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
相关推荐
willow2 天前
Axios由浅入深
设计模式·axios
七月丶4 天前
别再手动凑 PR 了:这个 AI Skill 会按仓库习惯自动建分支、拆提交、提 PR
人工智能·设计模式·程序员
刀法如飞4 天前
从程序员到架构师:6大编程范式全解析与实践对比
设计模式·系统架构·编程范式
九狼4 天前
Flutter + Riverpod +MVI 架构下的现代状态管理
设计模式
静水流深_沧海一粟5 天前
04 | 别再写几十个参数的构造函数了——建造者模式
设计模式
StarkCoder5 天前
从UIKit到SwiftUI的迁移感悟:数据驱动的革命
设计模式
阿星AI工作室5 天前
给openclaw龙虾造了间像素办公室!实时看它写代码、摸鱼、修bug、写日报,太可爱了吧!
前端·人工智能·设计模式
_哆啦A梦6 天前
Vibe Coding 全栈专业名词清单|设计模式·基础篇(创建型+结构型核心名词)
前端·设计模式·vibecoding
阿闽ooo9 天前
中介者模式打造多人聊天室系统
c++·设计模式·中介者模式