Builder Pattern

16 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$69.99
List Price:  $99.99
You save:  $30
€66.45
List Price:  €94.93
You save:  €28.48
£55.32
List Price:  £79.03
You save:  £23.71
CA$98.04
List Price:  CA$140.06
You save:  CA$42.02
A$107.72
List Price:  A$153.89
You save:  A$46.17
S$94.09
List Price:  S$134.42
You save:  S$40.33
HK$544.68
List Price:  HK$778.15
You save:  HK$233.46
CHF 61.92
List Price:  CHF 88.46
You save:  CHF 26.54
NOK kr774.79
List Price:  NOK kr1,106.89
You save:  NOK kr332.10
DKK kr495.67
List Price:  DKK kr708.13
You save:  DKK kr212.46
NZ$118.82
List Price:  NZ$169.76
You save:  NZ$50.93
د.إ257.07
List Price:  د.إ367.26
You save:  د.إ110.19
৳8,339.40
List Price:  ৳11,913.94
You save:  ৳3,574.54
₹5,914.23
List Price:  ₹8,449.26
You save:  ₹2,535.03
RM311.24
List Price:  RM444.65
You save:  RM133.41
₦118,083.62
List Price:  ₦168,698.12
You save:  ₦50,614.50
₨19,391.36
List Price:  ₨27,703.14
You save:  ₨8,311.77
฿2,410.17
List Price:  ฿3,443.25
You save:  ฿1,033.08
₺2,423.71
List Price:  ₺3,462.60
You save:  ₺1,038.88
B$418.75
List Price:  B$598.25
You save:  B$179.49
R1,273.04
List Price:  R1,818.71
You save:  R545.66
Лв129.87
List Price:  Лв185.54
You save:  Лв55.66
₩97,732.12
List Price:  ₩139,623.30
You save:  ₩41,891.18
₪255.55
List Price:  ₪365.09
You save:  ₪109.53
₱4,110.61
List Price:  ₱5,872.56
You save:  ₱1,761.94
¥10,630.43
List Price:  ¥15,186.98
You save:  ¥4,556.55
MX$1,418.07
List Price:  MX$2,025.91
You save:  MX$607.83
QR254.36
List Price:  QR363.39
You save:  QR109.02
P953.37
List Price:  P1,362.02
You save:  P408.64
KSh9,063.70
List Price:  KSh12,948.70
You save:  KSh3,885
E£3,478.89
List Price:  E£4,970.06
You save:  E£1,491.16
ብር8,820.94
List Price:  ብር12,601.88
You save:  ብር3,780.94
Kz63,759.63
List Price:  Kz91,089.09
You save:  Kz27,329.46
CLP$68,404.72
List Price:  CLP$97,725.22
You save:  CLP$29,320.50
CN¥507.53
List Price:  CN¥725.08
You save:  CN¥217.54
RD$4,206.74
List Price:  RD$6,009.89
You save:  RD$1,803.14
DA9,351.13
List Price:  DA13,359.34
You save:  DA4,008.20
FJ$158.85
List Price:  FJ$226.94
You save:  FJ$68.09
Q538.41
List Price:  Q769.20
You save:  Q230.78
GY$14,600.65
List Price:  GY$20,858.96
You save:  GY$6,258.31
ISK kr9,615.92
List Price:  ISK kr13,737.62
You save:  ISK kr4,121.70
DH699.24
List Price:  DH998.96
You save:  DH299.72
L1,281.52
List Price:  L1,830.82
You save:  L549.30
ден4,084.06
List Price:  ден5,834.63
You save:  ден1,750.56
MOP$559.24
List Price:  MOP$798.95
You save:  MOP$239.71
N$1,266.15
List Price:  N$1,808.86
You save:  N$542.71
C$2,568.06
List Price:  C$3,668.81
You save:  C$1,100.75
रु9,427.87
List Price:  रु13,468.97
You save:  रु4,041.09
S/262.66
List Price:  S/375.25
You save:  S/112.58
K281.39
List Price:  K402.01
You save:  K120.61
SAR262.91
List Price:  SAR375.60
You save:  SAR112.69
ZK1,903.42
List Price:  ZK2,719.29
You save:  ZK815.86
L330.70
List Price:  L472.45
You save:  L141.75
Kč1,679.39
List Price:  Kč2,399.23
You save:  Kč719.84
Ft27,552.80
List Price:  Ft39,362.83
You save:  Ft11,810.03
SEK kr765.72
List Price:  SEK kr1,093.94
You save:  SEK kr328.21
ARS$70,549.59
List Price:  ARS$100,789.45
You save:  ARS$30,239.85
Bs482.26
List Price:  Bs688.98
You save:  Bs206.71
COP$307,907.55
List Price:  COP$439,886.78
You save:  COP$131,979.23
₡35,643.95
List Price:  ₡50,922.11
You save:  ₡15,278.16
L1,765.02
List Price:  L2,521.57
You save:  L756.54
₲545,488.80
List Price:  ₲779,303.12
You save:  ₲233,814.31
$U2,997.83
List Price:  $U4,282.79
You save:  $U1,284.96
zł286.31
List Price:  zł409.03
You save:  zł122.72
Already have an account? Log In

Transcript

So, the next creational pattern which we are going to learn right now is going to be builder pattern. This visitor pattern is also a creational pattern like a factory and abstract factory. And I suggested before we move forward to this particular pattern, I hope you guys have understood factory and abstract factory patterns and you have done the practical hands on also with that. Now, in this particular builder pattern, the first thing is like factory pattern was creating a simple object is the factory pattern is also creating object but this create a complex objects most of the time when we have a complex object, and if we want to create this complex object step by step, then builder pattern is useful. For example, if you are using a C sharp programming language, I hope you are familiar with string builder, which actually helps us to create a complex strings with that Same way in real life projects, sometimes we have some objects which are going to be complex and we want to build that thing with a step by step process.

At that time we use this design pattern. In my sample, I'm going to use again a KitKat manufacturing company and because so that I do not get confused with this, I have added one extra a in the name of this particular sample and this is still giftcard manufacturing company which builds a computer for us. But this time we are just assuming that we are going to manufacture different types of computers. Assume that we have normal computers as well as we are going to manufacture supercomputers also for our clients. Let's see how we can write a code with this. In the industry, you you can see that I have a one class library which is KitKat core.

And like always, we have one C sharp class inside that is a default class. First we are going to just delete this class and then Going to add our own classes inside this later on. And after this, I'm going to add one new project in the same solution. But like always we add a console application with this in this, we're not going to add a console application. inside of that, I'm going to add a new test project, there's going to be a unit test project. And this time, we will write some test cases using this project.

I'm going to give a name of this project something meaningful. So it's going to be kit card dot case, you can see that we have an instance of this particular class on the top. And if you check the method, we have some each method kind of method inside that and this method is the one where we write a test case will not write any code inside this right now. Right now we'll save this and we'll go to work in our code. And first, I'm going to right click on this class library and I'm going to add a new feature class in that. Let's say that we are adding a name of this class which is computer Inside this computer class, first, I'm going to make sure that this class is going to be public.

And inside this class, we are going to have some properties which we are going to use in our logic. You can see right now I have four properties. And it's something like when we build up a computer, we are going to provide these four things inside that the first one, which is the CPU frequency, where actually I am going to specify that when I build a computer, what will be the frequency of that particular CPU that the second property which we find there is going to be amount of cores. So when we have multiple cores in our computer, we are going to certify amount of course in that. The third one is Drive type, which is actually going to specify that whether I'm going to have a hard disk or we are going to have an SSD with that and same way the fourth one is amount of RAM, which is actually going to be indigent.

Once you're done with this computer class, we are going to add a new C sharp class inside this project. And let me give a name of the C sharp class supercomputers. Inside the supercomputer builder class, we are going to make sure that it's good to be public. And using this class now the first thing which I want to do is I want to use a computer class object. So we are going to create an object of the computer class and using that computer object class, we are going to assign some values to those properties. But we'll create some methods and inside that will assign that so you can see that in my code on the top we have one private read only instance variable of a computer.

And using this underscore computer instance we are assigning a values to our four properties, which we have created inside a computer class. The thing is, we are assigning the values into the properties using this methods like set core set CPU freak See fat Ram. And same way we have fed drive, right, which defines that we are going to have an SSD, which is a solid state drive hard drive inside that, at the end, we have one get computer method, which is actually going to return that computer instance, whenever I need that. This four methods are so simple, and it's just assigning the values to those properties. And all these things are inside my supercomputer builders love. Once you're done with these two classes.

I'm going to save this and I want to check this thing inside my test project. So first, I'm going to add a reference on my test project. And I'm going to add a reference of code. After adding this reference, I'm going to go to my unit test project. And I'm going to add one namespace, which is going to be my using it cart dot code. We know we can use our classes inside this widget now and inside the stitch method.

I'm going to write some simple statements first I want to create an object of my computer class, which is there. So I'm going to create an object of a computer class. And I'm going to assign some values into that. You can see I have predefined some values to my amount, of course, Ram, CPU frequency and drive, right. So this is going to be a basic computer using that object. And now I have created a new object of a supercomputer builder, the name of the object is builder.

And using this builder object, I'm calling those four methods. When we call the four methods, we know that it's actually going to assign the values to that. And at the end, we are calling get computer which is going to store that computer instance into this variable supercomputer. I just want to run a test case which can check an assert statement on this particular two objects. This other statement is going to check that these two objects are seen or not, and that's why it's going to check the values are equal or not. Once we have returns is false.

Statement, we are going to build a solution. And now it's time to run the test case. To do this, I'm going to go to my taste menu windows and paste Explorer, inside the PC floor panel, I'm going to just refresh it once and you can see that it's showing me test method one. To run this test method, I'm going to right click on this, and I'm going to click on Run select a test. When I do this, you can see this is showing me a green right I can in that, which actually shows me that my test case has successfully passed. And it shows me that these two are see if I try to change the value of the amount of course and then if I run this case, again, you can see that it's actually showing me that it is free now because this is not having your matching value.

I hope you understood this this case and things which are comparing with that. So now if I run again, is going to pass and it shows me that both of these objects are actually see right now if we check this code We have created a computer using a simple object and we have created with the help of a builder object. But when we do this kind of step by step process, it's not giving me any added advantage chances. Because logically this both look same. The only thing is in the first one, we are creating a computer using some properties while in the second one, we are creating a computer using this method called it's not giving me any extra advantage. And that's why if I want to use this properly, we have a builder pattern, which actually going to convert the super computer builder into an abstract class how to make this super computer builder abstract.

I'm going to right click on this project and I'm going to add a new C sharp class into this. This class name I'm giving is going to be computer wizard. This computer builder class is going to be As well as abstract, and this abstract class is going to have some logic like this, you can see that it's having the instance of the object on the top and inside this instance of the object the define that is going to be protected as well as read only. Then we have four methods and all the methods are abstract. And then we have one virtual method, which is get computer and which is going to return that computer. This abstract class is having very basic logical things inside that and we do not have any logic, we just have some abstract methods inside that.

Using this abstract class, we can write some concrete implementation of this. And that's why now we are going to go to our supercomputer builder and whatever is there inside this, I'm first going to select all this and I'm going to delete this. After this, I'm going to implement that computer with an abstract class into this. So I'm just adding computer builder and I'm going to Click on this quick actions. And using this I'm going to implement the abstract class. You can see I'm getting all four methods inside this.

And we are just going to override those four methods into this class. Inside these four methods, we have already logic like this. And you can see that I have some property values assigned into those properties. So we are giving the same values which were there in the computer builder, but the only thing is now we are not having the actual methods, we are just overriding the methods of an abstract class. And this is our concrete implementation. Now let's create one more concrete implementation.

So I'm adding a new C sharp class and assume that the name of the class is going to be normal computer builder. This one is going to be another concrete implementation of the abstraction. And that's why we are going to put columns and we are going to put computer vision and will again Implement the abstract class into this class. This one is going to be a different implementation of the same abstraction. So inside this four methods, we are assigning some more values. And you can see that this is a really a normal computer because it's having less amount, of course, only one.

It's having less CPU frequency, less amount of RAM, and a normal hard disk drive time. But this is really a normal computer. But the good thing is if you check our project, we have computer builder abstract class, using which we created normal computer data and supercomputer builders. And now is the time to use all of this data into our taste project. Once you're done with this, now it's time to add a new C sharp class into our project. And this C sharp class is going to be KitKat computer.

So this is going to be that class which is actually going to manufacture a computer for us These computers can be a nominal or supercomputers, I'm going to make this task public. And inside this computer store, we are going to write one method which is going to be build. This build method is actually returning a computer type of object. And you can see that it's having one parameter, which is actually computer builder, which is that abstract class. Using this abstract class parameter, we are going to call those four methods. And because this is an abstract class, we know that this abstract class is either going to connect with a normal computer builder, all supercomputer builder, any of the concrete implementation of that.

This build method is the actual method which is going to build a computer for me. And maybe at runtime we are going to return this computer with that. Let me click on Save all and now we are going to use normal computer builder and supercomputer builder In our case project inside the stage project, I'm not going to touch this case method one. In spite of this, I'm going to add few more methods into this. So I'm adding two new methods, the name of the method, supercomputer, which is again a paste method. And then we have inside that one computer object, which is creating a computer with some property values, and then we are creating an object of a computer store which is stored inside the store object.

And that is calling our build method, which is actually passing a builder object into that and you can see that this builder is actually an instance of our supercomputer builder class, which is one of the complete implementation of our abstraction. We are passing that builder into that and that is going to return a computer and you can see that we are not calling those methods which we have gone earlier into the above method. We are just simply calling One method which is well, and there is going to give me a computer. After this, we are just checking whether the computer which is created with the help of this concrete implementation is seen Mila is matching with the above one or not. Same way in the below section, we created one more method, which is a normal computer. And the normal computer is also having a builder object is having a builder object associated with that and we are just going to build the solution so that we can see that we have two test cases inside that and we are just going to run the cases.

And you can see both the test cases are passing which shows me that my both the computers are created with the similar kind of objects and they are matching. Once we are done with this, I hope you guys are getting this cool that we created two different computer types. That we have actually created two different computers using our render object and this is something Which a builder pattern helps me to create a complex objects creating step by step. Now let's see the structure of our project. And you can see that in this structure, we have our main abstract class which is computer builder, which is connected with our two concrete implementations, normal computer builder, and supercomputer builder. And we are just using this to complete implementations into over KitKat computer store.

Computer store is having a method belt, which is actually going to take a parameter of a computer builder and it is going to return a computer that fi the computer class is also associated with KitKat computers too, because they are using abstraction into that you can see that the connection between computer builder and KitKat computer store is dotted because that is not a data connection that is used as a parameter. I hope you understood the structure and builder pattern. Thank you

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.