可以慢慢理解。。

对照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. windows安装rsync

    客户端:cwRsync 4.0.5 Installer 服务端:cwRsyncServer 4.0.5 Installer 安装配置Rsync服务端 1.直接双击安装包安装即可,在安装过程中会有要求用 ...

  2. 2016-03-12 Leanning Plan

    1,Python 2,Java 3,Html+Css 4,PHP 5,Crawl 6,WetChat Platform

  3. 将 JAR 转为 EXE – JSMOOTH 的使用教程(第二期)(转载)

    http://www.iteknical.com/convert-jar-to-exe-phase-ii-jsmooth-use-tutorial/

  4. phpcms更换域名用户无法注册问题

    问题背景: 用户注册必须在后台开启phpsso,这个sso也就是单点登录了,之前做的站都没有带用户登录,也一直没注意,今天线下localhost用户登录注册都没有问题,可是移到线上测试却怎么都无法注册 ...

  5. window下安装mongodb及php mongo扩展

    1.安装mongoDB 下载最新版本的 MongoDB 下载地址:http://www.mongodb.org/downloads 解压文件到 D:\mongodb 解压后的文件列表如下: 创建数据存 ...

  6. 转帖:用五分钟重温委托,匿名方法,Lambda,泛型委托,表达式树

    用五分钟重温委托,匿名方法,Lambda,泛型委托,表达式树 这些对老一代的程序员都是老生常谈的东西,没什么新意,对新生代的程序员却充满着魅力.曾经新生代,好多都经过漫长的学习,理解,实践才能掌握委托 ...

  7. Word Search I & II

    Word Search I Given a 2D board and a word, find if the word exists in the grid. The word can be cons ...

  8. hadoop MapReduce Yarn运行机制

    原 Hadoop MapReduce 框架的问题 原hadoop的MapReduce框架图 从上图中可以清楚的看出原 MapReduce 程序的流程及设计思路: 首先用户程序 (JobClient) ...

  9. c#.net Excel中的数据导入到SQL数据库中

    /// <summary>        /// 从Excel 导入学生        /// </summary>        /// <param name=&qu ...

  10. 迁移mysql数据到oracle上

    转自:http://www.cnblogs.com/Warmsunshine/p/4651283.html 我是生成的文件里面的master.sql里面的sql,一个一个拷出来的. 迁移mysql数据 ...