原型模式
原型模式 用来创建重复的对象 涉及对象的拷贝,浅拷贝(基本数据类型+String)可以直接被拷贝, 其他类型需要自己实现。
demo
package creationalpattern.prototypattern;
import java.util.HashMap;
import java.util.Map;
/**
* @author tx
* @version 1.0
* @date 2024/1/6 19:24
* @description:
* 原型模式
* 用来创建重复的对象
* 涉及对象的拷贝,浅拷贝(基本数据类型+String)可以直接被拷贝,
* 其他类型需要自己实现。
*/
public class PrototypePattern {
public static void main(String[] args) throws CloneNotSupportedException {
// 创建缓存对象
ProductCache productCache = new ProductCache();
ComplexItem complexItem = new ComplexItem("22222");
// 放入一些原型
productCache
.setPrototype("A", new AProduct().setAge(18).setName("小美").setComplexItem(complexItem))
.setPrototype("B", new BProduct().setAge(24).setName("张三").setComplexItem(complexItem));
// 获取原型的clone
Product a1 = productCache.getInstance("A");
a1.run();
Product a2 = productCache.getInstance1("A");
a2.run();
// 修改原型上的复杂对象
complexItem.setName2("修改");
// String 比较特殊,修改String时相当于创建了一个新的String,并吧当前的引用对象更新。
Product a = productCache.getPrototype("A");
a.setName("小美变如花");
a1.run();
a2.run();
Product b = productCache.getInstance("B");
b.run();
Product b1 = productCache.getInstance1("B");
b1.run();
}
}
/**
* 抽象原型,定义原型具备的方法和可被复制。
*/
abstract class Product implements Cloneable{
// 基本类型
private int age;
private String name;
// 复杂类型
private ComplexItem complexItem;
public Product clone() throws CloneNotSupportedException {
Product clone = (Product) super.clone();
return clone;
}
/**
* 深拷贝
* @param i
* @return
* @throws CloneNotSupportedException
*/
public Product clone(Integer i) throws CloneNotSupportedException {
Product clone = this.clone();
clone.complexItem = new ComplexItem(this.complexItem.name2());
return clone;
}
public Product setAge(int age) {
this.age = age;
return this;
}
public Product setName(String name) {
this.name = name;
return this;
}
public Product setComplexItem(ComplexItem complexItem) {
this.complexItem = complexItem;
return this;
}
public abstract void run();
@Override
public String toString() {
return "Product{" +
"age=" + age +
", name='" + name + '\'' +
", complexItem=" + complexItem +
'}'+ getClass().getName() + "@" + Integer.toHexString(hashCode());
}
}
class ComplexItem{
private String name2;
public ComplexItem(String name2) {
this.name2 = name2;
}
public ComplexItem setName2(String name2) {
this.name2 = name2;
return this;
}
public String name2() {
return name2;
}
@Override
public String toString() {
return "ComplexItem{" +
"name2='" + name2 + '\'' +
'}';
}
}
/**
* 一个具体的原型 A
*/
class AProduct extends Product{
@Override
public void run() {
System.out.println("原型A");
System.out.println(this);
}
}
/**
* 一个具体的原型 B
*/
class BProduct extends Product{
@Override
public void run() {
System.out.println("原型B--------------");
System.out.println(this);
}
}
/**
* 一个容器来装载原型,每次获取返回该对象的clone;
*/
class ProductCache{
private static Map<String,Product> cache = new HashMap<>();
/**
* 放入原型
* @param id 原型标识
* @param prototype 原型
* @return this
*/
public ProductCache setPrototype(String id,Product prototype){
cache.put(id,prototype);
return this;
}
/**
* 获取对应id原型的一个复制体
* @param id 原型id
* @return product
*/
public Product getInstance(String id) throws CloneNotSupportedException {
Product product = cache.get(id);
return product!=null?product.clone():null;
}
public Product getPrototype(String id){
return cache.get(id);
}
/**
* 不使用super.clone, 使用object 的clone 方法
* @param id
* @return
* @throws CloneNotSupportedException
*/
public Product getInstance1(String id) throws CloneNotSupportedException {
Product product = cache.get(id);
return product!=null?product.clone(1):null;
}
}