Login Register






[Paper] OOP Design Pattern: Decorator filter_list
Author
Message
RE: [Paper] OOP Design Pattern: Decorator #5
(04-06-2013, 02:13 PM)Deque Wrote:
(04-06-2013, 11:19 AM)Psycho_Coder Wrote: You explain the things in such a cool manner. Thank you.
But can you explain the house blend part a bit more , where the blue and green arrows are present and the costs are getting added, I got a bit confused there.

I will try to and add the explanation to the tutorial if it helped you. The image is the most important part to understand this:

[Image: eosr94a3.png]

Look at this. That's the whole point of the decorator. You decorate a house blend with a milk and another milk ingredient.
So if you do this to create it:
Code:
Beverage houseblend = new HouseBlend(); milk1 = new Milk(houseblend); milk2 = new Milk(milk1);
You get an object that is a Milk that contains a Milk that contains a House Blend, which is exactly what the image displays.

If you call the method cost on milk2, the milk2 object will do this:

Code:
method double costs() { return milk-costs + getBeverage().costs(); }

It adds up the own costs with the costs of the beverage it contains.
getBeverage().costs() returns the costs of milk1, which does this:

Code:
method double costs() { return milk-costs + getBeverage().costs(); }

getBeverage() will now return the houseblend.
This way the costs of houseblend, milk1 and milk2 add up, so that you get the sum of the houseblend and its ingredients.

If you still have problems understanding this, you might have a look into understanding recursion.

Well I got it this time, the following pseudo code made it clear.
Code:
Beverage houseblend = new HouseBlend(); milk1 = new Milk(houseblend); milk2 = new Milk(milk1);

I know Recursion but I got a bit confused that time when I read this paper. But my concepts are clear now. Thank again.
[Image: OilyCostlyEwe.gif]

Reply





Messages In This Thread
[Paper] OOP Design Pattern: Decorator - by Deque - 04-06-2013, 11:07 AM