Decorator Pattern

15 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

The next design pattern, which we're going to learn right now is a decorator pattern. And this is also a structural design pattern in our category. Now decorator pattern is one of the frequently used patterns, whatever business applications have seen in my career, I'm sure that if I see 10 business application, this is one of the pattern, which is going to be there in out of 10 is going to be there in five. So this is one of the useful pattern for each and every developer because the basic purpose of the design pattern is actually it's going to add some functionality of a code to our existing code dynamically. Now, in our scenario, we are going to take an example of a burger joint with the name Mac Mickey. In Mac Mickey we actually provide a different kind of burgers to our customer.

And this different burgers are going to have varieties of customization options inside that These options are going to be customizable and my customers are going to choose Add or Remove any of the options which are available inside the burger customization. So, this is what the scenario we are going to work in our code sample and we are going to develop some tasty burgers for Mac Micki client, the goal is we are going to create a base burger first and after that we are going to add multiple decorators on that. So we are going to create one basic burger element and then we are going to add different different decorators like cheese, ham, and some other ingredients which are requiring the burger. If you want to understand decorator pattern, then you have to understand the flow of the object creation. Actually, in our case, when we are going to follow this decorator pattern, the flow of this pattern is going to go like this.

That first we are going to create one abstract component. This abstract component is going to be my base component using this app component, we are going to create one concrete component, which is going to be our basic bug. Now we can create multiple concrete component, but in this sample we are going to create only one. After this, once you have your concrete component, you can create one abstract decorator, which is going to be a small part which can associate with your base component. And then you can create multiple concrete decorators. From that, we are going to create multiple concrete decorators from one abstract decorator.

So this is a flow which we are going to follow in the sample. And when we do the core, I want you to remember this flow structure so that we can develop a code base on that. In my Visual Studio, I'm first going to create a new project and I'm giving a name of the project. Mac Mickey. This is a basic class library project. And inside this MAC Mickey when we click OK, we know that we are going to have one C sharp class with the name class one.

Now first, I'm just going to delete this class. And then I'm going to add one more project in the same solution. So I'm doing add new project. And this one is going to be my unit test project with the name Mac Mickey dot paste, I will click OK. And you can see this test project is now I'm coming back to my class library. And in this class library, the first thing is I need to create my abstract base component. So for that, we are going to right click on this project and we're going to add a new C sharp class.

Remember, in this full design patterns, we are not going to have any interface we are all going to have abstract classes. So I'm going to click on OK and this burger component is going to be my public as well as abstract class. And you can see I have a simple logic like this, how abstract class burger component and this worker component is having two abstract properties, name and price. We do not have any values inside this because this is abstract. Let's add a concrete class. So add a new item, this is going to be a class.

And this is going to be my concrete base component. So it's going to be a concrete component, I'm giving a name of the class basic burger. And inside this basic burger, I'm going to make sure this class is going to be public, as well as I'm going to put my abstract class. So we are going to put our burger component in this. I'm going to implement my abstract class into this. And if I do that, you can see I'm getting those two properties overridden inside this.

The core of this class is going to have the values which we are going to get from that. But let's change the code from this one to this. You can see I have two private variables on the top with the name underscore name and underscore price with some default values assigning to that. Then I'm using this private variables into my properties and these properties are actually getting the values from these variables. I will click on Save all. And now I'm going to deal with my burger component, which is abstract class.

And then we have a basic worker, which is our concrete basic burger component, it's time to move on to our decorators. As we know, the flow for the first thing, which we're going to create is going to be an abstract decorator. So we are adding a new C sharp class, and I'm giving a name of this decorator. This one is going to be my abstract decorator. So I'm making sure that this could be public as well as abstract. And inside this we are going to have some base functionality.

But before that, we know that this is a decorator. And in decorator pattern, decorators are going to be applicable on our abstract component. In our case there is this burger component. So we are just putting Collin and I'm adding burger component as a parent class here. The connection between these two abstract class is actually going to show me that this burger component parent class is the base class on this We can apply this abstract decorator. Let's quickly implement this abstract class into this.

Now inside this abstract class, we are going to override our name property. And we're going to have some logic using that like this, you can see that we have a one protected underscore component reference variable. And then we have a name and price. These two properties inside that my abstract decorator is actually going to have a base price zero. And then we have a constructor of that class, which is actually taking one parameter of type component. We are simply injecting the connection of the burger component into this.

And then the name and price property, which we have overridden are actually going to have the name which is going to be my component name plus that name, which is going to come from the protected variable. Same way, the price is going to be the base component price, plus the price Which I'm going to add into this. This code is so simple. Let's up to this point we are done with this abstract decorator, which is based on the burger component. Now it's a time to create more than one. Concrete decorators based on this particular class.

I'm going to right click on my project, I'm going to add a new item. And this new item is going to be class. And let's say that the first concrete component is going to be a wheat bread. So I'm giving a name of this wheat bread, and we will click on Add. This wheat bread class is going to be a public class and you can see this is actually based on that abstract decorator. Inside the constructor, we are going to inject that burger component dependency so that we can associate this with the burger.

And then we are just having the values for the name and price. The name of this particular decorator is read braid and we are giving a price of this which is going to be 20 So if something like this that based on the logic of our abstract decorator, the total price is going to be based on the base price plus this particular decorator price. We'll see that when we are going to use this right now is the time to create some more like this. So we're going to click on Save all and then I'm going to right click on my project and I'm going to add a new item which is going to be a second decorator with the name ham. Now if my customer wants a hamburger, we're going to add a ham in that and the core of the ham is exactly same like that.

We still have a decorator and then the name is ham as well as we have a price of that which is 10. Let's quickly create one more decorator. Maybe the third decorator is going to be cheese. So we are going to add a name cheese and you can see the only thing which we are changing his name as well as the price. So quickly, we have created actually three different decorators from that abstract decorator class. We have We breed and ham and cheese and all three are our decorator parts, we can create some more also for right now we are happy with the three decorator parts.

And we have one base component which is basic logo, and which is based on the abstract class burger component. Let's check this particular structure in our taste project. So we are just going to have a unit test project in which the first we need to add a reference of our class library. So we are adding a reference of Mac Mickey. I'm going to click on OK. And now we are going to add a namespace of that using Mac Mickey. I hope you're familiar with this now because we are doing this thing in all the samples up to this point.

Now inside this taste method, I'm gonna write my basic logic First, we have basic pro side by side, so we can check the test case there. And now the first thing which I'm doing is you can see I have a variant with the name burgo, which is actually an instance of placebo. So I'm actually creating a basic burger first. And when I have this variable, obviously Find that the this one the burger price is should be equal to 20 because this is not having any other decorator into that. So we are just checking an assert that this should be a 20. If this is true this basic cases would pass.

Second, we have a wheat bread burger, which is actually adding a new wheat bread and in that we are passing that particular burger object which we treated on the top. So, when we do this thing, this burger which is having a price 20 is going to have a we read into that, and that's why the total price is going to be 40. If this assert is passing, it means that the combination of the base component with the decorator is done successfully. You can see if I right click on this, and if I do run selected test, this test case is going to pass because it's matching with the price 40. If I change the price to 50, if I build and now if I try to pass this as case this should fail, and you can see this is actually failing And if I check, the reason of that is showing that the expected is 40, but the actual value is 50.

And that's where our combination of the decorator with the base component is done successfully. Now let's check this thing with some more decorators into that. So I'm making sure this is 40 right now and then I'm going to write some more lines of code like this. Now I'm creating first one hamburger, which is having your ham on that we'd read burger. So base price of the burger was 40 Plus we're adding a ham into that so that should be 40 plus 10 is going to be 50 into that because ham price is 10. So we know that so this should be 50 and then we are adding one cheese hamburger exactly below that.

And you can see in this cheese hamburger, I'm adding cheese twice. So it's not that I'm just want one slice of cheese, I want double cheeseburger actually. So we are having two cheese clauses inserted into that and inside that we have passing over hamburger insurance. If we see the cheese price is also 10. But because we're adding this thing twice, the total cost is going to be 50 plus this double cheese 10 plus 10. So it's going to be 70.

If we save all and if I try to run the test once again, this test is going to be passed because we have assigned us right prices. Now right now, we have the 75. And we have a connection of multiple decorators onto our one base component, which is basic logo. So it's 20 plus 40 plus 50, which is ham. And then finally we are at two cheeseburgers into that so that's why it is 70 I hope you understood the total calculation which we have done right now in this and this shows the connections of all the decorators with our base component. Now let's do one thing that you know we are going to add some more code into this and we are going to have some more decorators which Give me some cheese discounts also.

So exactly below the score, I'm going to add some more code, which is actually going to help me to give some discounts on this particular cheeseburger price. So if my customer is going to choose cheeseburger, then we are going to give them a discounted burger in which maybe we are going to add some cheese discount. You can see right now in this code, I am getting some errors because I do not have any cheese discount decorator for me. So let's do one thing. Let's create one decorator. So I'm going to add a new C sharp class once again, and I'm giving a name of the C sharp class cheese discount.

The code is going to be similar but the customer name is going to be cheese discount and I'm just changing the name of this also. With that I'm going to give a price which is going to be not 10 this time because it's a discounted burger we're going to give minus 10 So assume that today is actually a world Cheese Day and today in all the burgers we are going to give cheese free of cost. So we are going to have a unit Test once again. And if I build, it should show me that okay, it's showing the error because it's not public right now let's make this class public. And once it is public, we'll save all will go to this project. And now this did not show me that error.

So this is working, you can see that because we have added cheese twice, now we are adding a cheese discount also twice. And if I add a cheese discount, the actual cost is going to be 70 minus 20. So there should be 50. If I save and if I run the test case, this should pass. So let me run the test case. And you can see it's popping.

Now let's assume that we are giving the discount also and we got a new price. Now I do not want to give a double discount. Because my user is having double cheese. Let's say that the discount is available only for the one cheese. So we are just adding cheese count once while we have added cheese twice. So let's say that in this case when the Chiefs twice and we are given one day Count, the cost should be 70 minus 10.

That should be 60. But let me check whether the fish case is showing the same thing or not. If you see it's failing this time, and it's showing me that X Factor should be 60, not 50. So if I make it 60, and then if I run the test case, this should pass. I hope you guys are getting what we are doing in this particular sample. And the dynamic addition of this decorators into this particular base component.

That's what the magic of the decorator pattern. The key factor is the connection between these two abstract classes, and all the concrete components which we can create. 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.