Prototype Pattern Code Review
Use the Prototype Pattern
Code Review
- Prototype Pattern Code Review: Define the Prototype Interface and Handler Objects
- Prototype Pattern Code Review: Define the PrototypeManager Object
- Prototype Pattern Code Review: Use the Prototype Pattern
Code Walkthrough
The Program class creates an instance of the PrototypeManager class, which creates different prototype instances. These prototypes are then cloned using shallow copy and deep copy methods. The outputs from the difference methods are displayed for comparision.
class Program
{
static void Report(String s, ProtoType a, ProtoType b)
{
Console.WriteLine(String.Format("\n{0}", s));
Console.WriteLine(String.Format("Prototype\t{0}\nClone\t\t{1}", a, b));
}
static void Main(string[] args) { PrototypeManager manager = new PrototypeManager(); ProtoType c2; ProtoType c3;
c2 = manager.prototypes["Australia"].Clone(); Report("Shallow cloning Australia\n===============", manager.prototypes["Australia"], c2);
c2.Capital = "Sydney"; Report("Altered Clone's shallow state, prototype unaffected", manager.prototypes["Australia"], c2);
c2.Language.Data = "Chinese"; Report("Altering Clone deep state: prototype affected *****", manager.prototypes["Australia"], c2);
c3 = manager.prototypes["Germany"].DeepCopy( ); Report("Deep cloning Germany\n============", manager.prototypes["Germany"], c3);
c3.Capital = "Munich"; Report("Altering Clone shallow state, prototype unaffected", manager.prototypes["Germany"], c3);
c3.Language.Data = "Turkish"; Report("Altering Clone deep state, prototype unaffected", manager.prototypes["Germany"], c3);
Console.Read(); } }
static void Main(string[] args) { PrototypeManager manager = new PrototypeManager(); ProtoType c2; ProtoType c3;
c2 = manager.prototypes["Australia"].Clone(); Report("Shallow cloning Australia\n===============", manager.prototypes["Australia"], c2);
c2.Capital = "Sydney"; Report("Altered Clone's shallow state, prototype unaffected", manager.prototypes["Australia"], c2);
c2.Language.Data = "Chinese"; Report("Altering Clone deep state: prototype affected *****", manager.prototypes["Australia"], c2);
c3 = manager.prototypes["Germany"].DeepCopy( ); Report("Deep cloning Germany\n============", manager.prototypes["Germany"], c3);
c3.Capital = "Munich"; Report("Altering Clone shallow state, prototype unaffected", manager.prototypes["Germany"], c3);
c3.Language.Data = "Turkish"; Report("Altering Clone deep state, prototype unaffected", manager.prototypes["Germany"], c3);
Console.Read(); } }
Printer Friendly Version
Add to Favourites
DotNet Kicks
Digg
del.icio.us
Live Favourites
ma.gnolia
reddit
Slashdot
Technorati
Yahoo!