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