๊ฐ์ฒด์งํฅ์ธ์ด๊ฐ ๊ฐ์ ธ์ผํ๋ ์กฐ๊ฑด์ธ ์๋ฆฌ๋ ์์น์ ๋ํด ์์๋ณด๊ฒ ๋์,,
from abc import *
class ์ถ์ํด๋์ค์ด๋ฆ(metaclass=ABCMeta):
@abstractmethod
def ๋ฉ์๋์ด๋ฆ(self):
์ฝ๋
์์ ๊ฐ์ ๊ตฌ์ฑ์ผ๋ก ์ฌ์ฉ
์๋๋ ์์
from abc import *
class StudentBase(metaclass=ABCMeta):
@abstractmethod
def study(self):
pass
@abstractmethod
def go_to_school(self):
pass
class Student(StudentBase):
def study(self):
print('๊ณต๋ถํ๊ธฐ')
def go_to_school(self):
print('ํ๊ต๊ฐ๊ธฐ')
james = Student()
james.study()
james.go_to_school()
์ถ์ฒ
์ถ์ํด๋์ค๋ ์ค์ ๊ธฐ๋ฅ์ ๊ตฌํํ๋ ํด๋์ค๊ฐ ์๋๋ผ ์์์ ์ํ ํด๋์ค์ด๋ค. ์ถ์ ํด๋์ค์์ ํด๋น ํด๋์ค๊ฐ ๊ผญ ๊ฐ์ ธ์ผํ ๊ธฐ๋ฅ์ ๋ช
์ํด๋์ผ๋ฉด ์ด๋ฅผ ์์๋ฐ๋ ํด๋์ค๋ ๊ผญ ์์ํด๋์ค์ ๋ชจ๋ ๊ธฐ๋ฅ์ ๊ตฌํํด์ผํ๋ค.
์ธํฐํ์ด์ค:
์์
class Human:
"""์ถ์ํ ๊ณผ์ ์ด ํ์ํ Human ํด๋์ค"""
def __init__(self, name, sex):
self.name = name
self.sex = sex
def go_restroom(self):
"""ํ์ฅ์ค ๊ฐ๋ ํจ์"""
if(self.sex == '๋จ์'):
print("๋จ์ํ์ฅ์ค๋ก ๊ฐ๋ค.")
elif(self.sex == '์ฌ์'):
print("์ฌ์ํ์ฅ์ค๋ก ๊ฐ๋ค.")
else:
print("์ฑ๋ณ์ ์ง์ ํด์ฃผ์ธ์.")
์ถ์ฒ: https://doorbw.tistory.com/236 [Tigercow.Door]
์๋ ๋จ์ผ ์ฑ ์์์น์ด ์์ง์ผ์ง ์๋ชป๋ ์์
from abc import *
class HumanBase(metaclass=ABCMeta):
def __init__(self, name):
self.name = name
@abstractmethod
def go_restroom(self):
pass
class Male(HumanBase):
def __init__(self, name):
super().__init__(name)
self.sex = "๋จ์"
def go_restroom(self):
print("๋จ์ํ์ฅ์ค๋ก ๊ฐ๋ค.")
class Female(HumanBase):
def __init__(self, name):
super().__init__(name)
self.sex = "์ฌ์"
def go_restroom(self):
print("์ฌ์ํ์ฅ์ค๋ก ๊ฐ๋ค.")
์ถ์ฒ: https://doorbw.tistory.com/236 [Tigercow.Door]
์๋ ๋จ์ผ์ฑ ์์์น์ด ์ง์ผ์ง ์์
๊ธฐ์กด์ ์ฝ๋๋ฅผ ๋ณ๊ฒฝํ์ง ์๊ณ (Closed) ๊ธฐ๋ฅ์ ์์ ํ๊ฑฐ๋ ์ถ๊ฐํ ์ ์๋๋ก(Open) ์ค๊ณํด์ผ ํ๋ค.
ํ์ฅ์๋ ์ด๋ ค์๊ณ , ๋ณ๊ฒฝ์๋ ๋ซํ์์ด์ผ ํ๋ค.
์๋ก์ด ๊ธฐ๋ฅ์ ์ถ๊ฐํจ์ ์์ด์๋ ์ ๊ท ๊ธฐ๋ฅ์ ๋ํ ์์ค ์ถ๊ฐ๋ง ์งํํด์ผ ํ๊ณ ๊ธฐ์กด์ ์ฝ๋๋ฅผ ์์ ํด์ผ ํ๋ ์ผ์ ์์ด์ผ ํ๋ค๋ ๊ฒ์ด๋ค.
class Message:
"""Message ์ถ์ ํด๋์ค"""
def __init__(self, data):
self.data = data
class FirstGradeMessage(Message):
"""FirstGrade์ ๋ํ ๋ฉ์ธ์ง ์ฒ๋ฆฌ ํด๋์ค"""
class SecondGradeMessage(Message):
"""SecondGrade์ ๋ํ ๋ฉ์ธ์ง ์ฒ๋ฆฌ ํด๋์ค"""
class ThirdGradeMessage(Message):
"""ThirdGrade์ ๋ํ ๋ฉ์ธ์ง ์ฒ๋ฆฌ ํด๋์ค"""
class DefaultGradeMessage(Message):
"""DefaultGrade์ ๋ํ ๋ฉ์ธ์ง ์ฒ๋ฆฌ ํด๋์ค"""
class GradeMessageClassification():
"""Grade์ ๋ฐ๋ฅธ ๋ฉ์ธ์ง ๋ถ๋ฅ ํด๋์ค"""
def __init__(self, data):
self.data = data
def classification(self):
if(self.data['grade'] == 1):
return FirstGrade(self.data)
elif(self.data['grade'] == 2):
return SecondGrade(self.data)
elif(self.data['grade'] == 3):
return ThirdGrade(self.data)
else:
return DefaultGrade(self.data)
์ถ์ฒ: https://doorbw.tistory.com/237 [Tigercow.Door]
Message๋ผ๋ ์ถ์ํด๋์ค๋ฅผ ๊ฐ ํ๋ ํด๋์ค๊ฐ ์์๋ฐ๋ ํ์
GradeclassificationMessage์์ data๋ฅผ ๋ฐ์์ ๊ฐ ํ๋ ์ ๊ตฌ๋ถํ๊ณ ํด๋น ํ๋ ์ ํด๋์ค๋ฅผ ์ด์ฉํด์ ๋ฐ์ดํฐ ๋ฐํ
์ฌ๊ธฐ์ ๋ฌธ์ ์ ์ grade๊ฐ ๋์ด๋ ๊ฒฝ์ฐ GradeclassificationMessage์ ํจ์๋ฅผ ๋ฐ๊ฟ์ผ ํ๋ค.
OCP๊ฐ ์ง์ผ์ง์ง ์์๋ค๋ ๊ฒ์ด๋ค.
class Message:
"""Message ์ถ์ ํด๋์ค"""
def __init__(self, data):
self.data = data
@staticmethod
def is_collect_grade_message(data: dict):
return False
class FirstGradeMessage(Message):
"""FirstGrade์ ๋ํ ๋ฉ์ธ์ง ์ฒ๋ฆฌ ํด๋์ค"""
@staticmethod
def is_collect_grade_message(data: dict):
return data['grade'] == 1
class SecondGradeMessage(Message):
"""SecondGrade์ ๋ํ ๋ฉ์ธ์ง ์ฒ๋ฆฌ ํด๋์ค"""
@staticmethod
def is_collect_grade_message(data: dict):
return data['grade'] == 2
class ThirdGradeMessage(Message):
"""ThirdGrade์ ๋ํ ๋ฉ์ธ์ง ์ฒ๋ฆฌ ํด๋์ค"""
@staticmethod
def is_collect_grade_message(data: dict):
return data['grade'] == 3
class DefaultGradeMessage(Message):
"""DefaultGrade์ ๋ํ ๋ฉ์ธ์ง ์ฒ๋ฆฌ ํด๋์ค"""
class GradeMessageClassification():
"""Grade์ ๋ฐ๋ฅธ ๋ฉ์ธ์ง ๋ถ๋ฅ ํด๋์ค"""
def __init__(self, data):
self.data = data
def classification(self):
for grade_message_cls in Message.__subclasses__():
try:
if grade_message_cls.is_collect_grade_message(self.data):
return grade_message_cls(self.data)
except KeyError:
continue
return DefaultGradeMessage(self.data)
์ถ์ฒ: https://doorbw.tistory.com/237 [Tigercow.Door]
์ฐ์ ์ถ์ํด๋์ค์ธ Message๊ฐ ์๊ณ , ๊ฐ ํ๋ ํด๋์ค๋ Message๋ฅผ ์์๋ฐ๋๋ค.
๊ฐ ํ๋ ํด๋์ค๋ ๋ฐ์ ๋ฐ์ดํฐ๊ฐ ํด๋น ํ๋ ๊ฒ์ธ์ง True/False ๋ก ์๋ ค์ฃผ๋ is_collect_grade_message ๋ฉ์๋๊ฐ ์๋ค.
Message์ ๋ชจ๋ ํ์ํด๋์ค์ is_collect_grade_message๋ฅผ ์คํํด์ True์ธ ๋ฉ์๋์ ๋ฐ์ดํฐ ๋ฐํ!
# DIP๋ฅผ ์งํค์ง ๋ชปํ ์์
from abc import *
class EventStreamer():
def __init__(self, parsed_data: str, client: Syslog):
self.parsed_data = parsed_data
assert client is Syslog, "Client is not Syslog"
self.client = client
def stream(self):
self.client.send(self.parsed_data)
class Syslog():
def send(data: str):
print(f"Syslog send: {data}")
pass
class OtherClient():
def send(data: str):
print(f"OtherClient send: {data}")
pass
streamer1 = EventStreamer("for Syslog data!", Syslog)
streamer1.stream()
streamer2 = EventStreamer("for OtherClient data!", OtherClient)
streamer2.stream()
์ถ์ฒ: https://doorbw.tistory.com/240 [Tigercow.Door]
์์ ํด๋์ค์ธ EventStreamer์์ Syslog๋ฅผ ์ ์๋ฅผ ํด๋ฒ๋ฆฌ๋ฉด ์๋๋ค.
EventStreamer() ๋ผ๋ ๊ณ ์์ค์ ๋ชจ๋์ด ์ง์ Syslog๋ฅผ ์ฐธ์กฐํจ์ผ๋ก์จ, Syslog์ ๋ํด ์์กดํ๊ณ ์๋ค.
# DIP๋ฅผ ์ ์ฉํ ์์
from abc import *
class EventStreamer():
def __init__(self, parsed_data: str, client):
self.parsed_data = parsed_data
assert client in DataTargetClient.__subclasses__(), "Client is not DataTargetClient"
self.client = client
def stream(self):
self.client.send(self.parsed_data)
class DataTargetClient(metaclass=ABCMeta):
"""Interface: DataTargetClient class"""
@abstractmethod
def send(self, data: str):
pass
class Syslog(DataTargetClient):
def send(data: str):
print(f"Syslog send: {data}")
pass
class OtherClient(DataTargetClient):
def send(data: str):
print(f"OtherClient send: {data}")
pass
streamer1 = EventStreamer("for Syslog data!", Syslog)
streamer1.stream()
streamer2 = EventStreamer("for OtherClient data!", OtherClient)
streamer2.stream()
์ถ์ฒ: https://doorbw.tistory.com/240 [Tigercow.Door]