创建型模式之原型模式(ProtoType)

ProtoType的英文意思是:原型、标准、模范。

原型模式的主旨思想即是基于一个现有的对象来构建一个新对象不是重新new创建),然后在新对象上作相应属性修改等以满足要求。

不用new,有多方面的原因,如有些对象的new操作开销较大,性能不好,如需要的对象类型是运行时决定的,编译时不知道类型,无法new等等。

原型模式也是创建出一个新对象,但不是new出来的,而是直接复制一个现有的对象 即克隆,或拷贝

对象的拷贝分为浅拷贝和 深拷贝,

浅拷贝:只拷贝简单属性的值和对象属性的地址,不拷贝引用对象本身,即浅拷贝创建出的对象和原对象有相同的属性对象引用;

深拷贝:在浅拷贝基础上,增加了属性引用对象的拷贝拷贝创建出的对象引用到的对象和原对象引用的性对象是不相同的对象,为避免被深拷贝过程出现循环引用的情况可以考虑对象序列化解决方案。


场景:商店有一种热销商品,数量有上万之多,甚至更多,但都一模一样,比如形状、大小、颜色、价格、厂商等都相同,唯独编号不同,这种类型的对象如果每个的哦new一次,如此多的对象会造成很大的开销,而且创建的对象几乎都一样,开销不值,最简单高效的方式是:复制,复制出来后改个编号即可


设计:



示例代码:

interface Toy {
    String show();
    void setId(String id);
    String getId();
    void setName(String name);
    String getName();
    void setSize(String size);
    String getSize();
    void setColor(String color);
    String getColor();
    void setCategory(Category category);
    Category getCategory();
}
class Category implements Cloneable {
    @Override
    public Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}
class HotSaleToy implements Toy, Cloneable {
    private String id;
    private String name;
    private String size;
    private String color;
    private Category category;
    @Override
    public Object clone() throws CloneNotSupportedException {
        HotSaleToy newToy = (HotSaleToy) super.clone();
        //手动进行 深层 复制,默认新对象和原对象的category是通一个,因为默认只复制一个引用,不复制引用的对象。
//        newToy.setCategory((Category) category.clone());
        return newToy;
    }
    @Override
    public String getId() {
        return id;
    }
    @Override
    public String show() {
        return java.text.MessageFormat.format("HotSaleToy'{'name=''{0}'', size=''{1}'', color=''{2}'''}'", name, size, color);
    }
    public void setId(String id) {
        this.id = id;
    }
    @Override
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @Override
    public String getSize() {
        return size;
    }
    public void setSize(String size) {
        this.size = size;
    }
    @Override
    public String getColor() {
        return color;
    }
    public void setColor(String color) {
        this.color = color;
    }
    @Override
    public Category getCategory() {
        return category;
    }
    @Override
    public void setCategory(Category category) {
        this.category = category;
    }
}

public class Test {
    public static void main(String[] args) throws CloneNotSupportedException {
        HotSaleToy hotSaleToy = new HotSaleToy();
        hotSaleToy.setId("1");
        hotSaleToy.setName("Genius");
        hotSaleToy.setSize("5L");
        hotSaleToy.setColor("orange");
        hotSaleToy.setCategory(new Category());
        Toy toy = hotSaleToy;

        Toy newToy = (Toy) hotSaleToy.clone();// 以hotSaleToy为模板,复制产生新的对象,十分快速
        newToy.setId("2");                    // 定制 修改,以满足需求

        System.out.println("玩具描述是否一样:" + (toy.show().equals(newToy.show())));
        System.out.println("玩具编号是否一样:" + (toy.getId().equals(newToy.getId())));
        // 如果没有手动进行属性复制,则为true,看HotSaleToy.clone()实现
        System.out.println("玩具种类是否一样:" + (toy.getCategory() == newToy.getCategory()));
        System.out.println("是否是同一个对象:" + (toy == newToy));
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
创建型: 1. 单件模式(Singleton Pattern) 2. 抽象工厂(Abstract Factory) 3. 建造者模式(Builder) 4. 工厂方法模式(Factory Method) 5. 原型模式(Prototype) 结构型: 6. 适配器模式(Adapter Pattern) 7. 桥接模式(Bridge Pattern) 8. 装饰模式(Decorator Pattern) 9. 组合模式(Composite Pattern) 10. 外观模式(Facade Pattern) 11. 享元模式(Flyweight Pattern) 12. 代理模式(Proxy Pattern) 13. 模板方法(Template Method) 14. 命令模式(Command Pattern) 15. 迭代器模式(Iterator Pattern) 行为型: 16. 观察者模式(Observer Pattern) 17. 解释器模式(Interpreter Pattern) 18. 中介者模式(Mediator Pattern) 19. 职责链模式(Chain of Responsibility Pattern) 20. 备忘录模式(Memento Pattern) 21. 策略模式(Strategy Pattern) 22. 访问者模式(Visitor Pattern) 23. 状态模式(State Pattern) 工程结构 ├─01.Singleton │ ├─html │ └─MySingleton │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties ├─02.ChainOfResponsibility │ ├─html │ ├─My2ChainOfResponsibility │ │ ├─bin │ │ │ └─Debug │ │ ├─obj │ │ │ └─Debug │ │ │ └─TempPE │ │ └─Properties │ └─MyChainOfResponsibility │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ ├─Refactor │ │ └─TempPE │ └─Properties ├─03.FactoryMethodMode │ ├─FactoryMethodMode │ │ ├─bin │ │ │ └─Debug │ │ ├─obj │ │ │ └─Debug │ │ │ └─TempPE │ │ └─Properties │ └─html ├─04.AbstractFactory │ ├─04.1.SimpleFactory │ │ ├─html │ │ └─SimpleFactory │ │ ├─bin │ │ │ └─Debug │ │ ├─obj │ │ │ └─Debug │ │ │ └─TempPE │ │ └─Properties │ ├─AbstractFactory │ │ ├─bin │ │ │ └─Debug │ │ ├─obj │ │ │ └─Debug │ │ │ └─TempPE │ │ └─Properties │ └─html ├─05.BuilderPattern │ ├─html │ └─MyBuilderPattern │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties ├─06.PrototypePattern │ ├─html │ │ └─C#设计模式(6)——原型模式Prototype Patt O技术博客_files │ └─PrototypePattern │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties ├─07.AdapterPattern │ ├─html │ └─MyAdapterPattern │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties ├─08.BridgePattern │ ├─html │ └─MyBridgePattern │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties ├─09.DecoratorPattern │ ├─html │ └─MyDecoratorPattern │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties ├─10.CompositePattern │ ├─html │ └─MyCompositePattern │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties ├─11.FacadePattern │ ├─html │ └─MyFacadePattern │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties ├─12.FlyweightPattern │ ├─html │ └─MyFlyweightPattern │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties ├─13.ProxyPattern │ ├─html │ └─MyProxyPattern │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties ├─14.TemplateMethod │ ├─html │ └─MyTemplateMethod │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties ├─15.VisitorPattern │ ├─html │ └─MyVisitorPattern │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties ├─16.StrategyPattern │ ├─html │ └─MyStrategyPattern │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties ├─17.StatePattern │ ├─html │ └─StatePattern │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties ├─18.MementoPattern │ ├─html │ └─MementoPattern │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties ├─19.MediatorPattern │ ├─html │ └─MediatorPattern │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties ├─20.OberverPattern │ ├─CatOberverPattern │ │ ├─bin │ │ │ └─Debug │ │ ├─obj │ │ │ └─Debug │ │ │ └─TempPE │ │ └─Properties │ ├─html │ └─MyOberverPattern │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties ├─21.IteratorPattern │ ├─html │ └─IteratorPattern │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties ├─22.InterpreterPattern │ ├─html │ └─MyInterpreterPattern │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties └─23.CommandPattern ├─html └─MyCommandPattern ├─bin │ └─Debug ├─obj │ └─Debug │ └─TempPE └─Properties

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值