`

PrototypePattern

阅读更多

意图
用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象.

 

原型模式基于.net中深拷贝和浅拷贝的概念.

注意:如果不明白深拷贝和浅拷贝的概念请查看:http://jhxk.iteye.com/blog/373211

 

 

适用性
1).当要实例化的类是在运行时刻指定时,例如,通过动态装载

 

2).为了避免创建一个与产品类层次平行的工厂类层次时
什么叫做与产品类平行的工厂类层次?
当你去产生一个产品类必然要有一个工厂,第二个产品又有第二个工厂,
这样工厂非常多.通过原型模式就能有效的避免这点.

 

3).当一个类的实例只能有几个不同状态组合中的一种时.建立相应数目
的原型并克隆它们可能比每次用合适的状态手工实例化该类更方便一些.

原型模式的构成

 

组成:
抽象原型类(Prototype):定义具有克隆自己方法的接口.
具体原型类(ConcretePrototype):实现具体的克隆方法.
客户类(Client):通过克隆生成一个新的对象.

 

实际上PrototypePattern非常简单
比如:我想去创建一个对象,但是我不想通过工厂方或new的方式去创建
我想根据已有对象的状态去创建一个新的对象.
那么当你创建完一个对象再克隆一个对象,对克隆的对象进行微小的修
改就可以直接使用了,而并不需要复杂的一些对象创建过程.比较方便.

 

ClassDiagram:

 

 

SequenceDiagram:

 

示例:

    class client
    {
        public static void Main(String[] args)
        {
            ConcretePrototype1 p1 = new ConcretePrototype1("I");

            ConcretePrototype1 c1 = p1.Clone() as ConcretePrototype1;

            Console.WriteLine("Cloned:{0}", c1.Id);

            ConcretePrototype2 p2 = new ConcretePrototype2("II");

            ConcretePrototype2 c2 = p2.Clone() as ConcretePrototype2;

            Console.WriteLine("Cloned:{0}", c2.Id);

            Console.ReadKey();
        }
    }

    /// <summary>
    /// 抽象原型类
    /// </summary>
    abstract class Prototype
    {
        private string id;

        public Prototype(string id)
        {
            this.id = id;
        }

        public string Id
        {
            get { return id; }
        }

        public abstract Prototype Clone();
    }

    /// <summary>
    /// 具体原型类1
    /// </summary>
    class ConcretePrototype1 : Prototype
    {
        public ConcretePrototype1(string id)
            : base(id)
        {

        }

        public override Prototype Clone()
        {
            return this.MemberwiseClone() as Prototype;
        }
    }

    class ConcretePrototype2 : Prototype
    {
        public ConcretePrototype2(string id)
            : base(id)
        {

        }

        public override Prototype Clone()
        {
            return this.MemberwiseClone() as Prototype;
        }
    }

 

  • 大小: 29.9 KB
  • 大小: 3.5 KB
分享到:
评论

相关推荐

    Prototype Pattern原型模式

    原型模式(Prototype Pattern)是用于创建重复的对象,同时又能保证性能。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。

    原型模式 Prototype Pattern

    原型模式(Prototype Pattern)是用于创建重复的对象,同时又能保证性能。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。

    创建型模式之原型模式(Prototype Pattern)

    6、原型模式(Prototype Pattern) 用意:通过拷贝创建一个新的对象

    原型模式(Prototype Pattern)原理图

    原型模式是一种创建型设计模式,它通过复制一个现有的对象来创建新的对象,而不是通过调用构造函数的方式。这种方式可以在运行时动态地创建和修改对象,而不需要知道具体的创建细节 。 原型模式的基本概念包括以下...

    [创建型模式]设计模之原型模式(Prototype Pattern)

    NULL 博文链接:https://jacky-dai.iteye.com/blog/2295379

    .NET设计模式(6):原型模式(PrototypePattern)

    .NET设计模式(6):原型模式(PrototypePattern)

    php示例详解Constructor Prototype Pattern 原型模式

    抽象原型(Prototype)角色:声明一个克隆自己的接口 具体原型(Concrete Prototype)角色:实现一个克隆自己的操作 当一个类大部分都是相同的只有部分是不同的时候,如果需要大量这个类的对象,每次都重复实例化那些...

    C#版 24种设计模式

    适配器模式(Adapter Pattern) 提供者模式(Provider Pattern) 外观模式(Facade Pattern) 享元模式(Flyweight Pattern) 原型模式(Prototype Pattern) 责任链模式(Chain of Responsibility Pattern) 中介者模式...

    Java设计模式,并加上个人理解

    10. 原型模式 (Prototype Pattern) 11. 组合模式 (Composite Pattern) 12. 装饰者模式 (Decorator Pattern) 13. 访问者模式 (Visitor Pattern) 14. 迭代器模式 (Iterator Pattern) 15. 享元模式 ...

    23种设计模式的基本介绍

    创建型模式(Creational Pattern) 1、 抽象工厂模式(Abstract Factory Pattern) 介绍 提供一个创建...3、 原型模式(Prototype Pattern) 介绍 用原型实例指定创建对象的种类,并且通过拷贝这个原型来创建新的对象。。。。

    用Java实现23种设计模式

    原型模式(Prototype Pattern) 2. 结构型模式 适配器模式(Adapter Pattern) 桥接模式(Bridge Pattern) 过滤器模式(Filter、Criteria Pattern) 组合模式(Composite Pattern) 装饰器模式(Decorator ...

    设计模式PPT

     原型模式(Prototype Pattern)  单例模式(Singleton Pattern) 结构型模式用来处理类或者对象的组合,主要包含以下7种设计模式:  适配器模式(Adapter Pattern)  桥接模式(Bridge Pattern)  组合...

    设计模式_原型模式.zip

    原型模式(Prototype Pattern)是用于创建重复的对象,同时又能保证性能。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。 这种模式是实现了一个原型接口,该接口用于创建当前对象的克隆。当...

    C#设计模式-吕震宇

    C#设计模式(9)-Prototype Pattern C#设计模式(8)-Builder Pattern C#设计模式(7)-Singleton Pattern C#设计模式(6)-Abstract Factory Pattern C#设计模式(5)-Factory Method Pattern C#设计模式(4)...

    33种JAVA设计模式DEMO

    原型模式(Prototype Pattern) 2 结构型模式 这些设计模式关注类和对象的组合。继承的概念被用来组合接口和定义组合对象获得新功能的方式。 适配器模式(Adapter Pattern) 桥接模式(Bridge Pattern) 过滤器模式...

    design-pattern-java.pdf

    (二) 确保对象的唯一性——单例模式 (三) 确保对象的唯一性——单例模式 (四) 确保对象的唯一性——单例模式 (五) 原型模式-Prototype Pattern 对象的克隆——原型模式(一) 对象的克隆——原型模式(二) ...

    .NET Design Patterns [Kindle Edition]

    After reading this book, you will be able to convincingly leverage these design patterns (factory pattern, builder pattern, prototype pattern, adapter pattern, facade pattern, decorator pattern, ...

    Apress.Pro.Design.Patterns.in.Swift

    The Prototype Pattern Chapter 6. The Singleton Pattern Chapter 7. The Object Pool Pattern Chapter 8. Object Pool Variations Chapter 9. The Factory Method Pattern Chapter 10. The Abstract Factory ...

Global site tag (gtag.js) - Google Analytics