Composite Pattern part 1

8 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

Next pattern which we're going to address right now is composite pattern. This is also a structural pattern and this actually organized our objects into a hierarchical structure. Now, if you want to understand composite pattern, I hope you guys have familiar with XML or HTML document. When we see this kind of markup document object model, we know that we have number of tag base element into that and we most of the time having one tag inside another one, this kind of hierarchical structure is achieved with the help of composite pattern. Now if you see on the screen I have a simple HTML page in which the root level component is HTML. Inside this we have head and body directly inside HTML file.

If you see there are certain tags like we have a title tag which is actually inside head and we have a div which is inside a body DB is one of the component which is inside a body tag, but it is already having some more tags inside that. Now, in our case, if you want to understand this composite pattern, you need to understand three terms first, this term tag component composite and leave component is the term which is used for the baseline object the root level object. In our case that was HTML compose it is useful for the container object which is under the base object, but it's still going to have some more components inside it. And the reason is that object which is not going to have any other object inside it is going to be the child does work. It will be the child element which is not going to have any other child in fact that if I compare the three terms with our HTML document, then you can see that our HTML tag is a component which is a root level component and inside this we have two composite head and body.

Inside head composite we have one leaf object Which is title. And inside the body composite, we have one composite object which is div. Inside the div composite, we have to leave object which is, which are these two input tags. Now looking into the structure if you understood this kind of hierarchical structure, now we have to create this kind of structure in our code sample. So, when we write a C sharp class object we can organize, we can structure them into this kind of hierarchical structure to maintain our project properly. In my case, in my code sample, I'm going to take a scenario of a computer, you can see that I'm going to have one of these root level components which is going to be a computer.

So we are again going to manufacture a computer and that computer is going to have two objects directly inside that one is motherboard and the other one is SSD. The motherboard is going to be a composite object which is going to have to leave object inside that RAM and CPU, while there will be one leaf object which is SSD Which is directly going to connect with computer. In short, we're going to create all these objects, few are going to be composed, you are going to believe, and the root level one is going to be my main component computer. Now let's see this thing in action in our Visual Studio application. In our Visual Studio, I'm going to create a new project. And I'm giving the name of this simple class library application, I'm going to give a name of this class and the application compose it pattern.

This composite pattern application is going to be there in my submission total. I'm going to add one more project which is going to be a test project and the name of the test project. I'm going to give composite pattern dot test. First, I'm going to delete this class inside the class library. And once it is deleted, we are going to add some more new things inside this. So I'm going to right click on this composite pattern application and I'm going to add Add a new item, which is going to be a class.

So I'm giving a name C sharp class and I'm giving a name component. This component class is going to be public, as well as abstract. And inside this abstract class, we're going to have some code like this. You can see I have some protected read only variables on the top with the name and price. And then we have a constructor inside which we are using this name in. My component class is having a constructor with two parameters.

And these two parameters are the same ones which are assigned to those protected read only variable. We have a method add, which is actually useful when I want to add some of the components inside the component or leave a component actually. So this add method is the one which is going to stop this component used inside someone. In short, we are making sure that this component is going to be the root level component So we cannot add this component into any other component. If someone tries to do that is going to throw an exception. Other than this, all the other components, which we are going to create is now going to have this method.

So when we want to add it into some other elements, we can do that we have ADD method, which is going to take one parameter which is component and this parameter is going to be helpful in adding those things. If we cannot add this if we do throw an exception, after this add method, we have two properties which are name and price which are public and which are just passing those variables into that. Once you're done with this, let's move forward to our next C sharp class. And the next C sharp class is going to be my concrete component based on this abstract class. I'm going to right click on my project and I'm going to add a new item, this class name, I'm going to give CPU, the CPU clock is going to have one clock into That which is component. And I'm going to just implement this abstract class into that, you can see that if I generate my decorator using this class, then you can see this CPU is actually having two parameters.

And it's having a beast, he will do that. So I'm not going to use that one. In spite of adding that generated code, I'm going to have my own code, which can have my parameters when you have my day. So you can see inside this CPU, I'm having a new constructor, which is not having two parameters. It's just having one parameter, which is nice, because I do not want to pass a name into the parameter. And in the beast, I'm giving a static name of this component, which is CPU.

And the price is the one which I'm going to assign when I'm going to create an instance of the CPU. I'm going to save the CPU clock. And this is a very simple component. Let's add one more component into this. So I'm adding a new component. And this is going to be a name that this ram component is also going to use That component C sharp class in the similar way, so you're going to have component so we have a constructor which is just having a named ramp and then we have a parameter right into that.

Let's create one more component. And I'm giving a name of this component SSE which is going to be for solid state drive. And similarly this class is also going to be public as well as is going to use component to that. And then inside this you can see we have a same kind of constructor to do to have a name SSD and then we are going to have a name of the constructor also SSD have you dealt with this, you can see we are done with one particular component C sharp class and we have three different components using that C sharp class one is SSD RAM and CPU. These three components are actually leaf component. So we know that right now we have actually created one root level component which is component And we have three leaf which is CPU, RAM and SSD.

If I follow my slide which I have shown you earlier, in that we were having one motherboard component, which was a composite component. We have still not created that motherboard that we are going to create 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.