首页 > 其他 > 详细

设计模式,代理模式(9)

时间:2018-03-08 01:25:18      阅读:239      评论:0      收藏:0      [点我收藏+]

在某些应用中,我们想要在访问某个对象之前执行一个或多个重要的操作,例如,访问敏感
信息——在允许用户访问敏感信息之前,我们希望确保用户具备足够的权限。操作系统中也存在
类似的情况,用户必须具有管理员权限才能在系统中安装新程序。
上面提到的重要操作不一定与安全问题相关。延迟初始化
是另一个案例:我们想要把一个计算成本较高的对象的创建过程延迟到用户首次真正使用它时
才进行。

 

在代理模式(Proxy Pattern)中,一个类代表另一个类的功能。这种类型的设计模式属于结构型模式。

在代理模式中,我们创建具有现有对象的对象,以便向外界提供功能接口。

 

# coding: utf-8


class SensitiveInfo:

    def __init__(self):
        self.users = [‘nick‘, ‘tom‘, ‘ben‘, ‘mike‘]

    def read(self):
        print(‘There are {} users: {}‘.format(len(self.users), ‘ ‘.join(self.users)))

    def add(self, user):
        self.users.append(user)
        print(‘Added user {}‘.format(user))


class Info:

    ‘‘‘SensitiveInfo的保护代理‘‘‘

    def __init__(self):
        self.protected = SensitiveInfo()
        self.secret = ‘0xdeadbeef‘

    def read(self):
        self.protected.read()

    def add(self, user):
        sec = input(‘what is the secret? ‘)
        self.protected.add(user) if sec == self.secret else print("That‘s wrong!")


def main():
    info = Info()
    while True:
        print(‘1. read list |==| 2. add user |==| 3. quit‘)
        key = input(‘choose option: ‘)
        if key == ‘1‘:
            info.read()
        elif key == ‘2‘:
            name = input(‘choose username: ‘)
            info.add(name)
        elif key == ‘3‘:
            exit()
        else:
            print(‘unknown option: {}‘.format(key))

if __name__ == ‘__main__‘:
    main()

 

设计模式,代理模式(9)

原文:https://www.cnblogs.com/ydf0509/p/8525970.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!