The first design pattern, which we are going to learn in this video is a factory pattern. As I said earlier, this is one of the most popular pattern in all the design patterns. Now, if you want to understand factory pattern, then first let's say that in real life when we say born factory is actually the place where we produce things. Most of the time, the manufacturing factories are going to produce some products. And usually, when they draw you something, it's not going to be one single product is going to be a family of things. It's something like that.
Suppose if I have a manufacturing company who's actually producing a stationery, it's like pen, charts, dusters, and whiteboards. Then it's something like this that they are not producing one particular Product they're producing family of products and all these products belongs to the one particular family which is stationary. In very similar way in a programming language C sharp, a factory something which creates a family of objects. And we have a factory of classes and factory of methods in it. Now, we already know this thing that a gang of four has divided design patterns into three different categories. And factory pattern is actually somewhere coming under the creational pattern.
It means this is the design pattern which is dealing with the object creation. And that's where if you see the definition of this is showing that the factory is going to be something which creates a family of objects. And the way we create this family of object using this pattern is what we need to understand. Now to understand this design pattern properly, I want to take one real life scenario kind of thing. Assume that we have a menu factoring company whose name is Kitt car. And this company wants you to write a software for them.
You are a software developer and you are going to build a software, which is going to be used by this manufacturing company and what they produce, they produce computers for their clients. Now obviously, when they produce computers in my scenario right now, I'm assuming that they are going to have multiple clients. So it's not going to be one client is going to be more than one we're going to order on we're going to purchase a computers from this manufacturing company. In this scenario as a case study, we are going to create a project sample right now, as this is my first example in design patterns. I'm going to keep this thing as simple as possible. But the structure of my project is going to be very strict and it's going to be perfectly following my factory design pattern.
So let's start doing something in Visual Studio throughout this video series, I'm going to use my Visual Studio 2015. But let me tell you, if you have with vSphere 2010 1230, or with the studio 2015, any version, you can still do the same kind of hands on with that. Because design patterns are theme from couple of years. I have the history of 2015 installed in my laptop, so I'm going to use that one only. Now the first thing which I'm going to do is I'm going to create a new project. And this new project is going to be my console application.
I'm giving a name of the console application just for easiness like it caught. The this gift card application, I'm going to click on OK is going to be C sharp application based on dotnet. framework 4.5. We are good to go. And I'm going to click on OK. The moment this console application is created, you can see in the Solution Explorer, that the project name is sketch card and the solution name is also critical.
Now, I'm going to right click on my solution and I'm going to add One more new project inside the same solution. In my case, I always write mine, a structured core of the design pattern in my class library. So this is going to be my class library, which I'm going to use for my factory design pattern. I'm getting the name of this class library, hit card, dot, dot, dot, this is going to have my core functionality of my design pattern. And the console application is just an outer layer which is going to be used for to run my sample. Now we know that once we are selecting a class library, this is going to be nothing but DLL.
So in future if I want to use this DLL in couple of projects, I can use my core logic in multiple applications. First, we have a by default class one.cs. I'm going to right click on this class when I am going to delete this. I don't want this I'm going to click on OK. And now my class library is really simple. For the next couple of steps, I'm going to do everything inside my DLL which is my class library will not touch this console application till we are done with this. Now, as we know that we are going to use interfaces and abstract classes in multiple design patterns in this design pattern which is a factory pattern I want to start with interface.
So, I'm going to right click on my credit card cool, and I'm going to add one new item. This new item is going to be one interface and I'm giving a name of this interface I factory. I'm gonna click on Add and you can see my factory interface as always when we have our interface is going to have some signature of the method. Inside this interface, I'm creating a very simple method. The name of the method is create computer. This create computer method is going to be there inside my interface but we are not To write logic inside this, it goes interfaces are just going to have a signature of the method not logic.
After this now I'm assuming that this interface is going to be used by more than one classes. So I'm going to add a new C sharp class with the name client a factory. Inside the client, a factory teach AP class we are going to implement our interface which is a factory. And once we implement this factory interface, we are going to do go to quick actions. And inside this we have implement interface. Once you do this, you can see that I'm getting my method which is create computer.
And this was the same method which was there inside I factory, we implemented the interface and we got that method inside this. Inside this method, we have to write a logic and my logic is really simple right now, that night a computer is created. This is what I'm printing in my console. Same way, let's add one more C sharp class because as we discussed, we are going to have more than one clients inside this. So I'm going to add one more feature class with the main client, the factory. And then inside this, we are going to save the implementing that I factory.
Quick actions implement interface. And inside this client create computer method, we are going to certify that it's going to be a client, the computer is created. Up to this point, nothing fancy. They just have two simple C sharp classes which are associated with one interface. I just want to do one additional change my client, a factory and client B factory, go to classes, access modifier. I'm going to keep public because we know that this is a class library and I'm going to use this class it's under in my console application.
So right now the roster is done. We have one factory which is interesting. And it gets implemented inside two different C sharp classes. And this interface is having one simple method, which is just returning nothing. That's why it's having void. Now once you're done with this two things, let's add one more C sharp class.
The name of the C sharp class, I'm going to give credit card manufacturing company. Inside this kickback manufacturing company class, the first thing which I want to do is I want to make this class abstract from the Define public abstract. And now because this class is after class, we are going to specify some logic inside this. And obviously there is going to be inside an abstract method so that we can override that thing in future. I'm having a simple abstract method. If you see this get computer is a method, which is having a return type II factory.
Right now if I move my mouse on this, it's showing me that this particular factory should be actually less accessible, it means somewhere we have to deal with the access modifier folders, I'm going to my eye factory, and I'm going to make sure that this interface is going to be public. Once I do this, and if I come back to this, you can see that now this get computer method is perfectly fine because this is an abstract class, and inside this we have an abstract method. Now it's time to deal with the concrete implementation of this class. So we're going to right click on this code project and we're going to add a new C sharp class I'm giving a name of the C sharp class concrete kid thought. So, this is going to be my concrete cap C sharp class and this concrete is not equal to obviously implement my card manufacturing company which is my abstract class.
Once I do this, it can override the methods which are there we are just going to 55 override and we should have on get computer method inside Inside this computer method, now we have to write our logic. And for this now we have to write some logic, which is going to be based on this client name parameter. So my simple logic looks like this. Like you can see I have a switch case, which is going to be walk on based on this client name parameter. And if my client name is a, then we are going to return a client a factory. And if it is B, we are going to return client D factory.
If user is going to pass some nonsense things, then we are going to just throw an exception like this. Now, we are pretty much done with everything inside my code project. Now it's the time to see this thing working in our console application. Before I go there, I just want to make sure that this class is also going to be public. So once I certify this, I'm going back to my console application. And inside this we already know we have a program class with a main method, which is going to be the starting point of this console application.
Inside this program class, we're going to write some logic. But before we do any logic, we want To use this DLL KitKat core into our KitKat project. So we're going to right click on this, and we are going to add reference in which I'm going to select that from this project, I want to refer my KitKat code inside this. Once we click OK, you can see that the DLL is referred inside this. And we all have to do is and now all we need to do is we have to add an instance of that fantasy fine using Picard dot code. This means it is done, we have access to all the classes which we have created inside the school library.
And that's why I'm going to write my simple logic like this. You can see that I am creating an object of my concrete kid card. But while creating an instance of a concrete kid card, the factory object is actually referring a type of my abstract class. After this, we have two objects below that if you see the have declined a computer and client B computer. Both are of type interface I factory. And now we're going to use this interface type variable client, a computer and client v computer to create an instance of a particular computer class.
I'm using my factory instance. And I'm calling my get computer method, which is actually going to come from this card manufacturing company. And inside this computer, the implementation is going to come from concrete ID card. Now because I'm passing these two parameters in my console application, if I'm passing a, this will go to case a and then it's going to create an instance of a client a factory, otherwise is going to create an instance of a client v factory. When I do this thing is going to call the connected classes and based on that is should give me the output. Let me run the sample to check whether what I'm getting You can see I'm getting pulled the console writeline which we have certified in two different computer classes, client a computer is created and client B computer is created.
This is a simplest example of a factory because right now we have one interface with the name AI factory, which is actually used inside two different classes, which are client a computer and client B computer. Now, let's see how these classes are connected with each other. As we have followed all the steps, I hope you remember that the first step which we have done is we created an interface with the name a factory, that interface was implemented in two different C sharp classes client a factory and client B factory. Now, once this connection this part is done, we have actually created one abstract class and that abstract class was kicked at manufacturing company. This abstract class was having a concrete implementation of Back, which was concrete KitKat. And finally, we have done a connection between this interface and this concrete card by actually calling a method of a concrete get card using the reference type variable of a factory.
As you can see, in my case, the connection between this interface and these two classes is data connection, because it's a real implementation of that same way, this connection between the abstract class and the concrete class is also a direct connection because it's directly connected with each other. But this connection which I'm doing between a factory and concrete good thought is dotted connection, because this is not something which is a direct instance of direct connection like that. If you remember the core which we have done in between this factory and concrete cart, then this is this connection. You can see that we are inside a concrete cart and inside a concrete type C sharp class, we have a method which is actually returning of type either This is that dotted connection which I'm talking about. And that fi This is not a direct connection. It's something like we have a class inside which we have a method.
And that method is returning something which is of type I factory interface. I hope you're clear with the structure. And this is something which is a very basic factory implementation. We have tried to reduce our code as much as possible, but structure right if you see this is a true factory pattern. Thank you.