【编程思想】【设计模式】【创建模式creational】Borg/Monostate
Python版
https://github.com/faif/python-patterns/blob/master/creational/borg.py
#!/usr/bin/env python
# -*- coding: utf-8 -*- """
*What is this pattern about?
The Borg pattern (also known as the Monostate pattern) is a way to
implement singleton behavior, but instead of having only one instance
of a class, there are multiple instances that share the same state. In
other words, the focus is on sharing state instead of sharing instance
identity.
>>Borg设计模式(我们通常叫它Monostate模式)是一个用来实现单例的行为,
>>但是它不是一个类只有一个实例,它有很多实例共享相同的状态。
>>换句话说,它专注在共享状态而不是共享实例 *What does this example do?
To understand the implementation of this pattern in Python, it is
important to know that, in Python, instance attributes are stored in a
attribute dictionary called __dict__.
>>为了弄明白这个设计模式在python中的实现,我们必须要知道,
>>在python中,实例的属性是存储在一个属性字典中,叫做__dict__
Usually, each instance will have
its own dictionary, but the Borg pattern modifies this so that all
instances have the same dictionary.
>>通常来说,每个实例都有它自己的字典,但是Borg模式修改了他
>>所以所有实例都有相同的字典
In this example, the __shared_state attribute will be the dictionary
shared between all instances, and this is ensured by assigining
__shared_state to the __dict__ variable when initializing a new
instance (i.e., in the __init__ method).
>>在这个例子中,属性__shared_state作为字典被所有实例共享,
>>这就确保了在初始化一个实例的时候(例如 __init__方法),
Other attributes are usually
added to the instance's attribute dictionary, but, since the attribute
dictionary itself is shared (which is __shared_state), all other
attributes will also be shared.
>>其他的属性其他属性经常被添加到实例属性字典,但是,因为属性字典本身是被共享的
>>(__shared_state),所以所有其他的属性也是被共享的
For this reason, when the attribute self.state is modified using
instance rm2, the value of self.state in instance rm1 also chages. The
same happends if self.state is modified using rm3, which is an
instance from a subclass.
>>因为这个原因,但是属性self.state被实例rm2修改之后,rm1中self.state的值也改变了
>>如果子类的实例rm3修改了self.state也会发生相同的事情
Notice that even though they share attributes, the instances are not
the same, as seen by their ids.
>>注意,即使他们共享属性,实例也是不一样的,从他们的id就能看出来 *Where is the pattern used practically?
Sharing state is useful in applications like managing database connections:
>>共享属性在管理数据库连接的应用中非常实用
https://github.com/onetwopunch/pythonDbTemplate/blob/master/database.py *References:
https://fkromer.github.io/python-pattern-references/design/#singleton *TL;DR80
Provides singletone-like behavior sharing state between instances.
""" class Borg(object):
__shared_state = {} def __init__(self):
self.__dict__ = self.__shared_state
self.state = 'Init' def __str__(self):
return self.state class YourBorg(Borg):
pass if __name__ == '__main__':
rm1 = Borg()
rm2 = Borg() rm1.state = 'Idle'
rm2.state = 'Running' print('rm1: {0}'.format(rm1))
print('rm2: {0}'.format(rm2)) rm2.state = 'Zombie' print('rm1: {0}'.format(rm1))
print('rm2: {0}'.format(rm2)) print('rm1 id: {0}'.format(id(rm1)))
print('rm2 id: {0}'.format(id(rm2))) rm3 = YourBorg() print('rm1: {0}'.format(rm1))
print('rm2: {0}'.format(rm2))
print('rm3: {0}'.format(rm3)) ### OUTPUT ###
# rm1: Running
# rm2: Running
# rm1: Zombie
# rm2: Zombie
# rm1 id: 140732837899224
# rm2 id: 140732837899296
# rm1: Init
# rm2: Init
# rm3: Init
Python转载版
【编程思想】【设计模式】【创建模式creational】Borg/Monostate的更多相关文章
- 【编程思想】【设计模式】【创建模式creational 】工厂模式factory_method
Python版 https://github.com/faif/python-patterns/blob/master/creational/factory_method.py #!/usr/bin/ ...
- 【编程思想】【设计模式】【创建模式creational】Pool
Python版 https://github.com/faif/python-patterns/blob/master/creational/pool.py #!/usr/bin/env python ...
- 【编程思想】【设计模式】【创建模式creational】抽象工厂模式abstract_factory
Python版 https://github.com/faif/python-patterns/blob/master/creational/abstract_factory.py #!/usr/bi ...
- 【编程思想】【设计模式】【创建模式creational】建造者模式builder
Python版 https://github.com/faif/python-patterns/blob/master/creational/builder.py #!/usr/bin/python ...
- 【编程思想】【设计模式】【创建模式creational】原形模式Prototype
Python版 https://github.com/faif/python-patterns/blob/master/creational/prototype.py #!/usr/bin/env p ...
- 【编程思想】【设计模式】【创建模式creational】lazy_evaluation
Python版 https://github.com/faif/python-patterns/blob/master/creational/lazy_evaluation.py #!/usr/bin ...
- 【java设计模式】【创建模式Creational Pattern】单例模式Singleton Pattern
//饿汉式:资源利用率较低(无论是否需要都会创建),性能较高(使用前无需判断实例是否存在,可直接使用) public class EagerSingleton{ private static fina ...
- 【java设计模式】【创建模式Creational Pattern】抽象工厂模式Abstract Factory Pattern
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAW0AAABvCAIAAACo3AbKAAALvUlEQVR4nO1dUa7cOA7U/c+zwJxkf4
- 【java设计模式】【创建模式Creational Pattern】建造模式Builder Pattern
package com.tn.pattern; public class Client { public static void main(String[] args) { Director dire ...
随机推荐
- [python]selenium常用的操作
浏览器 1.火狐浏览器 br = webdriver.Firefox() #最大化窗口br.maximize_window() br.get('http://baidu.com') 2.谷歌浏览器 b ...
- 菜鸡的Java笔记 comparator 比较器
1.数组操作类: Arrays 2.两种比较器的使用: Comparable Comparator 3.实现二叉树算法 content (内容) ...
- Prometheus的监控解决方案(含监控kubernetes)
prometheus的简介和安装 Prometheus(普罗米修斯)是一个开源系统监控和警报工具,最初是在SoundCloud建立的.自2012年成立以来,许多公司和组织都采用了普罗米修斯,该项目拥有 ...
- Web优化躬行记(5)——网站优化
最近阅读了很多优秀的网站性能优化的文章,所以自己也想总结一些最近优化的手段和方法. 个人感觉性能优化的核心是:减少延迟,加速展现. 本文主要从产品设计.前端.后端和网络四个方面来诉说优化过程. 一.产 ...
- [hdu6984]Tree Planting
构造一个01矩阵,其中格子$(i,j)$对应于第$ik+j$个的位置(其中$0\le i<\lceil\frac{n}{k}\rceil,0\le j<k$,位置 ...
- [noi253]A
定义f[i][j]表示从(i,j)走到最后一行的期望,不断从下往上dp那么对于每一行都可以得到m个方程. 但由于这m个方程不是DAG,因此考虑用高斯消元,但时间复杂度不对. 观察方程可以发现如果不断将 ...
- [luogu5387]人形演舞
先对每一个求sg函数,暴力复杂度为$o(m^{2})$ 取$k$满足$2^{k}\le x<2^{k+1}$(即$x$二进制下的最高位),考虑$y$与$2^{k}$的关系 1.若$1\le y& ...
- [luogu5537]系统设计
考虑哈希,令$h[x]$表示根到$x$路径的哈希值,那么有$h[x]+hash(l,r)=h[ans]$ 考虑用线段树维护$a_{i}$的区间哈希值,并用map去找到对应的$ans$ 但还有一个问题, ...
- 【Java面试题】-- Java String
Java String 2019-11-02 17:40:45 by冲冲 1.String的内存位置 String是定义在 java.lang 包下的一个类.它不是基本数据类型.String是不可 ...
- AutoHotkey
;注释 : #==win !==Alt ^==Ctr +==shift 需要注意的是不要和现有的快捷键冲突,他会代替掉原来的快捷键操作很难受的. 热指令: 比如 ::yx1::1359720840 ...