Composite Pattern part 2

6 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

In our last video, I hope you remember that we have created these three components. And using this particular abstract component class, we created those three leaf components. Now CPU, RAM and SSD, all three were leaf component because inside this component we are not going to add any other components. As we discussed in that we are going to have one motherboard component, which was going to be a composite component inside of which we can add any of the leaf components. But to create a motherboard component, we need to create an abstract class first, so I'm going to add a new item. And this new item is a C sharp class with the name composite component.

This composite component is going to be that abstract class which can help me to create multiple concrete composite components. To make sure that this class is going to be public as well as abstract, and this composite component class is going to have one paragraph associated with that, which is going to be that component. So because this composite component can be associated with this component abstract class, inside this we are going to have a logic like this, you can see I have one generic list on the top, which is going to hold the objects of type component, and it's going to have a name underscore components is going to have all the list of components is going to be stored inside that and then you can see we have an add method. This add method is also taking one parameter of type component, but it's going to add those components into this particular generate list.

Our constructor of this composite component class is not having any complicated logic inside that but it's going to have a name and price and then we are going to use this name in price when we are going to create an object of this class. So this constructor is very simple. In below section, we have one property price which we are overriding, which is actually going to take the values of this parameter. And using that it's going to calculate total price for all the components which are there in the collection is going to do the addition of all the prizes, and is going to return the one simple price at the end. And that is going to be accessible with this property capital pre price. So this is a very simple logic, which I hope you guys are able to understand.

Now this total price is something which we are going to use in our test cases. So that's what we'll see at that time. Let's click on Save all. And now I'm going to create some composite component using this abstract class. I'm going to right click on my project and I'm going to add a new C sharp class and I'm giving a name of the C sharp class motherboard. I'm gonna click on Add and you can see I have a class motherboard inside which first, I'm going to specify that this class is going to have a logic like this is going to be public as well as you can see is actually using that composite component using which I'm creating one more component, but this time also we are going to specify that is going to have only one parameter which is price.

And then a name is something which we are directly hard coding there, which is motherboard. This component looks similar to our leaf component, but the only difference is the parent ever class is different. Let's add one more new item which is going to be another composite component which is going to be a name computer. This computer is actually my root level object, but this is also a composite object because it's going to have other component inside that. The only thing which I'm going to change is is going to have price zero with the name computer, because computer is going to have any base price. The total price of the computer is going to be Total Cost of whatever we need to add.

So this is one of a composite pattern component. And now let's click on Save all. And now is the time, so that we can use all of these things into our unit test project. So let's go to the unit test project. And inside this test method, I'm going to write some simple logic. But before that, I need to add a reference of the class library.

So I'm just going to add the reference of composite pattern. And like always, we are going to add a nice base also on the top. We are good with this. Now let's write some logic inside this method. So I'm going to write a code like this. You can see first I'm creating an instance of a computer class.

And we know that it's going to be a composite object. Inside this computer, we can add some more elements. So after this, I'm adding one motherboard, CPU, RAM drive everything. The computer is not having any price in the constructor. So that's why we do not have any parameters in that If you check motherboard is having a price and same way CPU RAM and SSD is also having an associated price with that. Once we are done with all these instances on the top you can see motherboard is a composite component.

So inside this we can add something and we are adding CPU as well as we are adding ram into that. So the total cost of the motherboard is going to be a combination of RAM CPU plus the original motherboard cost. So that's why motherboard is a composite while it's actually a total combination of this three is going to create one cost. If you see in the computer which is a composite object, the airforce adding motherboard. So computer is going to have motherboard as well as is going to have one leaf object which is drive and that's why the total cost of the computer is going to be combination of all four, which I'm actually adding inside this. Once we are done with this we are just writing one a source statement to check the price of the computer and that should be similar to this which should be total of all four, which we are adding into that, if everything is fine and if it is created properly, we are going to run the test case and that test case should be passed.

So if you see when I run the test case, this test case is successfully passed. It shows me that the structure of this computer object is properly created with this kind of nested hierarchical object. I hope you understood this scenario. And you understood our composite pattern, which actually helps me to create a complex object with the hierarchical structure. 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.