Skip to content

Instantly share code, notes, and snippets.

@VladislavSoren
Created August 27, 2024 08:14
Show Gist options
  • Select an option

  • Save VladislavSoren/130c3ec49c24a39b2662ee6f6ff1787d to your computer and use it in GitHub Desktop.

Select an option

Save VladislavSoren/130c3ec49c24a39b2662ee6f6ff1787d to your computer and use it in GitHub Desktop.
design_pattern_decorator_example
from abc import ABC, abstractmethod
class DataSource(ABC):
@abstractmethod
def write_data(self):
pass
@abstractmethod
def read_data(self):
pass
class FileDataSource(DataSource):
def write_data(self):
print("Data was recorded")
return "Data was recorded"
def read_data(self):
print("Data was read")
return "Data was read"
class DataSourceDecorator(ABC):
def __init__(self, wrapped):
self.wrapped = wrapped
@abstractmethod
def write_data(self):
pass
@abstractmethod
def read_data(self):
pass
class EncryptionDecorator(DataSourceDecorator):
def write_data(self):
print("EncryptionDecorator: write_data")
self.wrapped.write_data()
def read_data(self):
self.wrapped.read_data()
print("DeEncryptionDecorator: read_data")
class CompressionDecorator(DataSourceDecorator):
def write_data(self):
print("CompressionDecorator: write_data")
self.wrapped.write_data()
def read_data(self):
self.wrapped.read_data()
print("DeCompressionDecorator: read_data")
store = FileDataSource()
store = EncryptionDecorator(store)
store = CompressionDecorator(store)
store.write_data()
print()
store.read_data()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment