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 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
Structural 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.
1.
2.
3. using System;
4.
5. namespace DoFactory.GangOfFour.Prototype.Structural
6. {
7. /// <summary>
8. /// MainApp startup class for Structural
9. /// Prototype Design Pattern.
10. /// </summary>
11. class MainApp
12. {
13. /// <summary>
14. /// Entry point into console application.
15. /// </summary>
16. static void Main()
17. {
18. // Create two instances and clone each
19.
20. ConcretePrototype1 p1 = new ConcretePrototype1("I");
21. ConcretePrototype1 c1 = (ConcretePrototype1)p1.Clone();
22. Console.WriteLine("Cloned: {0}", c1.Id);
23.
24. ConcretePrototype2 p2 = new ConcretePrototype2("II");
25. ConcretePrototype2 c2 = (ConcretePrototype2)p2.Clone();
26. Console.WriteLine("Cloned: {0}", c2.Id);
27.
28. // Wait for user
29. Console.ReadKey();
30. }
31. }
32.
33. /// <summary>
34. /// The 'Prototype' abstract class
35. /// </summary>
36. abstract class Prototype
37. {
38. private string _id;
39.
40. // Constructor
41. public Prototype(string id)
42. {
43. this._id = id;
44. }
45.
46. // Gets id
47. public string Id
48. {
49. get { return _id; }
50. }
51.
52. public abstract Prototype Clone();
53. }
54.
55. /// <summary>
56. /// A 'ConcretePrototype' class
57. /// </summary>
58. class ConcretePrototype1 : Prototype
59. {
60. // Constructor
61. public ConcretePrototype1(string id)
62. : base(id)
63. {
64. }
65.
66. // Returns a shallow copy
67. public override Prototype Clone()
68. {
69. return (Prototype)this.MemberwiseClone();
70. }
71. }
72.
73. /// <summary>
74. /// A 'ConcretePrototype' class
75. /// </summary>
76. class ConcretePrototype2 : Prototype
77. {
78. // Constructor
79. public ConcretePrototype2(string id)
80. : base(id)
81. {
82. }
83.
84. // Returns a shallow copy
85. public override Prototype Clone()
86. {
87. return (Prototype)this.MemberwiseClone();
88. }
89. }
90. }
91.
92.
93.
Output
Cloned: I
Cloned: II
Cloned: II
Real-world code in
C#
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.
1.
2.
3. using System;
4. using System.Collections.Generic;
5.
6. namespace DoFactory.GangOfFour.Prototype.RealWorld
7. {
8. /// <summary>
9. /// MainApp startup class for Real-World
10. /// Prototype Design Pattern.
11. /// </summary>
12. class MainApp
13. {
14. /// <summary>
15. /// Entry point into console application.
16. /// </summary>
17. static void Main()
18. {
19. ColorManager colormanager = new ColorManager();
20.
21. // Initialize with standard colors
22. colormanager["red"] = new Color(255, 0, 0);
23. colormanager["green"] = new Color(0, 255, 0);
24. colormanager["blue"] = new Color(0, 0, 255);
25.
26. // User adds personalized colors
27. colormanager["angry"] = new Color(255, 54, 0);
28. colormanager["peace"] = new Color(128, 211, 128);
29. colormanager["flame"] = new Color(211, 34, 20);
30.
31. // User clones selected colors
32. Color color1 = colormanager["red"].Clone() as Color;
33. Color color2 = colormanager["peace"].Clone() as Color;
34. Color color3 = colormanager["flame"].Clone() as Color;
35.
36. // Wait for user
37. Console.ReadKey();
38. }
39. }
40.
41. /// <summary>
42. /// The 'Prototype' abstract class
43. /// </summary>
44. abstract class ColorPrototype
45. {
46. public abstract ColorPrototype Clone();
47. }
48.
49. /// <summary>
50. /// The 'ConcretePrototype' class
51. /// </summary>
52. class Color : ColorPrototype
53. {
54. private int _red;
55. private int _green;
56. private int _blue;
57.
58. // Constructor
59. public Color(int red, int green, int blue)
60. {
61. this._red = red;
62. this._green = green;
63. this._blue = blue;
64. }
65.
66. // Create a shallow copy
67. public override ColorPrototype Clone()
68. {
69. Console.WriteLine(
70. "Cloning color RGB: {0,3},{1,3},{2,3}",
71. _red, _green, _blue);
72.
73. return this.MemberwiseClone() as ColorPrototype;
74. }
75. }
76.
77. /// <summary>
78. /// Prototype manager
79. /// </summary>
80. class ColorManager
81. {
82. private Dictionary<string, ColorPrototype> _colors =
83. new Dictionary<string, ColorPrototype>();
84.
85. // Indexer
86. public ColorPrototype this[string key]
87. {
88. get { return _colors[key]; }
89. set { _colors.Add(key, value); }
90. }
91. }
92. }
93.
94.
95.
Output
Cloning color RGB: 255, 0, 0
Cloning color RGB: 128,211,128
Cloning color RGB: 211, 34, 20
Cloning color RGB: 128,211,128
Cloning color RGB: 211, 34, 20

No comments:
Post a Comment