This is something that I have used in my work.
is on the real implementation layer, and then encapsulates the invocation of a function.
This allows for a real implementation of the inner layer function,
and the outer call function is open,
Isolate the variability inside and outside.
fromEnumImportEnum fromAbcImportAbcmeta, Abstractmethodstate= Enum (' State','new running sleeping restart zombie')classUser:PassclassProcess:PassclassFile:PassclassServer (metaclass=Abcmeta): @abstractmethoddef __init__(self):Pass def __str__(self):returnself.name @abstractmethoddefboot (self):Pass@abstractmethoddefKill (Self, restart=True):Passclassfileserver (Server):def __init__(self): Self.name='Fileserver'self.state=state.newdefboot (self):Print('booting the {}'. Format ( self) self.state=state.runningdefKill (Self, restart=True):Print('killing {}'. Format ( self) self.state= State.restartifRestartElseState.zombiedefcreate_file (self, user, name, permissions):Print("trying to create the file ' {} ' for user ' {} ' with permissions ' {} '". Format (name, user, permissions))classProcessserver (Server):def __init__(self): Self.name='Processserver'self.state=state.newdefboot (self):Print('booting the {}'. Format ( self) self.state=state.runningdefKill (Self, restart=True):Print('killing {}'. Format ( self) self.state= Stae.restartifRestartElseState.zobmiedefcreate_process (self, user, name):Print("trying to create process ' {} ' for user ' {} '". Format (name, user)classWindowsServer:PassclassNetworkserver:PassclassOperatingSystem:def __init__(self): Self.fs=fileserver () self.ps=Processserver ()defStart (self): [I.boot () forIinch(Self.fs, self.ps)]defcreate_file (self, user, name, permissions):returnself.fs.create_file (user, name, permissions)defcreate_process (self, user, name):returnself.ps.create_process (user, name)defMain (): OS=OperatingSystem () Os.start () Os.create_file ('Foo','Hello','-rw-r-r') os.create_process ('Bar','ls/tmp')if __name__=='__main__': Main ()
"Mastering Python Design Patterns" learning structure-based appearance patterns