原型模式-学习设计模式

做个笔记
引用
https://blog.csdn.net/lovelion/article/details/7424623

/**
 *接口类
 */

interface OfficialDocument extends Cloneable{
    public  OfficialDocument clone();

    public  void display();
}
public class A implements OfficialDocument {

    private String name;
    private String content;

    public A(String name,String content){
        this.name = name;
        this.content = content;
    }

    @Override
    public OfficialDocument clone() {
        Object obj = null;
        try {
            obj = super.clone();
            return (A)obj;
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void display() {
        System.out.println("A++++++++++++");
    }


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    @Override
    public String toString() {
        return "A{" +
                "name='" + name + '\'' +
                ", content='" + content + '\'' +
                '}';
    }
}
/**
 * 原型管理器
 */
public class PrototypeManager {

    private HashMap<String,OfficialDocument> map = new HashMap();

    //饿汉式
    private static PrototypeManager pm = new PrototypeManager();
    //私有构造器
    private PrototypeManager(){
        map.put("A",new A("ceshi","111"));
        map.put("B",new B());
    }

    //公有获取对现象的方法
    public static PrototypeManager getPm() {
        return pm;
    }


    public void addOfficialDocument(String  key,OfficialDocument doc){
        map.put(key,doc);
    }



    //通过浅克隆获取新的公文对象

    public OfficialDocument  getOfficialDocument(String key){
        return  ((OfficialDocument)map.get(key)).clone();
    }

    //先查看是否有 没有放入map
    public OfficialDocument getProto(Class<? extends OfficialDocument> type) {
        if (map.get(type.getClass().getSimpleName()) == null) {
            try {
                map.put(type.getSimpleName(), type.newInstance());
            } catch (InstantiationException | IllegalAccessException e) {
                e.printStackTrace();
                System.out.println("getProto InstantiationException IllegalAccessException ... ");
            }
        }
        return map.get(type.getSimpleName()).clone();
    }
}

public class Test {
    public static void main(String[] args) {
        PrototypeManager prototypeManager = PrototypeManager.getPm();
//        A a1= (A)prototypeManager.getProto(A.class);
//        a1.setName("测试员");
//        a1.setContent("测试第一个内容");
//
//        A a2= (A)prototypeManager.getProto(a1.getClass());
//        a2.setContent("测试第二个内容");

        A a1= (A)prototypeManager.getOfficialDocument("A");
        a1.setContent("测试第一个内容");
        A a2= (A)prototypeManager.getOfficialDocument("A");

        System.out.println(a1==a2);
        System.out.println(a1.toString());
        System.out.println(a2.toString());
    }
}

结果
false
A{name=‘ceshi’, content=‘测试第一个内容’}
A{name=‘ceshi’, content=‘111’}
两个对象不相同 切name后面没有赋值是前一个带过来克隆的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值