Last active
May 23, 2023 17:03
-
-
Save umaparvat/c26e9774374095860ca9ead82008fe30 to your computer and use it in GitHub Desktop.
Interface segregation principle violation
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
| from abc import ABC, abstractmethod | |
| import sqlalchemy as db | |
| from sqlalchemy.orm import sessionmaker | |
| engine = db.create_engine("sqlite:///example.db") | |
| Session = sessionmaker(bind=engine) | |
| session = Session() | |
| class DatabaseService: | |
| def insert(self, data): | |
| with session as ses: | |
| ses.insert(data) | |
| def delete(self, condition): | |
| with session as ses: | |
| ses.delete(condition) | |
| def read(self, condition): | |
| with session as ses: | |
| data = ses.query(condition).all() | |
| return data | |
| def update(self, data, condition): | |
| with session as ses: | |
| ses.update(data, condition) | |
| class DataReader(DatabaseService): | |
| def read(self, condition): | |
| super().read(condition) | |
| def insert(self, data): | |
| raise NotImplementedError | |
| def update(self, data, condition): | |
| raise NotImplementedError | |
| def delete(self, condition): | |
| raise NotImplementedError | |
| # client code | |
| ds = DatabaseService() | |
| ds.insert({"order_id":1, "order_item": "pen"}) | |
| ds.read(condition="order_id=1") | |
| reader = DataReader() | |
| reader.read(condition="order_id=1") | |
| reader.insert({"order_id":2, "order_item": "pencil"}) # this will throw error |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment