GOF pattern - Prototype

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

Frequency of use: use_medium_low.gif medium low
UML class diagram
prototype.gif
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
Sample code
This structural code demonstrates the Prototype pattern in which new objects are created by copying pre-existing objects (prototypes) of the same class.
// Prototype pattern -- Structural example

//--------------------------------------------------------
// Copyright (C) 2001 - 2002, Data & Object Factory
// All rights reserved. www.dofactory.com
//
// You are free to use this source code in your 
// applications as long as the original copyright
// notice is included.
//
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT 
// WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, 
// INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF 
// MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
//--------------------------------------------------------

using System;

// "Prototype" 
abstract class Prototype {
  
// Fields
  private string id;

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

  
public string Id {
    
getreturn 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.
// Prototype pattern -- Real World example

//--------------------------------------------------------
// Copyright (C) 2001 - 2002, Data & Object Factory
// All rights reserved. www.dofactory.com
//
// You are free to use this source code in your 
// applications as long as the original copyright
// notice is included.
//
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT 
// WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, 
// INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF 
// MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
//--------------------------------------------------------

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 thisstring name ] {
    
getreturn (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( 25500 );
    colormanager[ 
"green" ] = new Color( 02550 );
    colormanager[ 
"blue" ] = new Color( 00255 );

    
// User adds personalized colors
    colormanager[ "angry" ] = new Color( 255540 );
    colormanager[ 
"peace" ] = new Color( 128211128 );
    colormanager[ 
"flame" ] = new Color( 2113420 );

    
// 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();

    Console.Read();
  }
}

posted on 2005-07-07 08:19 毒菇求Buy 阅读(220) 评论(0)  编辑 收藏 引用 所属分类: Design PatternC#

只有注册用户登录后才能发表评论。
<2024年3月>
252627282912
3456789
10111213141516
17181920212223
24252627282930
31123456

导航

统计

常用链接

留言簿(7)

随笔分类(133)

随笔档案(111)

文章分类(65)

文章档案(53)

相册

收藏夹(30)

BLOG

Book store

Graphics Design

搜索

最新评论

阅读排行榜

评论排行榜