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
€65.39
List Price:  €93.41
You save:  €28.02
£55.92
List Price:  £79.90
You save:  £23.97
CA$96.01
List Price:  CA$137.16
You save:  CA$41.15
A$107.15
List Price:  A$153.08
You save:  A$45.93
S$95.13
List Price:  S$135.90
You save:  S$40.77
HK$547.14
List Price:  HK$781.66
You save:  HK$234.52
CHF 63.86
List Price:  CHF 91.23
You save:  CHF 27.37
NOK kr775.40
List Price:  NOK kr1,107.76
You save:  NOK kr332.36
DKK kr487.78
List Price:  DKK kr696.86
You save:  DKK kr209.07
NZ$118.01
List Price:  NZ$168.60
You save:  NZ$50.58
د.إ257.06
List Price:  د.إ367.24
You save:  د.إ110.18
৳7,680.49
List Price:  ৳10,972.60
You save:  ৳3,292.11
₹5,842.03
List Price:  ₹8,346.11
You save:  ₹2,504.08
RM332.86
List Price:  RM475.54
You save:  RM142.67
₦86,437.65
List Price:  ₦123,487.65
You save:  ₦37,050
₨19,491.96
List Price:  ₨27,846.85
You save:  ₨8,354.89
฿2,586.09
List Price:  ฿3,694.58
You save:  ฿1,108.48
₺2,265.39
List Price:  ₺3,236.41
You save:  ₺971.02
B$363.53
List Price:  B$519.35
You save:  B$155.82
R1,302.64
List Price:  R1,861
You save:  R558.35
Лв127.90
List Price:  Лв182.73
You save:  Лв54.82
₩96,270.48
List Price:  ₩137,535.16
You save:  ₩41,264.67
₪262.29
List Price:  ₪374.71
You save:  ₪112.42
₱4,033.94
List Price:  ₱5,763.02
You save:  ₱1,729.07
¥10,867.12
List Price:  ¥15,525.12
You save:  ¥4,658
MX$1,187.12
List Price:  MX$1,695.96
You save:  MX$508.84
QR254.93
List Price:  QR364.20
You save:  QR109.27
P994.08
List Price:  P1,420.18
You save:  P426.09
KSh9,360.69
List Price:  KSh13,372.99
You save:  KSh4,012.30
E£3,358.63
List Price:  E£4,798.26
You save:  E£1,439.62
ብር4,003.77
List Price:  ብር5,719.92
You save:  ብር1,716.15
Kz58,546.63
List Price:  Kz83,641.63
You save:  Kz25,095
CLP$67,216.99
List Price:  CLP$96,028.39
You save:  CLP$28,811.40
CN¥506.70
List Price:  CN¥723.89
You save:  CN¥217.19
RD$4,073.53
List Price:  RD$5,819.58
You save:  RD$1,746.04
DA9,418.34
List Price:  DA13,455.35
You save:  DA4,037.01
FJ$158.31
List Price:  FJ$226.17
You save:  FJ$67.86
Q543.96
List Price:  Q777.12
You save:  Q233.16
GY$14,650.29
List Price:  GY$20,929.88
You save:  GY$6,279.59
ISK kr9,815.39
List Price:  ISK kr14,022.59
You save:  ISK kr4,207.20
DH707.71
List Price:  DH1,011.06
You save:  DH303.35
L1,237.78
List Price:  L1,768.33
You save:  L530.55
ден4,025.24
List Price:  ден5,750.59
You save:  ден1,725.35
MOP$563.96
List Price:  MOP$805.69
You save:  MOP$241.73
N$1,304.33
List Price:  N$1,863.42
You save:  N$559.08
C$2,570.38
List Price:  C$3,672.13
You save:  C$1,101.75
रु9,397.27
List Price:  रु13,425.24
You save:  रु4,027.97
S/263.43
List Price:  S/376.35
You save:  S/112.91
K270.11
List Price:  K385.89
You save:  K115.77
SAR262.49
List Price:  SAR375.01
You save:  SAR112.51
ZK1,873.89
List Price:  ZK2,677.10
You save:  ZK803.21
L325.37
List Price:  L464.84
You save:  L139.46
Kč1,643.47
List Price:  Kč2,347.91
You save:  Kč704.44
Ft25,458.03
List Price:  Ft36,370.18
You save:  Ft10,912.14
SEK kr764.90
List Price:  SEK kr1,092.76
You save:  SEK kr327.86
ARS$61,327.27
List Price:  ARS$87,614.14
You save:  ARS$26,286.87
Bs483.57
List Price:  Bs690.85
You save:  Bs207.27
COP$273,218.78
List Price:  COP$390,329.27
You save:  COP$117,110.49
₡35,710.66
List Price:  ₡51,017.42
You save:  ₡15,306.75
L1,733.65
List Price:  L2,476.75
You save:  L743.09
₲524,442.73
List Price:  ₲749,236.02
You save:  ₲224,793.28
$U2,683.09
List Price:  $U3,833.15
You save:  $U1,150.06
zł283.24
List Price:  zł404.64
You save:  zł121.40
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.