How does the decorator pattern chain method call?

I read one example of a Decorator design pattern. I realized that this design pattern changes the behavior of one particular instance dynamically. The example I gave below is also clear. What I did not understand is that when I call c.getCost() on the milk object, it returns 1.5. Only Simplecoffee getCost() returns 1, but where c.getCost on milk return 1.5?

Can someone explain the relationship between the Milk class and Simplecoffee and how the getCost() method thread is executed when called with a milk object? How does the getCost() method return 1.5?

 //Decorator Design Pattern interface Coffee { public double getCost(); public String getIngredients(); } class Simplecoffee implements Coffee { public double getCost() { return 1; } public String getIngredients() { return "Coffee"; } } abstract class CoffeeDecorator implements Coffee { protected Coffee decoratedcoffee; protected String ingredientseparator = ":"; public CoffeeDecorator(Coffee decoratedcoffee) { this.decoratedcoffee = decoratedcoffee; } public double getCost() { return decoratedcoffee.getCost(); } public String getIngredients() { return decoratedcoffee.getIngredients(); } } class Milk extends CoffeeDecorator { public Milk(Coffee decoratedcoffee) { super(decoratedcoffee); System.out.println("Milk Constructor"); } public double getCost() { return super.getCost() + 0.5; } public String getIngredients() { return super.getIngredients() + ingredientseparator + "milk"; } } public class Decorator { public static void main(String[] ar) { System.out.println("calling simplecoffee in main"); Coffee c = new Simplecoffee(); System.out.println("Cost:" + c.getCost()); c = new Milk(c); System.out.println("Cost:" + c.getCost()); } } 
+5
source share
4 answers

How does the getCost () method return 1.5?

The getCost() method in Milk first calls the getCost method on decoratedcoffee , which is a reference to SimpleCoffee . Therefore, it will call the getCost method from SimpleCoffee , which returns 1 (read: run-time polymorphism). Then the getCost method in Milk adds the return value of this call (from 1 to) to 0.5 , giving you a result like 1.5

This is the whole point of the Decorator template. The decorator is used to create a chain of method calls by transferring a new object to an existing chain.

+5
source

I would call your class Milk MilkedCoffee. The decorator adds additional functions to the source object, wrapping it, at the same time refers to the same interface of the source object, so that your client code can work on it without knowing whether it is coffee (the original class) or milk machine (the decorator ) or iced coffee (another decorator). Thus, the garnished coffee in your example should be coffee, not an addition to coffee, such as milk, sugar. Etc

To answer your original question, a return value of 1.5 looks great if your logic is to add .5 Cost when coffee is served with milk (in other words, framed as MilkedCoffee)

+1
source

Let's take a look and understand this piece of code:

 coffee c = new simplecoffee(); System.out.println("Cost:" + c.getCost()); 
  • Create a simplecoffee object and assign it c .
  • c declared the type of coffee (no problem, since simplecoffee is an implementation of the coffee interface).

Next step:

 c = new milk(c); System.out.println("Cost:" + c.getCost()); 
  • Create a milk object and assign milk to c to the new object (overwrite the old value c ).
  • milk extends the coffeedecorator abstract class, which also implements the coffee interface.
  • The simplecoffee object at the top is assigned to the instance variable inside the milk class.

Consider the implementation of milk.getCost () , which is called before the last print:

  public double getCost(){ return super.getCost() + 0.5; } 
  • super.getCost() is implemented in the coffeedecorator class. It simply calls the getCost() method of the coffee instance inside.
  • In our case, it is simplecoffee , which costs 1.
  • Milk costs 0.5.

So you come together 1.5.

Greets Alan.

+1
source

Can someone explain the relationship between the Milk class and the Simplecoffee class and how the getCost () method thread executes when called with a milk object. How does the getCost () method return 1.5?

Hard to do better than visualizing Decorator calls in First First design templates . Follow the green arrows below. The calls to getCost() nested, and the return values ​​are also displayed in green. The image also shows how Milk wraps the Simplecoffee class.

enter image description here

0
source

Source: https://habr.com/ru/post/1264165/


All Articles