- 1.__new__(cls) 默认调用object的new 方法
- 使用该方法创建当前实例对象,一般不重写,就算重写
- 也是调用父类的__new__方法,注意:一定要返回创建的对象
- 如果没有返回,__init__方法将不会被Python解释器调用
- cls是类对象
- __init__有一个参数self,就是这个__new__返回的实例,__init__在__new__的基础上可以完成一些其它初始化的动作,__init__不需要返回值。
- 2.设计模式 设计模式是软件开发人员在软件开发过程中面临的一般问题的解决方案。
- 1.工厂模式
- 工厂模式:
- 1.简单工厂模式: 根据参数,造对象 缺点:类型不能扩展
- 2.工厂方法模式: 一个类别对应一个工厂, 好处:1.可以产生多种类型 2.如果有需要修改的类别,对其他的不会有影响
- 工厂模式:
- shoe ---> 皮鞋 球鞋 布鞋 ---->工厂
- # 鞋 父类
- class Shoe:
- def walk(self):
- pass
- def walk(self):
- class LeatherShoe(Shoe):
- def walk(self):
- print("穿着亮亮的皮鞋,自信满满上班去了....")
- def walk(self):
- class SoccerShoe(Shoe):
- def walk(self):
- print("穿着白白球鞋,去踢球.....")
- def walk(self):
- # 定义工厂
- class ShoeFactory:
- def make_shoe(self):
- pass
- def make_shoe(self):
- class LeatherShoeFactory(ShoeFactory):
- # 重写父类的make_shoe
- def make_shoe(self):
- return LeatherShoe()
- class SoccerShoeFactory(ShoeFactory):
- def make_shoe(self):
- return SoccerShoe()
- def make_shoe(self):
- class Store:
- def sell(self,shoe_type):
- if shoe_type=="皮鞋":
- self.factory=LeatherShoeFactory()
- elif shoe_type=="球鞋":
- self.factory=SoccerShoeFactory()
- if shoe_type=="皮鞋":
- def sell(self,shoe_type):
- store=Store()
- store.sell("皮鞋")
- store.factory.make_shoe().walk()
- 2.单例模式
- 即一个类只有一个对象实例。(创建的对象id相同)
- class Singleton:
- # 类属性 私有
- __instance=None
- # 控制__init__的执行
- __init__flag=False
- # 重写__new__
- def __new__(cls,name): # cls 就是Singleton对象
- if cls.__instance==None:
- cls.__instance=object.__new__(cls)
- return cls.__instance
- else:
- return cls.__instance
- if cls.__instance==None:
- def __init__(self,name):
- if not self.__init__flag:
- self.name=name
- print("-------init-----------",name)
- # 记住改变标志
- self.__init__flag=True
- if not self.__init__flag:
- s1=Singleton("aa")
- print(id(s1))
- print(s1.name)
- s2=Singleton("bb")
- print(id(s2))
- print(s2.name)
- s3=Singleton("cc")
- print(id(s3))
- print(s3.name)
- 1.工厂模式
- 3.代理模式
- 代理模式一般涉及到的角色有:
- 抽象角色:声明真实对象和代理对象的共同基类(接口);
- 代理角色:代理对象角色内部含有对真实对象的引用,从而可以操作真实对象,同时代理对象提供与真实对象相同的基类(接口)以便在任何时刻都能代替真实对象。
- 同时,代理对象可以在执行真实对象操作时,附加其他的操作,相当于对真实对象进行封装。
- 真实角色:代理角色所代表的真实对象,是我们最终要引用的对象。
- 步骤: 隐藏一部分类信息,使用的时候直接招代理 类似(中介)
- 1. 三要素: 抽象角色 真实角色 代理角色
- 2. 代理跟真实角色之间是如何关联:
- 代理类中存在一个真实角色属性
- 3.代理来说:更换真实角色
- 在代理类中定义了set_xxx(self,真实对象)
- 4.创建代理对象,代理对象调用方法.而调用方法的时候就去找真实角色
- 优点: 隐藏信息
- 缺点: 如果应用代理比较多的情况下,会降低了速度
- 抽象角色: 基类 父类
- 代理角色: 王婆
- 真实角色: 潘金莲
- class kindWoman:
- # 抛媚眼
- def make_eyes_with_man(self):
- pass
- def make_eyes_with_man(self):
- # 代理
- class WangPo(kindWoman):
- def __init__(self):
- self.woman=PanJinLian()
- def set_woman(self,kind_woman):
- self.woman=kind_woman
- def make_eyes_with_man(self):
- self.woman.make_eyes_with_man()
- # 真实角色
- class PanJinLian(kindWoman):
- def make_eyes_with_man(self):
- print("潘金莲抛媚眼.....")
- def make_eyes_with_man(self):
- class JiaShi(kindWoman):
- def make_eyes_with_man(self):
- print("贾氏抛媚眼.....")
- def make_eyes_with_man(self):
- class XimenQing:
- def start(self):
- # 测试王婆
- wangpo=WangPo()
- wangpo.make_eyes_with_man()
- # 修改kindwoman
- jia=JiaShi()
- wangpo.set_woman(jia)
- wangpo.make_eyes_with_man()
- def start(self):
- xmq=XimenQing()
- xmq.start()
- def __init__(self):
python 面向对象—设计模式
最新推荐文章于 2021-10-05 23:02:02 发布