06原型模式,最后一个创建型模式

原型模式

原型模式 用来创建重复的对象 涉及对象的拷贝,浅拷贝(基本数据类型+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;
    }
}
  • 28
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值