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
€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 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.