"Mastering Python Design Patterns" learning structure-based appearance patterns

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.