Last active
August 29, 2015 14:04
-
-
Save dstanek/bd4a642f694de01ef9a9 to your computer and use it in GitHub Desktop.
DI by hand
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
import abc | |
class Logger(object): | |
__metaclass__ abc.ABCMeta | |
@abstractmethod | |
def log_it(self, message, *args, **kwargs): | |
raise NotImplemented | |
class FileLogger(Logger): | |
def __init__(self, filename): | |
self.filename = filename | |
def log_it(self, message, *args, **kwargs): | |
print 'logging to a file' | |
class EmailLogger(Logger): | |
def __init__(self, emailer): | |
self.emailer = emailer | |
def log_it(self, message, *args, **kwargs): | |
print 'logging to an email' | |
class Emailer(object): | |
def __init__(self, server): | |
self.server = server | |
def send(self, message): | |
pass | |
class DomainObject(object): | |
def __init__(self, logger): | |
self.logger = logger | |
def operation(self): | |
self.logger.log_it('message') | |
d0 = DomainObject(logger=FileLogger('test.log')) | |
d1 = DomainObject(logger=EmailLogger(Emailer('server')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment