Advantage
Decorator Pattern
@ Decorator
Three Models
Options
from abc import ABCMeta, abstractproperty
class ICar(metaclass=ABCMeta):
@abstractproperty
def description(self):
pass
@abstractproperty
def cost(self):
pass
class Economy(ICar):
@property
def description(self):
return 'Economy'
@property
def cost(self):
return 12000.00
class IDecorator(ICar):
def __init__(self, car):
self._car = car
@property
def car(self):
return self._car
class V6(IDecorator):
@property
def description(self):
return self.car.description + ', V6'
@property
def cost(self):
return self.car.cost + 1200.00
class BlackPaint(IDecorator):
@property
def description(self):
return self.car.description + ', Black Paint'
@property
def cost(self):
return self.car.cost + 2000.00
class Vinyl(IDecorator):
@property
def description(self):
return self.car.description + ', Vinyl Interior'
@property
def cost(self):
return self.car.cost + 4000.00
car = Economy()
print(f'{car.description}: {car.cost}$')
car = BlackPaint(car)
print(f'{car.description}: {car.cost}$')
car = V6(car)
print(f'{car.description}: {car.cost}$')
car = Vinyl(car)
print(f'{car.description}: {car.cost}$')
Economy: 12000.0$ Economy, Black Paint: 14000.0$ Economy, Black Paint, V6: 15200.0$ Economy, Black Paint, V6, Vinyl Interior: 19200.0$