Abstract Factory Pattern Part 2

17 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 far, so good, we are done with the one particular I factory interface. And in that we have three different methods which are returning I computer, I smartphone and tablet. And we have already created some classes which are associated with that now it's time to move on. So we are going to add a new item in our project. And this new item is going to be my class KitKat manufacturing company, we know that this is going to be the class which is going to deal with all the manufacturing logic. So I'm going to make sure that this is going to be public.

And once this class is public, it's time to write some logic inside that. I do not want to create any methods right now. First, I want to create one constructor. I hope you know that the constructor is going to be the same name. The class. So this is a method, which is having a theme name, but we call it constructor.

Inside the constructor of this class, I'm going to deal with my factory because KitKat manufacturing company is going to associate with the factory interface. And using this factory interface only is going to connect with my either client, a factory or client B factory. So I'm simply passing one parameter into my constructor, which is going to be of type a factory and this parameter is actually going to associate with my private read only variable which I have created on the top. So whatever we are passing in the parameter, we are going to store that thing in the variable underscore factory. And using that we can access all this classes and the associated products with that. So I'm typing underscore factory is equals to factory.

Now once you're done This we want a multiple products, multiple types of products. But one thing we forgot, we know that KitKat manufacturing company class is actually associated with a factory interface. I factory interface is actually associated with client, a factory and client B factory. Now when my client a or client B is going to order for manufacturing number of products, we know that they are not going to have only one computer, one smartphone and one tablet. Maybe there is one thing which is associated with that which is quantity of the product. There are situations when my clients are going to ask me that I want you to manufacture for computers to smartphones and one tablet for me.

Now in this case, I have to take a help of quantity. For that I'm going to add a new item into my project and I'm just giving a name of this class. dot CS, this auto.cs class is also going to be public. And this class is going to help me in this quantities structure. That's why inside this class, I'm going to add some properties. I'm adding three properties with the name, computers, tablets, and smartphones.

And you can see all three properties are having a return type integer. Because these three properties are just going to give me how many number of computers tablets and smartphones I need to manufacture. So my either my client A or B, they're going to put this order using this KitKat manufacturing company. And we need to create order based on that. To deal with this order, I want to create one more thing which is going to be a generic list properties. So just generic list collections are created first, and you can see that this three things computers, tablets, and smartphones, all three returning a similar type of data.

And these are nothing but generic lists, which are private as well as read only. Once we have something which is private, we know the scope of this three things are actually within this class. So I'm going to use this thing inside the constructor of this class. And I'm going to just initialize these three list collections into that. The three list collections are initialized inside my constructor, and it's ready to use within this class. Now, one thing which we need to remember is that we are going to use all the things into our console application get caught inside which we have programmed or sees.

This three collections are accessible within this constructor, but it's not accessible within that class, which is program.cs. So I'm quickly going to create some properties which are going to be public and those properties are going to associate it with these three different lists collections. You can see that I have added Three different properties with the name computers, tablets and smartphones. And these three properties are having a get return from my collections, computers, tablets, and smartphones and all three are public. That's why we can access this properties into our program.cs class of our console application. Also, there's three things computers, tablets and smartphones are my simple properties.

I hope you guys are familiar with properties and the read only private variables. And I hope this kind of structure you have created earlier also in your projects. We are ready with the three collections and the three properties which are ienumerable type, and which can hold any kind of collections inside this. Now it's time to use this order.cs class into a rocket car manufacturing company. And for that, I am going to create one new method, which is going to have a name, create computer, you can see that I have a create computer method, which is actually going to return nothing, it's returning void. And then we have one integer parameter inside that which is actually count.

This count is going to be that particular number, which my client is going to enter when he's going to place an order. Based on this count parameter, we are going to run the while loop, which is actually going to deal with my create computer method. And you can see that this create computer method is going to be called with the help of underscore factory. And every time when we are adding a new computer, we are just adding this thing into this particular private read only list collection which is underscore computers. We don't know that this computer is going to be created for client a or client B. And that's why that logically good right inside of a console application But right now we just know one thing that create computer method can create a computer for me based on my I factory parameter which I'm passing inside my constructor of the Kitt car manufacturing company.

The Association of this parameter we are going to do inside of a program class. And then we are going to call this method based on that. Right now we need multiple methods which can actually help us in creating smartphones and tablets. So I'm going to create two more methods, which is the name create tablet and create smartphones. And this two methods are actually having the same logic like create computer I have seen parameter count but we are calling create template and create smartphone methods with the help of the same factory object. And we are storing this thing in the spective lists which are there in the constructor.

Once we're done with this, it's a time to create a new method, which is going to have a name produce this produce method is going to happen Have one parameter, which is going to be my order. And that order is going to have those three properties, computers, tablets, and smartphones. And we are just passing the values of those three properties into this methods, create computer, create templates, and create smartphone, which is exactly below that, these three parameters are going to be passed into those methods. And based on that, we are going to run the while loop which is run inside that. All in all, this one particular produce method is the one which I have to call from my program.cs class. I'm going to click on Save all.

And now we are going to use all of this classes into our program.cs file, which is inside of a console application. Before I write a logic inside this main method, let me do one thing. Let me add a reference of my ID card core into our console application. So I'm adding add reference. And I'm gonna click on OK And I'm going to add a nice face on the top, which is using get card dot code. Once we do this, we are ready to use our all the classes into our console application.

And the first thing which I'm going to do is I'm going to ask some input from a user. So for that, you can see that I have console dot write, in which I'm going to ask to my user, whether he is client A or B, whatever my user is going to enter in the console, we are going to read that and we're going to store that thing in a variant with the name client. This particular text which user is going to enter based on that we are writing a condition that we have a simple I factory type of instance variable. And this is a factory instance variable is either going to associate with client a factory or client B factory, based on the condition which I'm checking in this. So if my user adds client a is going to associate with client, a factory and if it enters B is going to associate with client B factory, and it's going to use all this products which are associated with particular factory.

So using this particular logic, we can first decide which factory we are going to deal with. If user enters anything nonsense, we're just going to return from that post, I need to create one method, which is going to take care of the input from the user from the console. So I'm just adding one method with the name convert.to, int 32. And you can see that this method is actually having one string input parameter, which is going to be added from the user. If user put some null or whitespace into that particular console, then we are just going to count that thing as zero. So whenever user enters whitespace, or no, we just pass into our customized method, and it's going to convert that thing into a zero.

Now it's the time to use this method into our logic. So I'm just going to go Create an object of order like this, you can see that I am creating an object of order. And using this order, we are just going to access the properties, computers, tablets and smartphones. In this integer properties, we are just passing the values which are going to come from the read line. Now I'm going to ask user how many computers you want me to manufacture. What a user is going to enter is going to add it into this read line and this will store into this computer's collection.

Same way, I'm going to ask how many tablets you want. If my user enters any value is going to find, but if my user enters any null or whitespace, is actually going to convert that thing into zero, and that zero is going to be stored into that auto dot tablet's same way we are going to ask to the user that how many phones you want and whatever number is going to add. We are just going to store that thing into our auto dot smartphone. Once we are done with this, we are actually going to get one order from the client And that is going to have the computer smartphones and tablets. Now it's the time to associate these things with our KitKat manufacturing company class, where we have the constructor with the parameter of a factory. So I'm just associating that factory object into this KitKat manufacturing company, which is going to call the constructor.

Once it is calling the constructor association of the factory is going to be done with either client a or with client B. And this object is going to be associated with my variant with the name company. Using this company object, we can actually call our method which is produce and we know that produce method is going to take one parameter which is going to be of type order. So I'm simply going to Pass my order object, which is now already having all the properties value. I'm passing that order object into that and then we are just calling the method. internally we know that when we call the bodies method going to run those wire loops.

And then based on that, it's going to generate all the products for me. And we just need to check whether we are getting the proper products or not. So I'm just having console dot write line, which are going to print a number of computers, number of tablets and number of smartphones. And he's just going to print the count of that into that. So at the end, we have console dot readline, just to check whether this is working fine or not. Let's quickly run the sample and we can see whether it's working for both the clients and all the products or not.

You can see how a console is asking the client EOB suppose first thing I want like a and then I'm gonna hit enter. It's asking me how many computers I need. Suppose we are going to say that we want 10 computers and tablets. I do not want any tablets right now. So I'm just hitting Enter is going to be take a blank space and then in the phones. I'm thinking that I on four phones.

Once I do this, if I hit Enter, you can see it's showing me that we created 10 computers, zero tablets and four smartphones for you. So this is something which is done for client a. Now let's run once again and this time, I'm going to use that this is going to be client B. And client B is going to order for maybe two computers, six tablets and eight smartphones. When you hit enter, you can see it's already done. It's showing me that these are created.

It means my code is working for both the clients and all three types of products and all six products. Now let's quickly recap the structure what we have done. I hope you remember that we created three interfaces the first step within the my computer, I smartphone and tablets. Using these three computers we have actually created a number of classes. So we created six classes which are connected with these three interfaces. This classes are actually representing my six products and these three interface are representing my three types of products.

This was the reason that I was saying that I have six products and three types of products. We have one factory interface which is actually associated with three interfaces, I computer I smartphone and tablet, the interface I factory is connected with this three interfaces which are counted as sub factories and using this three interface we are connected with actually those six classes. The connection between this AI factory and this interface is dotted. The reason behind that is the connection between these two interfaces are actually not direct connection. It's not a direct implementation, while the eye factory is connected with client, a factory and client v factory, which is an implementation. So you can see this from this structure, that the classes are connected with interface in the direct connection.

That's why it is a direct line. But in PCs are connected with each other using the dotted line. And that's why there is not the implementation. But it's because one I factory interface is actually having three methods which are returning of type I computer I smartphone and a tablet. So that's why I'm saying this is not a direct connection is the router connection, because this is not a direct implementation is actually having a methods which are returning of the similar type. My factory interface is connected with my cut cut manufacturing company using the constructor and that's why this is something which is known as dependency injection.

This dependency injection is going to happen at runtime and the instance of a factory is going to be passed into our constructor. That's why using that is going to connect with either client a factory or client B factory. car manufacturing company is going to be our actual class which is having those collections and the produce method and everything which can actually call my six classes and create any of the products which are required based on the insurance which we are passing from that. We know that our order class is also connected with a manufacturing company so that we can use all the order quantities into that. I hope you understood this thing.

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.