Skip to content

Commit fd22856

Browse files
authored
chapter 3
1 parent fb88038 commit fd22856

31 files changed

+394
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
I don't know how to implement it in python.

decorator/pizza/Cheese.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from Pizza import Pizza
2+
from ToppingDecorator import ToppingDecorator
3+
4+
5+
class Cheese(ToppingDecorator):
6+
def __init__(self, pizza: Pizza):
7+
self.pizza = pizza
8+
9+
def getDescription(self) -> str:
10+
return self.pizza.getDescription() + ", Cheese"
11+
12+
def cost(self) -> float:
13+
return self.pizza.cost() # cheese is free

decorator/pizza/Olives.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from Pizza import Pizza
2+
from ToppingDecorator import ToppingDecorator
3+
4+
5+
class Olives(ToppingDecorator):
6+
def __init__(self, pizza: Pizza):
7+
self.pizza = pizza
8+
9+
def getDescription(self) -> str:
10+
return self.pizza.getDescription() + ", Olives"
11+
12+
def cost(self) -> float:
13+
return self.pizza.cost() + 0.30

decorator/pizza/Pizza.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from abc import ABC, abstractmethod
2+
3+
4+
class Pizza(ABC):
5+
description: str = "Basic Pizza"
6+
7+
def getDescription(self) -> str:
8+
return self.description
9+
10+
@abstractmethod
11+
def cost(self) -> float:
12+
raise NotImplementedError

decorator/pizza/PizzaStore.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from Cheese import Cheese
2+
from Olives import Olives
3+
from Pizza import Pizza
4+
from ThincrustPizza import ThincrustPizza
5+
6+
7+
class PizzaStore:
8+
@staticmethod
9+
def main(*args) -> None:
10+
pizza: Pizza = ThincrustPizza()
11+
cheesePizza: Pizza = Cheese(pizza)
12+
greekPizza: Pizza = Olives(cheesePizza)
13+
14+
print(f'{greekPizza.getDescription()} ${greekPizza.cost()}')
15+
16+
if __name__ == '__main__':
17+
PizzaStore.main()

decorator/pizza/ThickcrustPizza.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from Pizza import Pizza
2+
3+
4+
class ThickcrustPizza(Pizza):
5+
def __init__(self):
6+
self.description = "Thick crust pizza, with tomato sauce"
7+
8+
def cost(self) -> float:
9+
return 7.99

decorator/pizza/ThincrustPizza.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from Pizza import Pizza
2+
3+
4+
class ThincrustPizza(Pizza):
5+
def __init__(self):
6+
self.description = "Thin crust pizza, with tomato sauce"
7+
8+
def cost(self) -> float:
9+
return 7.99
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from abc import abstractmethod
2+
3+
from Pizza import Pizza
4+
5+
6+
class ToppingDecorator(Pizza):
7+
pizza: Pizza
8+
9+
@abstractmethod
10+
def getDescription(self) -> str:
11+
raise NotImplementedError

decorator/starbuzz/Beverage.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from abc import ABC, abstractmethod
2+
3+
4+
class Beverage(ABC):
5+
description: str = "Unknown Beverage"
6+
7+
def getDescription(self) -> str:
8+
return self.description
9+
10+
@abstractmethod
11+
def cost(self) -> float:
12+
raise NotImplementedError
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from abc import ABC, abstractmethod
2+
3+
from Beverage import Beverage
4+
5+
6+
class CondimentDecorator(Beverage):
7+
beverage: Beverage
8+
9+
@abstractmethod
10+
def getDescription(self) -> str:
11+
raise NotImplementedError

0 commit comments

Comments
 (0)