Software Development Tricky

Monday, 25 September 2017

Builder Pattern

  •  The Builder Design Pattern is a Creational Design Pattern, It’s build a complex object by  using step by step approach and finally step will return the object. The same build process   can create different object.
  •  Builder is independent from the objects creation process.

    Example:

          Consider you are creating mobile product management application. The client want to ask different kind of mobile products like normal & smart phone. First you can construct mobile phone. Mobile phone is the final end product (object). It build different mobile product like Normal & Smart Phone based on many steps like Operating System Installation, Internal Memory Allocation, Allocate Connectivity, Allocate Screen Type and so on…..Finally the whole mobile phone object is returned.

    Sample Demo Diagram:


    UML Diagram:



    Drawbacks:

    ·         It’s requires code duplication as builder need to copy all fields or properties from original
    ·         Requires creating a separate ConcreteBuilder for each different type of product.
    ·         Requires the builder classes to be mutable.

    Sample Demo Program:

    The following participants are involved in given program

    1.       Builder – It’s an interface which is used to declare all the steps to create a product
    2.       ConcreteBuilder – It’s a class which implements or define the Builder interface to create  complex kind of product
    3.       Product – It’s main object that will be constructed
    4.       Director – It’s used to construct an object using the Builder Interface.

    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
     98
     99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    using System;
    namespace BuilderPattern
    {
        class Program
        {
            //Identifying various parts with help of enums helper
            public enum OperatingSystem
            {
                Android,
                Symbian_OS,
                Windows_Phone,
                Windows_Mobile,
                Nokia_Asha,
                Aliyun_OS
            }
            public enum Battery
            {
                mAH_2500,
                mAH_4000,
                mAH_4500,
                mAH_5000
            }
            public enum InternalMemory
            {
                IM_250_MB, //Internal Memory 250 MB
                IM_4_GB,
                IM_8_GB,
                IM_16_GB
            }
            public enum Weight
            {
                W_100_g, //Weight 100 grams
                W_146_g,
                W_200_g,
                W_250_g
            }
            public enum WirelessCommunication
            {
                Bluetooth,
                WiFi_Hotspot
            }
            public enum Connectivity
            {
                GSM,
                CDMA,
                LTE,
                TDD
            }
            public enum ScreenType
            {
                Touch,
                Non_Touch
            }
            public enum Colour
            {
                Black,
                Green,
                Yellow,
                White,
                Gold
            }
    
            //'Product' Class
            class MobilePhone
            {
                public string ModelNumber { get; set; }
                public string PhoneName { get; set; }
                public OperatingSystem OperatingSystem { get; set; }
                public InternalMemory InternalMemory { get; set; }
                public Weight Weight { get; set; }
                public Battery Battery { get; set; }
                public string WirelessCommunication { get; set; }
                public string Connectivity { get; set; }
                public string Camera { get; set; }
                public Colour Colour { get; set; }
                public ScreenType ScreenType { get; set; }
    
                public void DisplayPhoneDetails()
                {
                    Console.WriteLine(string.Format("Model Number : {0}\nPhone Name : {1}\nOperating System : {2}\n"
                        + "Internal Memory : {3}\nWeight : {4}\nBattery : {5}\nWireless Communication : {6}\n"
                        + "Connectivity : {7}\nCamera : {8}\nColour : {9}\nScreen Type : {10}", ModelNumber, PhoneName, OperatingSystem,
                        InternalMemory, Weight, Battery, WirelessCommunication, Connectivity, Camera, Colour, ScreenType));
    
                }
            }
    
            //'Builder' Interface
            interface IPhoneBuilder
            {
                void AddOS();
                void AddInternalMemory();
                void AddWeight();
                void AddBattery();
                void AddWirelessCommunication();
                void AddConnectivity();
                void AddCamera();
                void AddColour();
                void AddScreenType();
    
                MobilePhone GetPhone();
            }
    
            //'ConcreteBuilder' class - Implements builder interface
            class NormalPhoneBuilder : IPhoneBuilder
            {
                MobilePhone Phone = new MobilePhone();
    
                public NormalPhoneBuilder(string modelNumber, string phoneName)
                {
                    Phone.ModelNumber = modelNumber;
                    Phone.PhoneName = phoneName;
                }
    
                public void AddOS()
                {
                    Phone.OperatingSystem = OperatingSystem.Symbian_OS;
                }
    
                public void AddInternalMemory()
                {
                    Phone.InternalMemory = InternalMemory.IM_250_MB;
                }
    
                public void AddWeight()
                {
                    Phone.Weight = Weight.W_100_g;
                }
    
                public void AddBattery()
                {
                    Phone.Battery = Battery.mAH_2500;
                }
    
                public void AddWirelessCommunication()
                {
                    Phone.WirelessCommunication = WirelessCommunication.Bluetooth.ToString();
                }
    
                public void AddConnectivity()
                {
                    Phone.Connectivity = Connectivity.CDMA.ToString();
                }
    
                public void AddCamera()
                {
                    Phone.Camera = "Not Available";
                }
    
                public void AddColour()
                {
                    Phone.Colour = Colour.Black;
                }
    
                public void AddScreenType()
                {
                    Phone.ScreenType = ScreenType.Non_Touch;
                }
    
                public MobilePhone GetPhone()
                {
                    return Phone;
                }
            }
    
            //'ConcreteBuilder' class - Implements builder interface
            class SmartPhoneBuilder : IPhoneBuilder
            {
                MobilePhone Phone = new MobilePhone();
    
                public SmartPhoneBuilder(string modelNumber, string phoneName)
                {
                    Phone.ModelNumber = modelNumber;
                    Phone.PhoneName = phoneName;
                }
    
                public void AddOS()
                {
                    Phone.OperatingSystem = OperatingSystem.Windows_Mobile;
                }
    
                public void AddInternalMemory()
                {
                    Phone.InternalMemory = InternalMemory.IM_8_GB;
                }
    
                public void AddWeight()
                {
                    Phone.Weight = Weight.W_200_g;
                }
    
                public void AddBattery()
                {
                    Phone.Battery = Battery.mAH_4500;
                }
    
                public void AddWirelessCommunication()
                {
                    Phone.WirelessCommunication = string.Format("{0},{1}", WirelessCommunication.Bluetooth.ToString(), WirelessCommunication.WiFi_Hotspot.ToString());
                }
    
                public void AddConnectivity()
                {
                    Phone.Connectivity = string.Format("{0},{1},{2}",Connectivity.CDMA.ToString(), Connectivity.GSM.ToString(), Connectivity.LTE.ToString());
                }
    
                public void AddCamera()
                {
                    Phone.Camera = "13 MP"; //13 Mega Pixcel
                }
    
                public void AddColour()
                {
                    Phone.Colour = Colour.Gold;
                }
    
                public void AddScreenType()
                {
                    Phone.ScreenType = ScreenType.Touch;
                }
    
                public MobilePhone GetPhone()
                {
                    return Phone;
                }
            }
    
            //'Director' class - To constract mobile phone
            class MobilePhoneManufacturer
            {
                public void BuildMobilePhone(IPhoneBuilder phoneBuilder)
                {
                    phoneBuilder.AddOS();
                    phoneBuilder.AddInternalMemory();
                    phoneBuilder.AddWeight();
                    phoneBuilder.AddBattery();
                    phoneBuilder.AddWirelessCommunication();
                    phoneBuilder.AddConnectivity();
                    phoneBuilder.AddConnectivity();
                    phoneBuilder.AddCamera();
                    phoneBuilder.AddColour();
                    phoneBuilder.AddScreenType();
                }
            }
    
            static void Main(string[] args)
            {
                //Create 'Director'
                MobilePhoneManufacturer mobilePhoneManufacturer = new MobilePhoneManufacturer();
    
                //Build Normal Mobile Phone
                IPhoneBuilder normalPhoneBuilder = new NormalPhoneBuilder("Normal_001", "Nokia 1600");
                mobilePhoneManufacturer.BuildMobilePhone(normalPhoneBuilder);
                MobilePhone NormalMobilePhone = normalPhoneBuilder.GetPhone();
    
                //Display Details
                Console.WriteLine("----------------------Normal Mobile Phone Details-----------------");
                NormalMobilePhone.DisplayPhoneDetails();
    
                //Build Smart Phone
                Console.WriteLine("\n----------------------Smart Phone Details-----------------");
                IPhoneBuilder smartPhoneBuilder = new SmartPhoneBuilder("SmartPhone_001", "Nokia Asha");
                mobilePhoneManufacturer.BuildMobilePhone(smartPhoneBuilder);
                MobilePhone SmartPhone = smartPhoneBuilder.GetPhone();
    
                //Display Details
                SmartPhone.DisplayPhoneDetails();
    
                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