-
-
Save harshkumarchourasia/aaef3f58776424bc10d54923f9884c66 to your computer and use it in GitHub Desktop.
SOLID EXAMPLES FROM https://www.pythontutorial.net/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# dependency inversion principle | |
########################################## | |
class FXConverter: | |
def convert(self, from_currency, to_currency, amount): | |
print(f'{amount} {from_currency} = {amount * 1.2} {to_currency}') | |
return amount * 1.2 | |
class App: | |
def start(self): | |
converter = FXConverter() | |
converter.convert('EUR', 'USD', 100) | |
if __name__ == '__main__': | |
app = App() | |
app.start() | |
########################################## | |
from abc import ABC | |
class CurrencyConverter(ABC): | |
def convert(self, from_currency, to_currency, amount) -> float: | |
pass | |
class FXConverter(CurrencyConverter): | |
def convert(self, from_currency, to_currency, amount) -> float: | |
print('Converting currency using FX API') | |
print(f'{amount} {from_currency} = {amount * 1.2} {to_currency}') | |
return amount * 1.15 | |
class AlphaConverter(CurrencyConverter): | |
def convert(self, from_currency, to_currency, amount) -> float: | |
print('Converting currency using Alpha API') | |
print(f'{amount} {from_currency} = {amount * 1.2} {to_currency}') | |
return amount * 1.2 | |
class App: | |
def __init__(self, converter: CurrencyConverter): | |
self.converter = converter | |
def start(self): | |
self.converter.convert('EUR', 'USD', 100) | |
if __name__ == '__main__': | |
converter = AlphaConverter() | |
app = App(converter) | |
app.start() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# interface segregation principle | |
########################################## | |
from abc import ABC, abstractmethod | |
class Vehicle(ABC): | |
@abstractmethod | |
def go(self): | |
pass | |
@abstractmethod | |
def fly(self): | |
pass | |
class Aircraft(Vehicle): | |
def go(self): | |
print("Taxiing") | |
def fly(self): | |
print("Flying") | |
class Car(Vehicle): | |
def go(self): | |
print("Going") | |
def fly(self): | |
raise Exception('The car cannot fly') | |
########################################## | |
class Movable(ABC): | |
@abstractmethod | |
def go(self): | |
pass | |
class Flyable(Movable): | |
@abstractmethod | |
def fly(self): | |
pass | |
class Aircraft(Flyable): | |
def go(self): | |
print("Taxiing") | |
def fly(self): | |
print("Flying") | |
class Car(Movable): | |
def go(self): | |
print("Going") | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Liskov substitution principle | |
########################################## | |
from abc import ABC, abstractmethod | |
class Notification(ABC): | |
@abstractmethod | |
def notify(self, message, email): | |
pass | |
class Email(Notification): | |
def notify(self, message, email): | |
print(f'Send {message} to {email}') | |
class SMS(Notification): | |
def notify(self, message, phone): | |
print(f'Send {message} to {phone}') | |
if __name__ == '__main__': | |
notification = SMS() | |
notification.notify('Hello', '[email protected]') | |
########################################## | |
from abc import ABC, abstractmethod | |
class Notification(ABC): | |
@abstractmethod | |
def notify(self, message): | |
pass | |
class Email(Notification): | |
def __init__(self, email): | |
self.email = email | |
def notify(self, message): | |
print(f'Send "{message}" to {self.email}') | |
class SMS(Notification): | |
def __init__(self, phone): | |
self.phone = phone | |
def notify(self, message): | |
print(f'Send "{message}" to {self.phone}') | |
class Contact: | |
def __init__(self, name, email, phone): | |
self.name = name | |
self.email = email | |
self.phone = phone | |
class NotificationManager: | |
def __init__(self, notification): | |
self.notification = notification | |
def send(self, message): | |
self.notification.notify(message) | |
if __name__ == '__main__': | |
contact = Contact('John Doe', '[email protected]', '(408)-888-9999') | |
sms_notification = SMS(contact.phone) | |
email_notification = Email(contact.email) | |
notification_manager = NotificationManager(sms_notification) | |
notification_manager.send('Hello John') | |
notification_manager.notification = email_notification | |
notification_manager.send('Hi John') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# open-closed principle | |
########################################## | |
class Person: | |
def __init__(self, name): | |
self.name = name | |
def __repr__(self): | |
return f'Person(name={self.name})' | |
class PersonStorage: | |
def save_to_database(self, person): | |
print(f'Save the {person} to database') | |
def save_to_json(self, person): | |
print(f'Save the {person} to a JSON file') | |
if __name__ == '__main__': | |
person = Person('John Doe') | |
storage = PersonStorage() | |
storage.save_to_database(person) | |
########################################## | |
from abc import ABC, abstractmethod | |
class Person: | |
def __init__(self, name): | |
self.name = name | |
def __repr__(self): | |
return f'Person(name={self.name})' | |
class PersonStorage(ABC): | |
@abstractmethod | |
def save(self, person): | |
pass | |
class PersonDB(PersonStorage): | |
def save(self, person): | |
print(f'Save the {person} to database') | |
class PersonJSON(PersonStorage): | |
def save(self, person): | |
print(f'Save the {person} to a JSON file') | |
class PersonXML(PersonStorage): | |
def save(self, person): | |
print(f'Save the {person} to a XML file') | |
if __name__ == '__main__': | |
person = Person('John Doe') | |
storage = PersonXML() | |
storage.save(person) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Single responsibility Principle | |
########################################## | |
class Person: | |
def __init__(self, name): | |
self.name = name | |
def __repr__(self): | |
return f'Person(name={self.name})' | |
@classmethod | |
def save(cls, person): | |
print(f'Save the {person} to the database') | |
if __name__ == '__main__': | |
p = Person('John Doe') | |
Person.save(p) | |
########################################## | |
class PersonDB: | |
def save(self, person): | |
print(f'Save the {person} to the database') | |
class Person: | |
def __init__(self, name): | |
self.name = name | |
self.db = PersonDB() | |
def __repr__(self): | |
return f'Person(name={self.name})' | |
def save(self): | |
self.db.save(person=self) | |
if __name__ == '__main__': | |
p = Person('John Doe') | |
p.save() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment