python学习基础26 设计模式与pickle模块

94 阅读2分钟

设计模式

    1. 针对一些固定的问题设计出了固定的解决套路
    1. 设计模式一共有23种
    1. 设计模式分为三类
创建型         结构型        行为型
    1. 设计模式之单例模式
    类加括号调用多次只允许产生一个对象
      class MyClass:
      	pass
    
      obj1 = MyClass()
      obj2 = MyClass()
      obj3 = MyClass()
      print(id(obj1), id(obj2), id(obj3))  
      
      输出结果:
          # 2263190030944 2263190494080 2263189844464
      # 正常情况下,类名加括号实例化产生对象,执行的次数与产生对象的次数相同
    
    • 为什么要使用单例?
      类中有很多很好用的方法 程序很多地方都需要使用(通过对象调用)
          如果产生的地方特别多 那么会浪费一定的内存空间,所以需要使用单例
      
      • 方式1:使用元类干预对象的创建过程
      class MyMeTaClass(type):
         # 记录类是否已经创建了对象
                 instance = None
      
         def __call__(self, *args, **kwargs):
             if self.instance:
                 return self.instance
             # 获取空对象
             obj = super().__call__(*args, **kwargs)
             # 保存对象
             self.instance = obj
             # 返回空对象
             return obj
      
      class Single(metaclass=MyMeTaClass):
          def __init__(self, name):
                 self.name = name
      
      obj1 = Single('jason')
      obj2 = Single('kevin')
      obj3 = Single('tony')z
      print(id(obj1), id(obj2), id(obj3))
      print(obj1.name)
      print(obj2.name)
      print(obj3.name)
      
      • 方式2:定义一个类方法实现单例模式
      import settings
      
      class Mysql:
          __instance=None
          def __init__(self,host,port):
              self.host=host
              self.port=port
      
          @classmethod
          def singleton(cls):
              if not cls.__instance:
                  cls.__instance=cls(settings.HOST,settings.PORT)
              return cls.__instance
      
      obj1=Mysql('1.1.1.2',3306)
      obj2=Mysql('1.1.1.3',3307)
      print(obj1 is obj2)             #False
      
      obj3=Mysql.singleton()
      obj4=Mysql.singleton()
      print(obj3 is obj4)             #True
      
      
      • 方式3:定义一个装饰器实现单例模式
      import settings
      
      def singleton(cls):                     #cls=Mysql
          _instance=cls(settings.HOST,settings.PORT)
      
          def wrapper(*args,**kwargs):
              if args or kwargs:
                  obj=cls(*args,**kwargs)
                  return obj
              return _instance
          return wrapper
      
      
      @singleton                             # Mysql=singleton(Mysql)
      class Mysql:
          def __init__(self,host,port):
              self.host=host
              self.port=port
      
      obj1=Mysql()
      obj2=Mysql()
      obj3=Mysql()
      print(obj1 is obj2 is obj3)            #True
      
      obj4=Mysql('1.1.1.3',3307)
      obj5=Mysql('1.1.1.4',3308)
      print(obj3 is obj4) #False
      
      

pickle模块

  • pickle用来做序列化,反序列化的
  • pickle模块支持python所有的数据类型,但只支持python间的传输
  • pickle模块了解即可
    class  Myclass:
        def __init__(self, name):
            self.name = name
        def choice_course(self):
            print('%s正在选课' % self.name)
    
      import  pickle
    
      with open(r'a.txt', 'rb') as f:
          data = pickle.load(f)
      print(data)
      print(data.name)
      data.choice_course()