Skip to content

Commit d5badb7

Browse files
authored
Merge pull request #10 from dancergraham/feature/decorator
Feature/decorator
2 parents 0ef758f + 71a9ee3 commit d5badb7

File tree

2 files changed

+58
-8
lines changed

2 files changed

+58
-8
lines changed

chapter03_decorator/coffee.py

+43-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import abc
22

3-
# TODO : Add Dark roast, Soy, Whip
4-
53

64
class Beverage(abc.ABC):
75
"""Base class for all beverages"""
@@ -41,6 +39,15 @@ def cost(self):
4139
return 0.89
4240

4341

42+
class DarkRoast(Beverage):
43+
44+
def __init__(self):
45+
self.description = "Dark Roast Coffee"
46+
47+
def cost(self):
48+
return 0.99
49+
50+
4451
class Mocha(CondimentDecorator):
4552
def __init__(self, beverage: Beverage):
4653
self.beverage = beverage
@@ -52,15 +59,46 @@ def cost(self):
5259
return self.beverage.cost() + 0.20
5360

5461

62+
class Whip(CondimentDecorator):
63+
64+
def __init__(self, beverage):
65+
self.beverage = beverage
66+
67+
def get_description(self):
68+
return self.beverage.get_description() + ", Whip"
69+
70+
def cost(self):
71+
return 0.1 + self.beverage.cost()
72+
73+
74+
class Soy(CondimentDecorator):
75+
76+
def __init__(self, beverage):
77+
self.beverage = beverage
78+
79+
def get_description(self):
80+
return self.beverage.get_description() + ", Soy"
81+
82+
def cost(self):
83+
return 0.15 + self.beverage.cost()
84+
85+
5586
def star_buzz_coffee():
5687
"""Test code"""
5788
beverage = Espresso()
58-
print(beverage.get_description(), beverage.cost())
89+
print(beverage.get_description() + f" ${beverage.cost()}")
90+
91+
beverage2 = DarkRoast()
92+
beverage2 = Mocha(beverage2)
93+
beverage2 = Mocha(beverage2)
94+
beverage2 = Whip(beverage2)
95+
print(beverage2.get_description() + f" ${beverage2.cost()}")
5996

6097
beverage3 = HouseBlend()
98+
beverage3 = Soy(beverage3)
6199
beverage3 = Mocha(beverage3)
62-
beverage3 = Mocha(beverage3)
63-
print(beverage3.get_description(), beverage3.cost())
100+
beverage3 = Whip(beverage3)
101+
print(beverage3.get_description() + f" ${beverage3.cost()}")
64102

65103

66104
if __name__ == "__main__":

chapter03_decorator/readme.md

+15-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,19 @@
11
# Chapter 3: Decorator design pattern
22

3-
> **Decorator**: Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.
3+
> **Decorator**: Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative
4+
> to subclassing for extending functionality.
45
5-
Not quite the same as python [decorator syntax](https://docs.python.org/3/reference/compound_stmts.html#grammar-token-decorators) as in python you call the _decorated function_ and the decorating function is called first whereas the _decorating function_ must be called here.
6+
Not quite the same as
7+
python [decorator syntax](https://docs.python.org/3/reference/compound_stmts.html#grammar-token-decorators)
8+
as in python you call the _decorated function_ and the decorating function
9+
is called first whereas the _decorating function_ must be called here.
610

7-
I subclass `ABC` and used the `@abstractmethod` decorator from the `abc` module here but do not use any of this functionality - it just serves as documentation.
11+
I subclass `ABC` and used the `@abstractmethod` decorator from the
12+
`abc` module here but do not use any of this functionality -
13+
it just serves as documentation.
14+
15+
## Running the code
16+
17+
```bash
18+
python coffee.py
19+
```

0 commit comments

Comments
 (0)