.Learning.Python.Design.Patterns.2nd.Edition之单实例模式
可以慢慢理解。。
对照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之单实例模式的更多相关文章
- 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 ...
- Learning PHP Design Patterns
Learning PHP Design Patterns CHAPTER 1 Algorithms handle speed of operations, and design patterns ha ...
- AMD - Learning JavaScript Design Patterns [Book] - O'Reilly
AMD - Learning JavaScript Design Patterns [Book] - O'Reilly The overall goal for the Asynchronous Mo ...
- Python静态方法实现单实例模式
单实例模式 当程序中需要同一个实例就可以解决问题的场景,可以使用单实例模式
- xadmin系列之单实例模式
先看下单实例的定义 python的模块实现单例模式是python语言特有的,python的模块天然就是单例的,因为python有个pyc文件,导入一次后,第二次导入直接从pyc中取数据了 这里我们主要 ...
- 设计模式之单实例模式(Singleton)
原理:将类的构造函数由pubic变为private或者protect,添加获取对象的public 成员函数,返回指向对象的静态指针. 首先来一段简单的代码实现 代码一 class Singleton ...
- 8.2 GOF设计模式一: 单实例模式 SingleTon
GOF设计模式一: 单实例模式 SingleTon 整个美国,只有一个“现任美国总统” 比如,在学校,“老师”,有数百个:“校长”,只有一个 系统运行时,如何保证某个类只允许实例化一个对象 ...
- Learning JavaScript Design Patterns The Observer Pattern
The Observer Pattern The Observer is a design pattern where an object (known as a subject) maintains ...
- Learning JavaScript Design Patterns The Module Pattern
The Module Pattern Modules Modules are an integral piece of any robust application's architecture an ...
随机推荐
- HTTP协议与HTTPS协议区别
HTTPS(Secure Hypertext Transfer Protocol)安全超文本传输协议: 它是一个安全通信通道,它基于HTTP开发,用于在客户计算机和服务器之间交换信息,它使用安全套 ...
- memcache 与 mencached扩展的区别
memcache是一套分布式的高速缓存系统,由LiveJournal的Brad Fitzpatrick开发,但目前被许多网站使用以提升网站的访问速度,尤其对于一些大型的.需要频繁访问数据库的网站访问速 ...
- ndk学习15: IPC机制
Linux IPC机制 来自为知笔记(Wiz)
- sone2(未完成)
先留个半完成代码 边看论文边看题解写的...难受.. #include<cstdio> #include<cstring> namespace utils{ inline in ...
- Smallest Rectangle Enclosing Black Pixels
An image is represented by a binary matrix with 0 as a white pixel and 1 as a black pixel. The black ...
- SQL Server 流程控制
流程控制语句: BEGIN ... END WAITFOR GOTO WHILE IF ... ELSE BREAK RETURN CONTINURE 1.BEGIN ... END BEGIN ...
- centos7.0 安装LNMP运行环境
LNMP作为php流行的运行环境,而最近需要搭建一个内部的php论坛.记录下LNMP的安装: 1.安装mysql 请参考:centos7 安装mysql5.7.11注意事项 2.安装php yum i ...
- 在wpf中使用winrt的Toast弹框效果
源码地址:https://code.msdn.microsoft.com/windowsdesktop/Sending-toast-notifications-71e230a2/sourcecode? ...
- Java Web开发框架Spring+Hibernate整合效果介绍(附源码)
最近花了一些时间整合了一个SpringMVC+springAOP+spring security+Hibernate的一套框架,之前只专注于.NET的软件架构设计,并没有接触过Java EE,好在有经 ...
- VC++ 模块与资源分离
在一些开发过程中,需要模块支持中英文语言切换,比较好的实现方式是从模块中将资源分离出来,做成中英文两个资源dll,根据需要加载不同的dll从而实现切换不同的语言显示. 新建一个资源dll文件,选择Wi ...