Abstract Factory Pattern Part 1

14 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

So in our last video, we understood factory pattern which was a creational pattern. And now we are moving forward to the second design pattern which is also a creational pattern. Name of the design pattern is abstract factory pattern. Now I hope you can notice one thing that in the name of the design patterns we have one important word which is factory which shows me that there is also something which is connected or something similar to our factory pattern which we learn in the last video. Let me tell you one thing that in this particular design pattern video you are going to create a factory pattern in a very advanced way because this is not going to be one simple factory like in the last video. This pattern is also called as a factory of factories because it's good to have more than one factory send to one.

And actually, this is something which is almost there in all the real life business values. Because whatever sample we have done in the last one, that was a factory pattern, but in real life, we do not have a project which is going to have only a single factory, we most of the times, we are going to have more than one factories into one particular project. And that's why abstract factory is the one which is going to be the most useful, this creational pattern. In this case, the basic terminology of the factory pattern is going to show us that our interface is going to be responsible for creating a set of related objects or dependent objects with that without defining the concrete class. If I compare this scenario with the last sample, in that also we were having interface, which was actually with the name I factory, and that was responsible for my face.

Three instances. In this case also we are going to have an interface, but this interface is going to associate with the other factories, which may be going to create different types of product or different different products. One basic difference between the factory pattern and abstract factory pattern is in abstract factory pattern, we are not going to have our concrete classes which were associated with the interfaces. So without specifying the concrete classes somewhere the main root level interface is going to take care of the other interfaces, how we can deal with this multiple factories and how we can associate one main route level factories with the multiple factories. That's what we need to see in this particular sample. Now, before we see the code, let me tell you one thing that we are also going to deal with this scenario which is going to have a KitKat manufacturing company with us.

The company's same which you have used in the last time Also, but in this case, this time, this company wants me to create three products, not one. And like last time, we still have more than one client. So we are going to have multiple clients inside this. So the scenario of the project is this, that we are going to have multiple clients and for each client, we are going to create multiple products. When we want this kind of thing, we have three kinds of products right now, computers, tablets, and smartphones. Now we are going to create three types of products.

That's why maybe our structure is going to change. And somewhere we need one factory pattern, which can be associated with all three different products. So let's see how we can deal with the code in this case. In this scenario, you can see that I have created two basic projects in one solution. One is get card, which is actually my console application and the other one is get card cool which is actually My class library. Right last sample, we are going to use this to projects and the most of the code we are going to write inside this card core.

And then we are going to have everything inside get card main method. Now the first thing which I'm going to do is I'm going to right click on my car core project, and I'm going to add a new item. This new item is actually going to be my first product type, which is going to be computer so I'm going to add one interface. And I'm going to give a name of the interface which is going to be AI computer. I will click on OK. And this one is going to create a new interface for me which is going to be a computer. I'm going to make sure that this computer is going to be public so that I can access this thing anywhere.

And after this. I'm going to copy this i computer.cs file, and I'm going to paste it twice Because I am going to create multiple types of products. The second interface is going to have a name, I tablet, and the third one is going to have a name I smartphones. Now once we have three interfaces, we know that we are going to have three different types of products. Now, all three interfaces are public. And we are going to change the name of this interface file with this I smartphone and I template.

I'm going to right click on my project and I'm going to add a new item now. And this time this is going to be a class because we have three interfaces. Now is the time to create classes which are associated with this. That's why I'm going to give a name of this class client a and we know that you know we are going to have more than one clients. So assume that this class is going to represent client a and the computer for example. Day, and therefore the name of the class is client computer.

I'm making this class public. And now, same way because we have multiple clients and multiple products, I'm just making a copy of this class. So we're going to rename this classes to client, a tablet, as well as client a smartphone. In short, these three classes are going to represent my three different products which are going to manufacture for my client a. Obviously, these three classes are actually having three different types of products and three different products. Once you're done with the renaming of these files, now let me change the class names also to the similar.

So we have client a tablet, and we have client a smartphone. Once we have the three classes ready with us, I'm going to copy all three classes and I will paste them Once we do that, you can see that we got a copy of this three classes. And now I'm going to rename this for my client B. So this one is going to be client B tablet. Same way, we're going to have some more class renamed like we are going to have client B smartphone. So this is going to be a smartphone only but for a different client.

And then we are going to have client B tablet also. And then we are going to have client B computer also. So you can see now we have six different classes and out of the six different classes, to our computers, to our tablets and to our smartphones. And I'm just renaming these classes based on the file names. Let me build this project. And I hope I'll get build succeeded.

Yes, I'm getting that and now it's time to move on. So we have three interfaces in our site. And you can see that we have six different products which are actually representing three types of products. So we if someone asked me I would say that we have computers, smartphone and tablet, but we have six different products for two different clients. Now because this client a computer is a computer to specify the type we are just implementing this I computer interface in that same way client B computer is also offer same type so we are going to add a computer in that. Similarly we are going to smartphone and we are adding a smartphone in that and client the smartphone is also going to be of the same type.

So we are adding a smartphone in that. Let's go to the tablet and we are reading it tablet and client B tablet is also going to be a tablet. Logically, we should have some logic inside this three C sharp classes because these three C sharp Classes are implementing interface. So, we should have some method inside the interface and when we implement the interface we should have some logic inside this. But right now, we are not making this code that much complex. So, we do not have any logic here.

And, but if you want in the real sample, you should have some logic inside a computer. And then you're going to implement that interface into this class and you can write some different different logics inside these three methods. So, the six classes are going to have logical code inside that, but right now, we are not doing that thing because we are not concentrating on that. So, we will right click on our project now and we are going to add a new item. This new item is going to be my interface which is going to be my factory. So now this is going to be my actual factory which can deal with my all the products.

I will make sure that this class is public and then in Inside this AI factory, it's time to write some methods. So I'm going to create one method first, maybe the name of the method is going to be create computer. So this method is going to create a new computer for my client which is going to return of Type II computer. So we are going to make sure that this method returns I computer type of object. We are going to copy this method and because our factory deals with multiple products, I'm going to say that my second method is going to return I smartphone and the name of the method is create smartphone. Similarly, the third one is going to return a tablet and the name of the method is going to be create tablet.

You can see that we have one eye factory which is actually having three methods inside that which are returning three different types of products. Now my each method is actually returning some particular type of product, which is a computer I smartphone or a tablet. But we know that because we have two clients, this one method can actually associate with two different kinds of computers. So client, a computer or client computer, anyone can be there into that, which is returning a computer. Same way, the second method is also going to associate with any of the classes of client a or client B. Let me right click on the project and I'm going to add a new item again.

And this new item is going to be a class. So because we have a factory, now we have to create a implementation class of that. And I'm creating a name of this client a factory. This client a factory class is going to implement I factory and when we are going to implement this interface by right click quick actions. And we are going to implement this interface, you can see that I'm getting three different methods. Now this three different methods are going to return three products specifically for this particular client.

So we are going to return the client a computer. And same way the second and third method is going to return client, a smartphone and client a tablet. So we are just going to rename this thing to client, a smartphone and client a tablet. So this class is returning all the products for this particular client. Now it's time to deal for the second one. So let me go to my solution explorer.

I'm going to copy my client a factory I'm going to paste it and I will rename this class to client B factory. This client B factory class is going to give me all the products of client B. So we are quickly going to change the class names which are returning client B computer, client B smartphone and client B tablet. Once we are done with this, we are all set to go you can see that we have two factories now which are client a factory and client B factory, both associated with our interface I factory, which is having these three methods inside that. And we have two different implementations of this interface which is actually connected with my three three products of my each client. So this are the three products of client a and same way we have three different products of client we also where we have Client B computers, smartphones and tablets.

Once you're done with this is a time to deal with our credit card manufacturing company, which can actually manufacture these three types of products and six different products for me. So that's what we are going to do in our next video. 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.