---
# System prepended metadata

title: Design Pattern (3)
tags: [GLA Notes & Slides]

---

# Design Pattern (3)
###### tags: `GLA Notes & Slides`

Extended example notes for AP.

## Composite Pattern

See [here](https://hackmd.io/Ku0tZH6URwWL1HYys2mLYw#Composite).

## Decorator

* It addes behaviours to individual objects without inheriting from them.


:::spoiler Example: Coffee Maker

* This example is simplified from the example of the Head First book.

```java
abstract class Beverage {
    public abstract double cost();
}

abstract class CondimentDecorator extends Beverage {

}

class Espresso extends Beverage {
    @Override
    public double cost() {
        return 1.99;
    }
}

class HouseBlend extends Beverage {
    @Override
    public double cost() {
        return .89;
    }
}

class Mocha extends CondimentDecorator {
    Beverage beverage;

    public Mocha(Beverage beverage) {
        this.beverage = beverage;
    }

    @Override
    public double cost() {
        return .20 + beverage.cost();
    }
}

class CoffeeMaker {
    public static void main(String[] args) {
        Beverage espresso = new Espresso();
        System.out.println(espresso.cost());

        Beverage mocha = new Mocha(espresso);
        System.out.println(mocha.cost());
    }
}
```

:::

## Observer

See [here](https://hackmd.io/WxVlNJb1S4OPAVKzhSsRWw?view#Observer).