Software Development Tricky

Tuesday 26 September 2017

Prototype Pattern

  • The Prototype Pattern is a Creational Design Pattern, It’s used to Clone / Duplicate the object.
  • It’s allows us to hide the complexity of making new instances from the client. The concept is to copy an existing object rather than creating a new instance from scratch. The existing object act as a prototype. The newly copied object may change same properties only if required. This approach saves costly resources and time and resolved problem related with duplicating a class.

    Example:

    Consider you are creating mobile product management application. The client want to ask same mobile product with different features. In this scenario, we copy new product from existing product just by changing properties based on client expectation.

Sample Demo Diagram:


UML Diagram:



Drawbacks:

·         To making the copy of an object that can be complicated sometimes.
·         Classes that have circular references to other classes cannot really be cloned.

Sample Demo Program:

The following participants are involved in given program

1.       Prototype – It’s an interface which is used for the types of object that can be cloned itself.
2.       ConcretePrototype – it’s a class which implements the prototype interface for cloning itself.
3.       Client – creates a new object by asking a prototype to clone itself.

Entire Code Structure:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
using System;
namespace PrototypePattern
{
    class Program
    {

        //'IPrototype' Interface
        interface IMobilePhone
        {
            IMobilePhone Clone();
            string GetMobilePhoneDetails();
        }

        //'ConcretePrototype' class
        class MobilePhone : IMobilePhone
        {
            public string Name { get; set; }
            public string OperatingSystem { get; set; }
            public string InternalMemory { get; set; }
            public string RearCamera { get; set; }
            public string ScreenSize { get; set; }
            public string ExpandableMemory { get; set; }
            public string BatteryCapacity { get; set; }
            public string Resolution { get; set; }
            public string Colour { get; set; }

            public IMobilePhone Clone()
            {
                //Shallow Copy: every reference type objects are duplicated
                return (IMobilePhone)MemberwiseClone();
            }

            public string GetMobilePhoneDetails()
            {
                return string.Format("Name : {0}\nOpeartingSystem : {1}\nInternal Memory : {2}\nRear Camera : {3}\n"
                    + "ScreenSize : {4}\nExpandableMemory : {5}\nBatteryCapacity : {6}\n"
                    + "Resolution : {7}\nColour : {8}", Name, OperatingSystem, InternalMemory, RearCamera, ScreenSize,
                    ExpandableMemory, BatteryCapacity, Resolution, Colour);
            }

        }


        //'Client'
        static void Main(string[] args)
        {
            MobilePhone mobilePhone = new MobilePhone();
            mobilePhone.OperatingSystem = " Nokia OS Series 30";
            mobilePhone.InternalMemory = "250 MB";
            mobilePhone.ExpandableMemory = "32 GB";

            //Clone mobile phone object with Clone method
            
            //Clone Nokia 215 Mobile Phone Object with help of default value from Original object
            MobilePhone mobilePhone_Nokia215 = (MobilePhone)mobilePhone.Clone();
            mobilePhone_Nokia215.Name = "Nokia 215";
            mobilePhone_Nokia215.RearCamera = "0.3 MP";
            mobilePhone_Nokia215.ScreenSize = "2.4 Inches";
            mobilePhone_Nokia215.BatteryCapacity = "1100 mAh";
            mobilePhone_Nokia215.Resolution = "240 x 320 pixels";
            mobilePhone_Nokia215.Colour = "Green";

            Console.WriteLine("------------------Nokia 215 Mobile Phone Details-------------------");
            Console.WriteLine(mobilePhone_Nokia215.GetMobilePhoneDetails());

            //Clone Nokia 130 Dual SIM Mobile Phone Object with help of default value from Original object
            MobilePhone mobilePhone_Nokia130DualSim = (MobilePhone)mobilePhone.Clone();
            mobilePhone_Nokia130DualSim.Name = "Nokia 130 Dual SIM";
            mobilePhone_Nokia130DualSim.RearCamera = "No";
            mobilePhone_Nokia130DualSim.ScreenSize = "1.8 Inches";
            mobilePhone_Nokia130DualSim.BatteryCapacity = "1200 mAh";
            mobilePhone_Nokia130DualSim.Resolution = "128 x 160 pixels";
            mobilePhone_Nokia130DualSim.Colour = "Red";

            Console.WriteLine("------------------Nokia 130 Dual SIM Mobile Phone Details-------------------");
            Console.WriteLine(mobilePhone_Nokia130DualSim.GetMobilePhoneDetails());

            //Clone Nokia 130 Mobile Phone Object with help of default value from Original object
            MobilePhone mobilePhone_Nokia130 = (MobilePhone)mobilePhone.Clone();
            mobilePhone_Nokia130.Name = "Nokia 130";
            mobilePhone_Nokia130.RearCamera = "No";
            mobilePhone_Nokia130.ScreenSize = "1.8 Inches";
            mobilePhone_Nokia130.BatteryCapacity = "1020 mAh";
            mobilePhone_Nokia130.Resolution = "128 x 160 pixels";
            mobilePhone_Nokia130.Colour = "Red";

            Console.WriteLine("------------------Nokia 130 Mobile Phone Details-------------------");
            Console.WriteLine(mobilePhone_Nokia130.GetMobilePhoneDetails());


            Console.Write("Press any key to exist...");
            Console.ReadKey();


        }
    }
}

Final Output:


Click here to download sample program

If you like this post. Kindly share your valuable comments.

No comments:

Post a Comment

Followers