python创建型设计模式——原型模式、单例模式
2017-01-04 12:37
351 查看
原型模式
应用情景
如果想根据现有对象复制出线的对象并进行修改,那么可以考虑原型模式python 创建对象的7种方法
Point 类
class Point: __slots__ = ("x","y") def __init__(self,x,y): self.x = x self.y = y
原型模式实现
def test(): point1 = Point(1, 2) #这种方式有风险 point2 = eval("{}({},{})".formate("Point", 2, 4)) point3 = getattr(sys.modules[__name__], "Point")(3,6) point4 = globals()["Point"](4,8) point5 = make_object(Point,5,10) point6 = copy.deepcopy(point5) #经典原型创建方式 point6.x = 6 point6.y = 12 #此方法无需先克隆对象,直接用新参数创建对象,效率会高很多 point7 = point1.__class__(7,4) def make_object(Class,*args,**kwargs) return Class(*args, **kwargs)
单例模式
应用情景
在整个程序运行的过程中,如果某个类只应该有一个实例,可以通过单例模式来保证单例模式的实现:
创建模块时,把全局状态放在私有变量中,并提供用于访问此变量的公开函数
例子
创建一个函数,用于返回含有货币汇率的字典(该字典以货币名称为键,以汇率为值)。这个函数可能被调用多次,但是大部分情况下,汇率数据只需要获取一次即可。
_URL = "http://......" def get(refresh=False): if refresh: get.rates = {} if get.rates: return get.rates with urllib.request.urlopen(_URL) as file: for line in file : line = line.rstrip().decode("utf-8") if not line or line.startswith("#","Date"): continue name, currency, *rest = re.split(r"\s*,\s*",line) key = "{} ({})".format(name,currency) try: get.rates[key] = float(rest[-1]) except ValueError as err: print("error {}: {}".format(err, line)) return get.rates #创建了rates的字典用于保存私有数据,并将该字典设置成get()的私有属性 get.rates = {}
相关文章推荐
- Python的创建型设计模式之原型模式与单例模式
- 设计模式中创建型模式 (二):原型模式
- C#面向对象设计模式纵横谈(6):Prototype 原型(创建型模式)
- 面向对象设计模式之Prototype原型模式(创建型)
- 23种设计模式五:创建型原型模式
- 设计模式——创建型设计模式总结(简单工厂、普通工厂、抽象工厂、建造者、原型和单例)
- 设计模式笔记(6)---原型模式(创建型)
- 设计模式之python版(六)--原型模式
- 设计模式五:Prototype原型模式——对象创建型模式
- 设计模式--创建型--原型模式
- 设计模式--创建型模式--原型模式(Protype Pattern)
- [导入]C#面向对象设计模式纵横谈(6):Prototype 原型模式(创建型模式).zip(9.56 MB)
- 设计模式笔记--创建型模式之四--原型prototype
- 设计模式(四)原型模式Prototype(创建型)
- 设计模式笔记(6)---原型模式(创建型)
- 七.创建型设计模式——Prototype Pattern(原型模式)
- 本文详细介绍Python 设计模式系列之二: 创建型 Simple Factory 模式(转载)
- 设计模式之原型法(ProtoType)----对象创建型模式
- 设计模式——创建型设计模式总结(简单工厂、普通工厂、抽象工厂、建造者、原型和单例)
- 设计模式笔记 5.Prototype 原型模式(创建型模式)