Created at : 2024-04-11 03:35
Auther: Soo.Y
SOLID μμΉμ΄λ
SOLID μμΉμ κ°μ²΄ μ§ν₯ νλ‘κ·Έλλ°κ³Ό λμμΈμμ μ€μν 5κ°μ§ μμΉμ λνλΈ μμΉμ λλ€. μ΄ μμΉλ€μ μννΈμ¨μ΄λ₯Ό λ μ΄ν΄νκΈ° μ½κ³ , μ μ§λ³΄μνκΈ° μ½κ³ νμ₯ κ°λ₯νκ² λ§λλλ° λμμ΄ λ©λλ€. SOILDλ λ€μκ³Ό κ°μ μμΉλ€μ μ½μμ λλ€.
- Single Responsibility Principle (λ¨μΌ μ± μ μμΉ)
- Open/Closed Principle (κ°λ°©/νμ μμΉ)
- Liskov Substitution Principle (리μ€μ½ν μΉν μμΉ)
- Interface Segregation Principle (μΈν°νμ΄μ€ λΆλ¦¬ μμΉ)
- Dependency Inversion Principle (μμ‘΄μ± μμ μμΉ)
S (Single Responsibility Principle)
λ¨μΌ μ± μ μμΉμ ν΄λμ€κ° νλμ μ± μλ§μ κ°μ ΈμΌ νλ€λ μμΉμ λλ€. μ¬κΈ°μ λ§νλ μ± μμ βλ³κ²½λμ΄μΌ νλ μ΄μ β λΌκ³ νλλ° μ΄λ»κ² 보면 κΈ°λ₯μ΄λΌκ³ μ΄ν΄νλ©΄ μ½λ€κ³ μκ°νλ€.
Loggerλ λ‘κ·Έλ₯Ό μΆλ ₯νλ κΈ°λ₯λ§ κ°μ§κ³ μκ³ EmailSenderλ μ΄λ©μΌμ μ μ‘νλ κΈ°λ₯λ§ λ³΄μ νκ³ μλ€. Orderλ μ£Όλ¬Έμ μ²λ¦¬νλ κΈ°λ₯λ§ λ³΄μ νκ³ μλ€. μ¬κΈ°μ __init__
μμ Loggerμ EmailSender ν΄λμ€μ μΈμ€ν΄μ€λ₯Ό μμ±ν΄μ κΈ°λ₯μ μ¬μ©ν λΏ Logger λ° EmailSenderμ κΈ°λ₯μ λ¬Έμ λ₯Ό λ΄λΉνμ§ μλλ€. κ·Έλμ Loggerμ κΈ°λ₯μ΄ λ¬Έμ κ° λ°μνλ©΄ Logger ν΄λμ€μμλ§ μ€λ₯λ₯Ό μμ νλ©΄ λλ€.
class Logger:
"""λ‘κ·Έ κΈ°λ‘ μ±
μλ§μ κ°μ§"""
def log(self, message: str):
print(f"Log: {message}")
class EmailSender:
"""μ΄λ©μΌ μ μ‘ μ±
μλ§μ κ°μ§"""
def send_email(self, to: str, content: str):
print(f"Sending email to {to}: {content}")
class Order:
"""μ£Όλ¬Έ κ΄λ ¨ μ±
μμ κ°μ§. λ‘κΉ
κ³Ό μ΄λ©μΌ μ μ‘μ μΈλΆ μλΉμ€λ₯Ό μ΄μ©"""
def __init__(self):
self.logger = Logger()
self.email_sender = EmailSender()
def process_order(self, user_email: str):
# μ£Όλ¬Έ μ²λ¦¬ λ‘μ§
self.logger.log("Order processed.")
self.email_sender.send_email(user_email, "Your order has been processed.")
# μ¬μ© μμ
order = Order()
order.process_order("user@example.com")
O (Open-Closed Principle)
κ°λ°©-νμ μμΉμ μννΈμ¨μ΄ κ°μ²΄(ν΄λμ€, λͺ¨λ, ν¨μ λ±)λ νμ₯μ λν΄μλ μ΄λ € μμ΄μΌ νκ³ μμ μ λν΄μλ λ«ν μμ΄μΌ νλ€λ μμΉμ λλ€.
λΆλͺ¨ ν΄λμ€μΈ Reportκ° CSVλ₯Ό μ²λ¦¬νλ report ν΄λμ€, JSONμ μ²λ¦¬νλ report ν΄λμ€λ‘ λ€μνκ² λ§λ€ μ μλ€.
from abc import ABC, abstractmethod
class Report(ABC):
@abstracmethod
def generate(self):
pass
class CSVReport(Report):
def generate(self):
"""csv reportλ₯Ό μ²λ¦¬νλ λ‘μ§"""
return "CSV report data"
class JSONReport(Report):
def generate(self):
"""JSON reportλ₯Ό μ²λ¦¬νλ λ‘μ§"""
return "JSON report data"
μ’ λ 볡μ‘ν μμλ₯Ό μ΄ν΄λ³΄μ. π€ Discount μΆμ ν΄λμ€λ₯Ό μ μΈνκ³ λͺ¨λ ν μΈμ κΈ°λ³Έμ μ μνλ©° μ€μ ν μΈ λ‘μ§μ μ΄λ₯Ό μμλ°λ ꡬ체μ μΈ ν΄λμ€(NoDiscount, PercentageDiscount, FixedDiscount)μμ ꡬνλ μμμ λλ€. Product ν΄λμ€λ ν μΈ κ°μ²΄λ₯Ό λ°μμ ν μΈλ κ°κ²©μ κ³μ°ν©λλ€. μ΄ λ°©μμΌλ‘ μλ‘μ΄ ν μΈ μ νμ μΆκ°νκ³ μΆμ λ Discountλ₯Ό μμ λ°λ μλ‘μ΄ ν΄λμ€λ₯Ό λ§λ€κΈ°λ§ νλ©΄ λλ―λ‘, κΈ°μ‘΄ μ½λλ₯Ό λ³κ²½ν νμ μμ΄ νμ₯ν μ μμ΅λλ€.
from abc import ABC, abstractmethod
class Discount(ABC):
@abstractmethod
def apply(self, price: float) -> float:
pass
class NoDiscount(Discount):
def apply(self, price: float) -> float:
return price
class PercentageDiscount(Discount):
def __init__(self, percentage: float):
self.percentage = percentage
def apply(self, price: float) -> float:
return price - (price * self.percentage / 100)
class FixedDiscount(Discount):
def __init__(self, discount: float):
self.discount = discount
def apply(self, price: float) -> float:
return max(0, price - self.discount)
class Product:
def __init__(self, name: str, price: float, discount: Discount):
self.name = name
self.price = price
self.discount = discount
def price_after_discount(self):
return self.discount.apply(self.price)
# μ¬μ© μμ
no_discount_product = Product("Product with no discount", 100.0, NoDiscount())
percentage_discount_product = Product("Product with 10% discount", 100.0, PercentageDiscount(10))
fixed_discount_product = Product("Product with $20 discount", 100.0, FixedDiscount(20))
print(no_discount_product.price_after_discount())
print(percentage_discount_product.price_after_discount())
print(fixed_discount_product.price_after_discount())
L (Liskov Substitution Principle)
리μ€μ½ν μΉν μμΉμ νλ‘κ·Έλ¨μ΄ μ νμ±μ μμ§ μμΌλ©΄μ νμ νμ μ μΈμ€ν΄μ€λ₯Ό μμ νμ κ°μ²΄λ‘ μΉνν μ μμ΄μΌ νλ€λ μμΉμ λλ€.
I (Interface Segregation Principle)
μΈν°νμ΄μ€ λΆλ¦¬ μμΉμ ν΄λΌμ΄μΈνΈκ° μμ μ΄ μ΄μ©νμ§ μλ λ©μλμ μμ‘΄νμ§ μμμΌ νλ€λ μμΉμ λλ€.
D (Dependency Inversion Principle)
μμ‘΄ μμ μμΉμ μμ λͺ¨λμ΄ νμ λͺ¨λμ μμ‘΄νλ©΄ μ λλ©°, λ λ€ μΆμνμ μμ‘΄ν΄μΌ νλ€λ μμΉμ λλ€.