可以慢慢理解。。

对照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. qt-5.6.0 移植之qt文件系统的建立

    经过差不多两个星期的奋斗,终于在板子里面跑起来了qt 程序,虽然现在还没有把触摸屏驱动加上去,但是我相信已经不远了!!!!! 在前两篇的随笔里面 , 已经编译好了最纯净的文件系统以及交叉编译完成了qt ...

  2. ccs3中icon转换为字体的方法

    小图标转换为字体有几大优点 文件小,一般50k以内 避免了加载多个icons,减少了加载次数,有利于页面优化. 兼容性很好,可以随便放大缩小,都能正常显示. 维护起来也很简单,只用找到这个字体文件(比 ...

  3. mysql中You can't specify target table for update in FROM clause错误

    原SQL delete from DEP_SYSTEM_PORTLET_SETTINGS where ID in ( select ID from DEP_SYSTEM_PORTLET_SETTING ...

  4. 怎样将myeclipse里默认编码设置成utf-8

    需要设置三个位置: [1]需要在  Preferences->general->content types->下角是文件编码,可以自己定义 [2]windows->Prefer ...

  5. Ubuntu下安装eclipse

    一.eclipse安装环境JDK的安装 1.下载JDK 从官网下载jdk8 jdk-8u5-linux-x64.tar.gz 2.解压$ tar -zxvf jdk-8u5-linux-x64.tar ...

  6. Linq to 泛型集合查询集合包括大写M和年龄小于等于18

    #region Linq to 泛型集合查询集合包括大写M和年龄小于等于18            //List<Student> list = new List<Student&g ...

  7. 【leetcode】N-Queens II

    N-Queens II Follow up for N-Queens problem. Now, instead outputting board configurations, return the ...

  8. UESTC 250

    windy数 基本的数位DP,需要判断当前位是否为起始位. #include <cstdio> #include <cmath> #include <cstring> ...

  9. MMO可见格子算法

    看注释吧,写的很清楚了 using System; using System.Collections.Generic; using System.Diagnostics; using System.L ...

  10. windows hosts

    2015年6月29日 11:10:56 星期一 windows 的 hosts 文件生效机制 以最前边的为准, 重复的硬解析只有第一次出现的地方生效