Factory Pattern

15 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
€64.99
List Price:  €92.85
You save:  €27.85
£55.77
List Price:  £79.68
You save:  £23.90
CA$95.68
List Price:  CA$136.70
You save:  CA$41.01
A$105.96
List Price:  A$151.38
You save:  A$45.42
S$94.48
List Price:  S$134.98
You save:  S$40.50
HK$546.78
List Price:  HK$781.15
You save:  HK$234.36
CHF 63.33
List Price:  CHF 90.48
You save:  CHF 27.14
NOK kr761.11
List Price:  NOK kr1,087.35
You save:  NOK kr326.23
DKK kr485.02
List Price:  DKK kr692.92
You save:  DKK kr207.89
NZ$116.44
List Price:  NZ$166.35
You save:  NZ$49.91
د.إ257.06
List Price:  د.إ367.25
You save:  د.إ110.18
৳7,660.01
List Price:  ৳10,943.35
You save:  ৳3,283.33
₹5,835.78
List Price:  ₹8,337.18
You save:  ₹2,501.40
RM331.75
List Price:  RM473.95
You save:  RM142.20
₦86,437.65
List Price:  ₦123,487.65
You save:  ₦37,050
₨19,492.21
List Price:  ₨27,847.21
You save:  ₨8,355
฿2,572.74
List Price:  ฿3,675.50
You save:  ฿1,102.76
₺2,264.43
List Price:  ₺3,235.04
You save:  ₺970.61
B$355.02
List Price:  B$507.19
You save:  B$152.17
R1,295.44
List Price:  R1,850.71
You save:  R555.27
Лв127.05
List Price:  Лв181.51
You save:  Лв54.46
₩94,909.58
List Price:  ₩135,590.93
You save:  ₩40,681.35
₪260.15
List Price:  ₪371.66
You save:  ₪111.51
₱3,993.87
List Price:  ₱5,705.78
You save:  ₱1,711.90
¥10,713.01
List Price:  ¥15,304.96
You save:  ¥4,591.95
MX$1,187.73
List Price:  MX$1,696.83
You save:  MX$509.10
QR254.83
List Price:  QR364.06
You save:  QR109.23
P950.82
List Price:  P1,358.38
You save:  P407.55
KSh9,247.76
List Price:  KSh13,211.65
You save:  KSh3,963.89
E£3,352.12
List Price:  E£4,788.95
You save:  E£1,436.83
ብር4,006.43
List Price:  ብር5,723.72
You save:  ብር1,717.28
Kz58,511.64
List Price:  Kz83,591.64
You save:  Kz25,080
CLP$65,950.47
List Price:  CLP$94,219
You save:  CLP$28,268.52
CN¥506.53
List Price:  CN¥723.65
You save:  CN¥217.11
RD$4,055.76
List Price:  RD$5,794.19
You save:  RD$1,738.43
DA9,420.16
List Price:  DA13,457.95
You save:  DA4,037.79
FJ$157.70
List Price:  FJ$225.30
You save:  FJ$67.59
Q542.52
List Price:  Q775.06
You save:  Q232.54
GY$14,601.52
List Price:  GY$20,860.22
You save:  GY$6,258.69
ISK kr9,773.40
List Price:  ISK kr13,962.60
You save:  ISK kr4,189.20
DH703.98
List Price:  DH1,005.73
You save:  DH301.75
L1,236.34
List Price:  L1,766.28
You save:  L529.93
ден3,998.26
List Price:  ден5,712.05
You save:  ден1,713.78
MOP$561.77
List Price:  MOP$802.57
You save:  MOP$240.79
N$1,291.99
List Price:  N$1,845.78
You save:  N$553.78
C$2,569.36
List Price:  C$3,670.67
You save:  C$1,101.31
रु9,319.09
List Price:  रु13,313.56
You save:  रु3,994.46
S/260.54
List Price:  S/372.22
You save:  S/111.67
K269.79
List Price:  K385.44
You save:  K115.64
SAR262.50
List Price:  SAR375.01
You save:  SAR112.51
ZK1,882.68
List Price:  ZK2,689.66
You save:  ZK806.98
L323.40
List Price:  L462.03
You save:  L138.62
Kč1,628.77
List Price:  Kč2,326.92
You save:  Kč698.14
Ft25,334.28
List Price:  Ft36,193.38
You save:  Ft10,859.10
SEK kr755.02
List Price:  SEK kr1,078.64
You save:  SEK kr323.62
ARS$61,468.17
List Price:  ARS$87,815.44
You save:  ARS$26,347.26
Bs483.33
List Price:  Bs690.51
You save:  Bs207.17
COP$271,845.87
List Price:  COP$388,367.89
You save:  COP$116,522.02
₡35,672.25
List Price:  ₡50,962.55
You save:  ₡15,290.29
L1,724.16
List Price:  L2,463.20
You save:  L739.03
₲522,510.75
List Price:  ₲746,475.93
You save:  ₲223,965.17
$U2,674.97
List Price:  $U3,821.56
You save:  $U1,146.58
zł281.37
List Price:  zł401.98
You save:  zł120.60
Already have an account? Log In

Transcript

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.

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.