0% found this document useful (0 votes)
4 views3 pages

SOLID Checklist Python DonGian

The document outlines the SOLID principles of object-oriented design in Python, providing examples of correct and incorrect implementations for each principle. It covers the Single Responsibility Principle (SRP), Open/Closed Principle (OCP), Liskov Substitution Principle (LSP), Interface Segregation Principle (ISP), and Dependency Inversion Principle (DIP). Each principle is illustrated with code snippets to demonstrate best practices and common pitfalls.

Uploaded by

huythua0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

SOLID Checklist Python DonGian

The document outlines the SOLID principles of object-oriented design in Python, providing examples of correct and incorrect implementations for each principle. It covers the Single Responsibility Principle (SRP), Open/Closed Principle (OCP), Liskov Substitution Principle (LSP), Interface Segregation Principle (ISP), and Dependency Inversion Principle (DIP). Each principle is illustrated with code snippets to demonstrate best practices and common pitfalls.

Uploaded by

huythua0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Checklist Python theo SOLID - D■ Hi■u & Có Ví D■

S – Single Responsibility Principle (SRP)


M■t class/hàm ch■ nên làm m■t vi■c duy nh■t.

■ Ví d■ ■úng:
class User:
def __init__(self, name, email):
self.name = name
self.email = email

class EmailSender:
def send_email(self, email, subject, body):
print(f"Sending email to {email}")

■ Ví d■ sai:
class User:
def __init__(self, name, email):
self.name = name
self.email = email

def send_email(self, subject, body):


print(f"Sending email to {self.email}")

O – Open/Closed Principle (OCP)


Có th■ m■ r■ng mà không ph■i ch■nh s■a code c■.

■ Ví d■ ■úng:
class Greeting:
def say_hello(self): return "Hello!"

class SpanishGreeting(Greeting):
def say_hello(self): return "¡Hola!"

class Greeter:
def greet(self, greeting):
print(greeting.say_hello())

■ Ví d■ sai:
class Greeter:
def greet(self, language):
if language == "en": print("Hello!")
elif language == "es": print("¡Hola!")

L – Liskov Substitution Principle (LSP)


Subclass thay th■ ■■■c class cha mà không l■i.

■ Ví d■ ■úng:
class Animal:
def make_sound(self): print("Some sound")

class Dog(Animal):
def make_sound(self): print("Bark")

■ Ví d■ sai:
class Fish(Animal):
def make_sound(self): raise Exception("Fish can't make sound")

I – Interface Segregation Principle (ISP)


Không ép object implement method không c■n thi■t.

■ Ví d■ ■úng:
class Printer:
def print(self): pass

class Scanner:
def scan(self): pass

class SimplePrinter(Printer):
def print(self): print("Printing only")

■ Ví d■ sai:
class Machine:
def print(self): pass
def scan(self): pass

class SimplePrinter(Machine):
def print(self): print("Printing")
def scan(self): raise NotImplementedError()

D – Dependency Inversion Principle (DIP)


Không ph■ thu■c vào class c■ th■, ch■ ph■ thu■c vào abstraction.

■ Ví d■ ■úng:
class MessageSender:
def send(self, message): pass

class EmailSender(MessageSender):
def send(self, message): print(f"Sending email: {message}")

class Notification:
def __init__(self, sender):
self.sender = sender
def notify(self):
self.sender.send("Hello!")

■ Ví d■ sai:
class Notification:
def __init__(self):
self.sender = EmailSender()
def notify(self):
self.sender.send("Hello!")

You might also like