另外

想有创造力的人

 

C#说明设计模式(五) 创建型的OVER

原型模式:Prototype
Definition

Specify the kind of objects to create using a prototypical instance, and create new objects by copying this prototype.
UML class diagram
原型模式
Participants

   The classes and/or objects participating in the Prototype pattern are:

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

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



using System;

// "Prototype"


abstract
class Prototype

{
  // Fields
  private string id;

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

  public
string Id

  {
    get{ return id; }
  }

 
// Methods
  abstract public Prototype Clone();
}

// "ConcretePrototype1"


class
ConcretePrototype1 : Prototype

{
  // Constructors
  public ConcretePrototype1( string id ) : base ( id ) {}

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

// "ConcretePrototype2"


class
ConcretePrototype2 : Prototype

{
  // Constructors
  public ConcretePrototype2( string id ) : base ( id ) {}

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

///
<summary>

/// Client test
/// </summary>
class Client
{
  public static void Main( string[] args )
  {
   // 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 );
  }
}


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.


using System;
using System.Collections;

// "Prototype"


abstract
class ColorPrototype

{
  // Methods
  public abstract ColorPrototype Clone();
}

// "ConcretePrototype"


class
Color : ColorPrototype

{
  // Fields
  private int red, green, blue;

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

 
// Methods
  public override ColorPrototype Clone()
  {
    // Creates a 'shallow copy'
    return (ColorPrototype) this.MemberwiseClone();
  }

  public
void Display()

  {
    Console.WriteLine( "RGB values are: {0},{1},{2}",
                                    red, green, blue );
  }
}

// Prototype manager


class
ColorManager

{
  // Fields
  Hashtable colors = new Hashtable();

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

///
<summary>

///  PrototypeApp test
/// </summary>
class PrototypeApp
{
  public static void Main( string[] args )
  {
     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 );

   
// User uses selected colors
    string colorName = "red";
    Color c1 = (Color)colormanager[ colorName ].Clone();
    c1.Display();

   
colorName = "peace";

    Color c2 = (Color)colormanager[ colorName ].Clone();
    c2.Display();

   
colorName = "flame";

    Color c3 = (Color)colormanager[ colorName ].Clone();
    c3.Display();
  }
}

posted on 2005-01-20 00:10  田政.NET  阅读(416)  评论(0)    收藏  举报

导航