设计模式之原型模式

Prototype Design Pattern



 


definition

Specify the kind of objects to create using a prototypical instance, and create new objects by copying this prototype.

Frequency of use:  nbsp;medium

UML class diagram

participants

    The classes and/or objects participating in this pattern are:

  • Prototype  (ColorPrototype)
    • declares an interface for cloning itself
  • ConcretePrototype  (Color)
    • implements an operation for cloning itself
  • Client  (ColorManager)
    • creates a new object by asking a prototype to clone itself

sample code in C#

This structural code demonstrates the Prototype pattern in which new objects are created by copying pre-existing objects (prototypes) of the same class.

Hide code

// Prototype pattern -- Structural example

using System;

namespace DoFactory.GangOfFour.Prototype.Structural
{
  
   // MainApp test application

   class MainApp
  {
    
     static void Main()
    {
       // Create two instances and clone each

      ConcretePrototype1 p1 = new ConcretePrototype1("I");
      ConcretePrototype1 c1 = (ConcretePrototype1)p1.Clone();
      Console.WriteLine ("Cloned: {0}", c1.Id);

      ConcretePrototype2 p2 = new ConcretePrototype2("II");
      ConcretePrototype2 c2 = (ConcretePrototype2)p2.Clone();
      Console.WriteLine ("Cloned: {0}", c2.Id);

       // Wait for user
      Console.Read();
    }
  }

   // "Prototype"

   abstract class Prototype
  {
     private string id;

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

     // Property
     public string Id
    {
       get{ return id; }
    }

     public abstract Prototype Clone();
  }

   // "ConcretePrototype1"

   class ConcretePrototype1 : Prototype
  {
     // Constructor
     public ConcretePrototype1( string id) : base(id)
    {
    }

     public override Prototype Clone()
    {
       // Shallow copy
       return (Prototype) this.MemberwiseClone();
    }
  }

   // "ConcretePrototype2"

   class ConcretePrototype2 : Prototype
  {
     // Constructor
     public ConcretePrototype2( string id) : base(id)
    {
    }

     public override Prototype Clone()
    {
       // Shallow copy
       return (Prototype) this.MemberwiseClone();
    }
  }
}

Output
Cloned: I
Cloned: II




This real-world code demonstrates the Prototype pattern in which new Color objects are created by copying pre-existing, user-defined Colors of the same type.



// Prototype pattern -- Real World example

using System;
using System.Collections;

namespace DoFactory.GangOfFour.Prototype.RealWorld
{
  
   // MainApp test application

   class MainApp
  {
     static void Main()
    {
      ColorManager colormanager = new ColorManager();

       // Initialize with standard colors
      colormanager["red" ] = new Color(255, 0, 0);
      colormanager["green"] = new Color( 0, 255, 0);
      colormanager["blue" ] = new Color( 0, 0, 255);

       // User adds personalized colors
      colormanager["angry"] = new Color(255, 54, 0);
      colormanager["peace"] = new Color(128, 211, 128);
      colormanager["flame"] = new Color(211, 34, 20);

      Color color;

       // User uses selected colors
       string name = "red";
      color = colormanager[name].Clone() as Color;

      name = "peace";
      color = colormanager[name].Clone() as Color;

      name = "flame";
      color = colormanager[name].Clone() as Color;

       // Wait for user
      Console.Read();
    }
  }

   // "Prototype"

   abstract class ColorPrototype
  {
     public abstract ColorPrototype Clone();
  }

   // "ConcretePrototype"

   class Color : ColorPrototype
  {
     private int red;
     private int green;
     private int blue;

     // Constructor
     public Color( int red, int green, int blue)
    {
       this.red = red;
       this.green = green;
       this.blue = blue;
    }

     // Create a shallow copy
     public override ColorPrototype Clone()
    {
      Console.WriteLine(
        "Cloning color RGB: {0,3},{1,3},{2,3}",
        red, green, blue);

       return this.MemberwiseClone() as ColorPrototype;
    }
  }

   // Prototype manager

   class ColorManager
  {
    Hashtable colors = new Hashtable();

     // Indexer
     public ColorPrototype this[ string name]
    {
       get
      {
         return colors[name] as ColorPrototype;
      }
       set
      {
        colors.Add(name, value);
      }
    }
  }
}

Output
Cloning color RGB: 255,  0,  0
Cloning color RGB: 128,211,128
Cloning color RGB: 211, 34, 20

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值