前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python 设计模式,“多”例模式

python 设计模式,“多”例模式

作者头像
用户1225216
发布2018-03-05 14:25:46
1K0
发布2018-03-05 14:25:46
举报
文章被收录于专栏:扎心了老铁扎心了老铁
代码语言:javascript
复制
版本1:一个账号不能同时是司机乘客。
 
 
#-*- coding:utf-8 -*-
'''
Created on 2016年8月2日
 
@author: yangfanholiday
'''
 
 
class User(object):
     
    __doc__ = '在不改变case代码情况下,防止重复login,不会被其他case登录顶掉(并发),类似单例模式的demo,核心代码是User类的__new__部分,将这部分复制到需要的类中即可'
     
    def __init__(self, phone, info):
        self.phone = phone
        self.info = info
 
 
    def __new__(cls, *args):
        phone = args[0]
        if not hasattr(cls, '_users'):
            cls._users = list()
            orig = super(User, cls)
            ob = orig.__new__(cls)
            cls._users.append(ob)
        elif not reduce(lambda x,y: x or y, map(lambda x:x.phone == phone, cls._users), False):
            orig = super(User, cls)
            ob = orig.__new__(cls)
            cls._users.append(ob)
        else:
            for u in cls._users:
                if u.phone == phone:
                    ob = u
        return ob
 
 
class Driver(User):
    def __init__(self, phone, info, say):
        self.say = say
        User.__init__(self, phone, info)            
 
 
class Passenger(User):
    def __init__(self, phone, info, say):
        self.say = say
        User.__init__(self, phone, info)
         
u1 = Driver('111', '还没有任何司机登录' , '我是司机')
print u1, u1.phone, u1.say, u1.info
 
 
u2 = Driver('222', '我还没哟登录', '我是司机')
print u2, u2.phone, u2.say, u2.info
 
 
u3 = Driver('222', '我已经登录了', '我是司机')
print u3,u3.phone,u3.say, u3.info
 
 
u4 = Passenger('444', '我还没有登录', '我是乘客')
print u4,u4.phone,u4.say, u4.info
 
 
u4 = Passenger('222', '我作为司机登录过了', '我是乘客')
print u4,u4.phone,u4.say, u4.info
 
 
输出:
<__main__.Driver object at 0x10de2ba10> 111 我是司机 还没有任何司机登录
<__main__.Driver object at 0x10de2ba50> 222 我是司机 我还没哟登录
<__main__.Driver object at 0x10de2ba50> 222 我是司机 我已经登录了
<__main__.Passenger object at 0x10de2bad0> 444 我是乘客 我还没有登录
<__main__.Driver object at 0x10de2ba50> 222 我是司机 我已经登录了
 
 
 
 
 
 
版本2:一个账号可以登录司机乘客各一次
#-*- coding:utf-8 -*-
'''
Created on 2016年8月2日
 
@author: yangfanholiday
'''
 
 
class User(object):
         
    __doc__ = '在不改变case代码情况下,防止重复login,不会被其他case登录顶掉(并发),类似单例模式的demo,核心代码是User类的__new__部分,将这部分复制到需要的类中即可'
     
    def __init__(self, phone, info):
        self.phone = phone
        self.info = info
 
 
class Driver(User):
    def __init__(self, phone, info, say):
        self.say = say
        User.__init__(self, phone, info)            
 
 
    def __new__(cls, *args):
        phone = args[0]
        if not hasattr(cls, '_users'):
            cls._users = list()
            orig = super(User, cls)
            ob = orig.__new__(cls)
            cls._users.append(ob)
        elif not reduce(lambda x,y: x or y, map(lambda x:x.phone == phone, cls._users), False):
            orig = super(User, cls)
            ob = orig.__new__(cls)
            cls._users.append(ob)
        else:
            for u in cls._users:
                if u.phone == phone:
                    ob = u
        return ob
     
class Passenger(User):
    def __init__(self, phone, info, say):
        self.say = say
        User.__init__(self, phone, info)
     
    def __new__(cls, *args):
        phone = args[0]
        if not hasattr(cls, '_users'):
            cls._users = list()
            orig = super(User, cls)
            ob = orig.__new__(cls)
            cls._users.append(ob)
        elif not reduce(lambda x,y: x or y, map(lambda x:x.phone == phone, cls._users), False):
            orig = super(User, cls)
            ob = orig.__new__(cls)
            cls._users.append(ob)
        else:
            for u in cls._users:
                if u.phone == phone:
                    ob = u
        return ob   
u1 = Driver('111', '还没有任何司机登录' , '我是司机')
print u1, u1.phone, u1.say, u1.info
 
 
u2 = Driver('222', '我还没哟登录', '我是司机')
print u2, u2.phone, u2.say, u2.info
 
 
u3 = Driver('222', '我已经登录了', '我是司机')
print u3,u3.phone,u3.say, u3.info
 
 
u4 = Passenger('444', '我还没有登录', '我是乘客')
print u4,u4.phone,u4.say, u4.info
 
 
u4 = Passenger('222', '我作为司机登录过了', '我是乘客')
print u4,u4.phone,u4.say, u4.info
 
 
 
 
输入:
<__main__.Driver object at 0x105e23ad0> 111 我是司机 还没有任何司机登录
 
<__main__.Driver object at 0x105e2fc10> 222 我是司机 我还没哟登录
 
<__main__.Driver object at 0x105e2fc10> 222 我是司机 我已经登录了
 
<__main__.Passenger object at 0x105e2fc50> 444 我是乘客 我还没有登录
 
<__main__.Passenger object at 0x105e2fcd0> 222 我是乘客 我作为司机登录过了
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017-07-30 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档