当前位置:   article > 正文

C#设计模式--原型模式_c#原型

c#原型

0.C#设计模式--简单工厂模式

1.C#设计模式--工厂方法模式

2.C#设计模式--抽象工厂模式

3.C#设计模式--单例模式

4.C#设计模式--建造者模式

设计模式

原型模式(Prototype Pattern)

简单介绍:

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

这种模式是实现了一个原型接口,该接口用于创建当前对象的克隆。当直接创建对象的代价比较大时,则采用这种模式。例如,一个对象需要在一个高代价的数据库操作之后被创建。我们可以缓存该对象,在下一个请求时返回它的克隆,在需要的时候更新数据库,以此来减少数据库调用。

原型模式参与者:

  Prototype:原型类,声明一个Clone自身的接口;

  ConcretePrototype:具体原型类,实现一个Clone自身的操作。

  在原型模式中,Prototype通常提供一个包含Clone方法的接口,具体的原型ConcretePrototype使用Clone方法完成对象的创建。

原型模式类图:

原型模式c#代码示例:

Prototype类 原型基类

  1. public abstract class Prototype
  2. {
  3. private string _id;
  4. public Prototype(string id)
  5. {
  6. this._id = id;
  7. }
  8. public string Id
  9. {
  10. get { return _id; }
  11. }
  12. public abstract Prototype Clone();
  13. }

ConcretePrototype1类 具体的原型类实现Clone方法

  1. public class ConcretePrototype1:Prototype
  2. {
  3. public ConcretePrototype1(string id)
  4. : base(id)
  5. {
  6. }
  7. public override Prototype Clone()
  8. {
  9. return (Prototype)this.MemberwiseClone();
  10. }
  11. }

ConcretePrototype2类 具体的原型类实现Clone方法

  1. public class ConcretePrototype2:Prototype
  2. {
  3. public ConcretePrototype2(string id)
  4. : base(id)
  5. {
  6. }
  7. public override Prototype Clone()
  8. {
  9. return (Prototype)this.MemberwiseClone();
  10. }
  11. }

测试调用

  1. class Client
  2. {
  3. static void Main(string[] args)
  4. {
  5. ConcretePrototype1 p1 = new ConcretePrototype1("I");
  6. ConcretePrototype1 c1 = (ConcretePrototype1)p1.Clone();
  7. Console.WriteLine("Cloned: {0}", c1.Id);
  8. ConcretePrototype2 p2 = new ConcretePrototype2("II");
  9. ConcretePrototype2 c2 = (ConcretePrototype2)p2.Clone();
  10. Console.WriteLine("Cloned: {0}", c2.Id);
  11. Console.Read();
  12. }
  13. }

运行结果:

 

源代码工程文件下载

 

原型模式手机举例

MobilePhonePrototype类 手机原型基类

  1. public abstract class MobilePhonePrototype
  2. {
  3. private string _brand;
  4. public string Brand
  5. {
  6. get { return _brand; }
  7. }
  8. public MobilePhonePrototype(string brand)
  9. {
  10. this._brand = brand;
  11. }
  12. public abstract MobilePhonePrototype Clone();
  13. }

XiaoMiPrototype类 小米手机原型类

  1. public class XiaoMiPrototype:MobilePhonePrototype
  2. {
  3. public XiaoMiPrototype(string brand)
  4. : base(brand)
  5. {
  6. }
  7. public override MobilePhonePrototype Clone()
  8. {
  9. return (MobilePhonePrototype)this.MemberwiseClone();
  10. }
  11. }

ApplePrototype类 苹果手机原型类

  1. public class ApplePrototype:MobilePhonePrototype
  2. {
  3. public ApplePrototype(string brand)
  4. : base(brand)
  5. {
  6. }
  7. public override MobilePhonePrototype Clone()
  8. {
  9. return (MobilePhonePrototype)this.MemberwiseClone();
  10. }
  11. }

用户测试类

  1. class Client
  2. {
  3. static void Main(string[] args)
  4. {
  5. XiaoMiPrototype xiaomi = new XiaoMiPrototype("小米");
  6. XiaoMiPrototype xiaomi2 = (XiaoMiPrototype)xiaomi.Clone();
  7. Console.WriteLine(xiaomi2.Brand);
  8. ApplePrototype iphone = new ApplePrototype("iPhone7 Plus");
  9. ApplePrototype iphone2 = (ApplePrototype)iphone.Clone();
  10. Console.WriteLine(iphone2.Brand);
  11. Console.Read();
  12. }
  13. }

运行结果:

源码工程文件下载

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号