zoukankan      html  css  js  c++  java
  • 面向对象之单例模式

    1. 单例模式

    1.1 什么是单例模式

    • 单例模式就是基于某种方法实例化多次得到的示例是同一个
    class Foo():
        def __init__(self,name, age):
            self.name = name
            self.age = age
            
    f1 = Foo('hades',13)
    print(f1)
    f2 = Foo('hades',13)
    print(f2)
    f3 = Foo('hades',13)
    print(f3)
    
    <__main__.Foo object at 0x0000023CB56AD518>
    <__main__.Foo object at 0x0000023CB56AD550>
    <__main__.Foo object at 0x0000023CB5688048>
    

    上面演示的就不属于单例模式,虽然看起来实例化对象一摸一样,但从内存地址就能看出,其实都是单独的对象

    1.2 为什么用单例模式

    • 当实例化多次得到的对象存放的属性都是一样的时候,我们就没必要去占用更多的内存空间,实例化对象指向同一个内存

    • 最终目的就是省内存

    1.3 单例模式三种方式

    1.3.1 使用类内部绑定方法特性,定义静态方法

    NAME = 'hades'
    AGE = 27
    
    class People:
        __instance = None
        
        def __init__(self,name,age):
            self.name = name 
            self.age = age
            
        @classmethod
        def from_conf(cls):
            if cls.__instance:
                return cls.__instance
            
            cls.__instance = People.__init__(cls,NAME,AGE)
            return cls.__instance
            
    p1 = People.from_conf()
    p2 = People.from_conf()
    print(p1==p2)
    
    True
    

    1.3.2 利用装饰器实现单例模式

    NAME = 'hades'
    AGE = 27
    
    def deco(cls):
        cls.__instance = cls(NAME,AGE)
        
        def wrapper(*args,**kwargs):
            if len(args) == 0 and len(kwargs) == 0:
                return cls.__instance
            
            res = cls(*args,**kwargs)
            return res
        
        return wrapper
    
    @deco
    class People():
        
        def __init__(self,name,age):
            self.name = name
            self.age = age
            
    p1 = People()
    p2 = People()
    p3 = People('bonnie',16)
    print(p1 == p2)
    print(p1 == p3)
    
    True
    False
    

    1.3.3 利用元类实现单例模式

    NAME = 'hades'
    AGE = 27
    
    class Mymeta(type):
        def __init__(self, class_name, class_bases, class_dict):
            super().__init__(class_name, class_bases, class_dict)
            
            self.__instance = self(NAME, AGE)
            
        def __call__(self, *args, **kwargs):
            
            if len(args) == 0 and len(kwargs) == 0:
                return self.__instance
            
            obj = self.__new__(self)
            self.__init__(obj, *args, **kwargs)
            
            return obj
            
    class People(metaclass=Mymeta):
        def __init__(self,name,age):
            self.name = name
            self.age = age
            
    p1 = People()
    p2 = People()
    p3 = People('bonnie',16)
    print(p1 == p2)
    print(p1 == p3)
    
    True
    False
  • 相关阅读:
    UDP and netstat
    UDP learn by Python3
    UDP headers and checksum
    routetrace
    IPv4 headers
    Commands for IP
    IP checksum
    POJ 3667 Hotel 线段树处理区间信息
    【枚举】Codeforces Round #432 (Div. 2, based on IndiaHacks Final Round 2017) Div2C题
    二分图最大匹配模板 HDU1083
  • 原文地址:https://www.cnblogs.com/Hades123/p/11068357.html
Copyright © 2011-2022 走看看