python 设计模式,“多”例模式
版本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 我是乘客 我作为司机登录过了