추상 클래스의 선언과 구현 그리고 생성자 주입
class Switchable(ABC):
@abstractmethod
def turn_on(self):
pass
@abstractmethod
def turn_off(self):
pass
class LightBulb(Switchable):
def turn_on(self):
print("Lightbulb: turned on")
def turn_off(self):
print("Lightbulb: turned off")
class Fan(Switchable):
def turn_on(self):
print("Fan: turned on")
def turn_off(self):
print("Fan: turned off")
class PowerSwitch:
def __init__(self, c: Switchable):
self.client = c
self.on = False
def press(self):
if self.on:
self.client.turn_off()
else:
self.client.turn_on()
self.on = True
myBulb = LightBulb()
myFan = Fan()
power_switch = PowerSwitch(myBulb)
fan_switch = PowerSwitch(myFan)
power_switch.press()
power_switch.press()
fan_switch.press()
fan_switch.press()
isinstance(myBulb, (Switchable)) #True
isinstance(myBulb, Switchable) #True