可以慢慢理解。。

对照JAVA

class Singleton(object):
    def __new__(cls):
        if not hasattr(cls, 'instance'):
            cls.instance = super(Singleton, cls).__new__(cls)
        return cls.instance

s = Singleton()
print("Object created", s, id(s))

s1 = Singleton()
print("Object created", s1, id(s1))

class SingletonA:
    __instance = None
    def __init__(self):
        if not SingletonA.__instance:
            print("__init__ method called..")
        else:
            print("Instance already created:", self.getInstance())

    @classmethod
    def getInstance(cls):
        if not cls.__instance:
            cls.__instance = SingletonA()
        return cls.__instance

s = SingletonA()
print("Object created", SingletonA.getInstance())
s = SingletonA()

class Borg:
    "}
    def __init__(self):
        self.x = 1
        self.__dict__ = self.__shared_state
        pass

b = Borg()
b1 = Borg()
b.x = 4

print("Borg Object 'b':", b)
print("Borg Object 'b1':", b1)
print("Object State 'b':", b.__dict__)
print("Object State 'b1':", b1.__dict__)

class BorgA:
    _shared_state = {"}
    def __new__(cls, *args, **kwargs):
        obj = super(BorgA, cls).__new__(cls, *args, **kwargs)
        obj.__dict__ = cls._shared_state
        return obj

b = BorgA()
b1 = BorgA()
b.x = 4

print("BorgA Object 'b':", b)
print("BorgA Object 'b1':", b1)
print("Object State 'b':", b.__dict__)
print("Object State 'b1':", b1.__dict__)

.Learning.Python.Design.Patterns.2nd.Edition之单实例模式的更多相关文章

  1. use getters and setters Learning PHP Design Patterns

    w Learning PHP Design Patterns Much of what passes as OOP misuses getters and setters, and making ac ...

  2. Learning PHP Design Patterns

    Learning PHP Design Patterns CHAPTER 1 Algorithms handle speed of operations, and design patterns ha ...

  3. AMD - Learning JavaScript Design Patterns [Book] - O'Reilly

    AMD - Learning JavaScript Design Patterns [Book] - O'Reilly The overall goal for the Asynchronous Mo ...

  4. Python静态方法实现单实例模式

    单实例模式 当程序中需要同一个实例就可以解决问题的场景,可以使用单实例模式

  5. xadmin系列之单实例模式

    先看下单实例的定义 python的模块实现单例模式是python语言特有的,python的模块天然就是单例的,因为python有个pyc文件,导入一次后,第二次导入直接从pyc中取数据了 这里我们主要 ...

  6. 设计模式之单实例模式(Singleton)

    原理:将类的构造函数由pubic变为private或者protect,添加获取对象的public 成员函数,返回指向对象的静态指针. 首先来一段简单的代码实现 代码一 class Singleton ...

  7. 8.2 GOF设计模式一: 单实例模式 SingleTon

    GOF设计模式一: 单实例模式 SingleTon  整个美国,只有一个“现任美国总统”  比如,在学校,“老师”,有数百个:“校长”,只有一个  系统运行时,如何保证某个类只允许实例化一个对象 ...

  8. Learning JavaScript Design Patterns The Observer Pattern

    The Observer Pattern The Observer is a design pattern where an object (known as a subject) maintains ...

  9. Learning JavaScript Design Patterns The Module Pattern

    The Module Pattern Modules Modules are an integral piece of any robust application's architecture an ...

随机推荐

  1. Codeforces Gym 101138 D. Strange Queries

    Description 给你一下长度为 \(n\) 的序列. \(a_i=a_j\) \(l_1 \leqslant i \leqslant r_1\) \(l_2 \leqslant i \leqs ...

  2. jQuery调用后台方法

    前台: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.as ...

  3. mysql 日志文件mysql-bin文件清除方法,和mysql-bin相关文件的配置

    默认情况下mysql会一直保留mysql-bin文件,这样到一定时候,磁盘可能会被撑满,这时候是否可以删除这些文件呢,是否可以安全删除,是个问题. 首先要说明一下,这些文件都是mysql的日志文件,如 ...

  4. Oauth 2.0第三方账号登录原理图

    百度.QQ等服务商

  5. neutron中创建子网时禁用dhcp服务的问题

    在neutron中创建provider网络时,可以指定是否禁用dhcp.若禁用,就可以使用物理网络中的dhcp服务.若使用物理网络的dhcp,就要禁用子网中提供的.如图

  6. 【leetcode】Surrounded Regions

    Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...

  7. 【leetcode】Remove Duplicates from Sorted Array II

    Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...

  8. 【leetcode】Reverse Linked List II

    Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one-pass. F ...

  9. AC自动机题目汇总

    POJ 4052 ZJU 3430 HDU 4117 HNU 10104 HDU 2457 HNU 11187 ZJU 3545 HDU 3341

  10. Lock+Condition 相对于 wait+notify 的一个优势案例分析

    问题的描述 启动3个线程打印递增的数字, 线程1先打印1,2,3,4,5, 然后是线程2打印6,7,8,9,10, 然后是线程3打印11,12,13,14,15. 接着再由线程1打印16,17,18, ...