Decorator

Decorator Pattern

Decorator

AddOnDecoractor is-A Beverage
SolidCoffeeCLass is-A Beverage
Decorator is-A AddOnDecoractor
Decorate Has-A SolidCoffeeCLass

This should only be used when the Cost() function is vastly different from each other.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39

abstract class Beverage() {
abstract public function cost();
}

abstract class AddOnDecorator extends Beverage {

abstract public function cost();
}

class Espresso extends Beverage {

public function cost(){
return 1;
}
}

class Caramel extends AddOnDecorator {

Beverage $beverage;

__constructor(Beverage $beverage) {

this.beverage = $beverage;
}

public function cost() {

return this->beverage->cost() + '0.2';
}

}


$coffee = new Caramel(new Espresso());
$coffee->cost();
---> 1.2



 Previous
Factory Factory
Factory Patterns are normally use to abstract away the logic for create a product.i.e creating a product by passing thro
2019-05-15 Ray Zhang
Next 
Observer Pattern Observer Pattern
Observer PatternObserver Pattern is commonly use to implement Pub/Sub model. IObservable Has-A IObservable WeatherStatio
2019-05-15 Ray Zhang
  TOC