|
1 | 1 |
|
| 2 | +# Definition |
| 3 | + |
| 4 | +## The "formal" definition |
| 5 | + |
| 6 | +> The Decorator Pattern attaches additional responsibilities to an object dynamically. Decorators provide a flexible alternative to sub-classing for extending functionality. |
| 7 | +
|
| 8 | + |
| 9 | + |
| 10 | +# How does it work |
| 11 | + |
| 12 | +```java |
| 13 | +// Coffee.java |
| 14 | +public interface Coffee { |
| 15 | + String getDescription(); |
| 16 | + double getCost(); |
| 17 | +} |
| 18 | + |
| 19 | +// PlainCoffee.java |
| 20 | +public class PlainCoffee implements Coffee { |
| 21 | + @Override |
| 22 | + public String getDescription() { |
| 23 | + return "Plain Coffee"; |
| 24 | + } |
| 25 | + |
| 26 | + @Override |
| 27 | + public double getCost() { |
| 28 | + return 2.0; |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +// CoffeeDecorator.java |
| 33 | +public abstract class CoffeeDecorator implements Coffee { |
| 34 | + protected Coffee decoratedCoffee; |
| 35 | + |
| 36 | + public CoffeeDecorator(Coffee decoratedCoffee) { |
| 37 | + this.decoratedCoffee = decoratedCoffee; |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public String getDescription() { |
| 42 | + return decoratedCoffee.getDescription(); |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public double getCost() { |
| 47 | + return decoratedCoffee.getCost(); |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +// MilkDecorator.java |
| 52 | +public class MilkDecorator extends CoffeeDecorator { |
| 53 | + public MilkDecorator(Coffee decoratedCoffee) { |
| 54 | + super(decoratedCoffee); |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public String getDescription() { |
| 59 | + return decoratedCoffee.getDescription() + ", Milk"; |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public double getCost() { |
| 64 | + return decoratedCoffee.getCost() + 0.5; |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +// SugarDecorator.java |
| 69 | +public class SugarDecorator extends CoffeeDecorator { |
| 70 | + public SugarDecorator(Coffee decoratedCoffee) { |
| 71 | + super(decoratedCoffee); |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public String getDescription() { |
| 76 | + return decoratedCoffee.getDescription() + ", Sugar"; |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public double getCost() { |
| 81 | + return decoratedCoffee.getCost() + 0.2; |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +// Main.java |
| 86 | +public class Main { |
| 87 | + public static void main(String[] args) { |
| 88 | + // Plain Coffee |
| 89 | + Coffee coffee = new PlainCoffee(); |
| 90 | + System.out.println("Description: " + coffee.getDescription()); |
| 91 | + System.out.println("Cost: $" + coffee.getCost()); |
| 92 | + |
| 93 | + // Coffee with Milk |
| 94 | + Coffee milkCoffee = new MilkDecorator(new PlainCoffee()); |
| 95 | + System.out.println("\nDescription: " + milkCoffee.getDescription()); |
| 96 | + System.out.println("Cost: $" + milkCoffee.getCost()); |
| 97 | + |
| 98 | + // Coffee with Sugar and Milk |
| 99 | + Coffee sugarMilkCoffee = new SugarDecorator(new MilkDecorator(new PlainCoffee())); |
| 100 | + System.out.println("\nDescription: " + sugarMilkCoffee.getDescription()); |
| 101 | + System.out.println("Cost: $" + sugarMilkCoffee.getCost()); |
| 102 | + } |
| 103 | +} |
| 104 | +``` |
| 105 | + |
| 106 | +As you can see, for each additional property that can be in a cup of coffee, we create a decorator class. This decorator class can alter other attributes of the main object (like `cost` and `description`). Using this pattern will able us to create extensions and removes the need to alter the original class. |
0 commit comments