python外观模式

python外观模式

外观模式隐藏了内部对象的复杂性,向外提供了简单的一致性接口

from enum import Enum
from abc import ABCMeta, abstractmethod

State = Enum('State', 'new running sleeping restart zombie')


class User:
    pass


class Process:
    pass


class File:
    pass


class Server(metaclass=ABCMeta):
    @abstractmethod
    def __init__(self):

        pass

    def __str__(self):

        return self.name

    @abstractmethod
    def boot(self):

        pass

    @abstractmethod
    def kill(self, restart=True):

        pass


class FileServer(Server):
    def __init__(self):

        '''初始化文件服务进程要求的操作'''
        self.name = 'FileServer'
        self.state = State.new

    def boot(self):

        print('booting the {}'.format(self))
        '''启动文件服务进程要求的操作'''
        self.state = State.running

    def kill(self, restart=True):

        print('Killing {}'.format(self))
        '''终止文件服务进程要求的操作'''
        self.state = State.restart if restart else State.zombie

    def create_file(self, user, name, permissions):

        '''检查访问权限的有效性、用户权限等'''
        print("trying to create the file '{}' for user '{}' with permissions{}".format(name, user, permissions))

class ProcessServer(Server):

    def __init__(self):

        '''初始化进程服务进程要求的操作'''
        self.name = 'ProcessServer'
        self.state = State.new

    def boot(self):

        print('booting the {}'.format(self))
        '''启动进程服务进程要求的操作'''
        self.state = State.running

    def kill(self, restart=True):

        print('Killing {}'.format(self))
        '''终止进程服务进程要求的操作'''
        self.state = State.restart if restart else State.zombie

    def create_process(self, user, name):

        '''检查用户权限和生成PID等'''
        print("trying to create the process '{}' for user '{}'".format(name, user))


class WindowServer:
    pass


class NetworkServer:
    pass


class OperatingSystem:
    '''外观'''

    def __init__(self):

        self.fs = FileServer()
        self.ps = ProcessServer()

    def start(self):

        [i.boot() for i in (self.fs, self.ps)]

    def create_file(self, user, name, permissions):

        return self.fs.create_file(user, name, permissions)

    def create_process(self, user, name):

        return self.ps.create_process(user, name)


def main():
    os = OperatingSystem()
    os.start()
    os.create_file('foo', 'hello', '-rw-r-r')
    os.create_process('bar', 'ls /tmp')


if __name__ == '__main__':
    main()

python外观模式_第1张图片

你可能感兴趣的:(数据结构与算法,python,设计模式)