創建型模式
創建型模式有以下幾種:
Creational Patterns:
Pattern Description
singleton 僅僅實例化一次
abstract_factory use a generic function with specific factories
borg a singleton with shared-state among instances
builder instead of using multiple constructors, builder object receives parameters and returns constructed objects
factory_method delegate a specialized function/method to create instances
lazy_evaluation lazily-evaluated property pattern in Python
pool preinstantiate and maintain a group of instances of the same type
prototype use a factory and clones of a prototype for new instances (if instantiation is expensive)
已經花費了大量篇幅已經介紹了抽象工廠,建造者,工廠,原型這幾個模式,沒有介紹的是有單例,共享(borg),惰性求值和pool這幾個模式,下面簡單介紹下:
對于singleton,如果你真的想使用其他編程語言中類似的“單例模式”,你需要看:
http://blog.csdn.net/ghostfromheaven/article/details/7671853
http://ghostfromheaven.iteye.com/blog/1562618
但是,我要問的是,Python真的需要單例模式嗎?我指像其他編程語言中的單例模式。
答案是:不需要!
因為,Python有模塊(module),最pythonic的單例典范。
模塊在在一個應用程序中只有一份,它本身就是單例的,將你所需要的屬性和方法,直接暴露在模塊中變成模塊的全局變量和方法即可!
http://stackoverflow.com/a/31887/1447185
下面是四種方法實現了singleton,可以參考以下。
#-*- encoding=utf-8 -*-
print '----------------------方法1--------------------------'
#方法1,實現__new__方法
#并在將一個類的實例綁定到類變量_instance上,
#如果cls._instance為None說明該類還沒有實例化過,實例化該類,并返回
#如果cls._instance不為None,直接返回cls._instance
class Singleton(object):
def __new__(cls, *args, **kw):
if not hasattr(cls, '_instance'):
orig = super(Singleton, cls)
cls._instance = orig.__new__(cls, *args, **kw)
return cls._instance
class MyClass(Singleton):
a = 1
one = MyClass()
two = MyClass()
two.a = 3
print one.a
#3
#one和two完全相同,可以用id(), ==, is檢測
print id(one)
#29097904
print id(two)
#29097904
print one == two
#True
print one is two
#True
print '----------------------方法2--------------------------'
#方法2,共享屬性;所謂單例就是所有引用(實例、對象)擁有相同的狀態(屬性)和行為(方法)
#同一個類的所有實例天然擁有相同的行為(方法),
#只需要保證同一個類的所有實例具有相同的狀態(屬性)即可
#所有實例共享屬性的最簡單最直接的方法就是__dict__屬性指向(引用)同一個字典(dict)
#可參看:http://code.activestate.com/recipes/66531/
class Borg(object):
_state = {}
def __new__(cls, *args, **kw):
ob = super(Borg, cls).__new__(cls, *args, **kw)
ob.__dict__ = cls._state
return ob
class MyClass2(Borg):
a = 1
one = MyClass2()
two = MyClass2()
#one和two是兩個不同的對象,id, ==, is對比結果可看出
two.a = 3
print one.a
#3
print id(one)
#28873680
print id(two)
#28873712
print one == two
#False
print one is two
#False
#但是one和two具有相同的(同一個__dict__屬性),見:
print id(one.__dict__)
#30104000
print id(two.__dict__)
#30104000
print '----------------------方法3--------------------------'
#方法3:本質上是方法1的升級(或者說高級)版
#使用__metaclass__(元類)的高級python用法
class Singleton2(type):
def __init__(cls, name, bases, dict):
super(Singleton2, cls).__init__(name, bases, dict)
cls._instance = None
def __call__(cls, *args, **kw):
if cls._instance is None:
cls._instance = super(Singleton2, cls).__call__(*args, **kw)
return cls._instance
class MyClass3(object):
__metaclass__ = Singleton2
one = MyClass3()
two = MyClass3()
two.a = 3
print one.a
#3
print id(one)
#31495472
print id(two)
#31495472
print one == two
#True
print one is two
#True
print '----------------------方法4--------------------------'
#方法4:也是方法1的升級(高級)版本,
#使用裝飾器(decorator),
#這是一種更pythonic,更elegant的方法,
#單例類本身根本不知道自己是單例的,因為他本身(自己的代碼)并不是單例的
def singleton(cls, *args, **kw):
instances = {}
def _singleton():
if cls not in instances:
instances[cls] = cls(*args, **kw)
return instances[cls]
return _singleton
@singleton
class MyClass4(object):
a = 1
def __init__(self, x=0):
self.x = x
one = MyClass4()
two = MyClass4()
two.a = 3
print one.a
#3
print id(one)
#29660784
print id(two)
#29660784
print one == two
#True
print one is two
#True
one.x = 1
print one.x
#1
print two.x
#1
對于共享(borg)模式,主要來自于star trek中的borg種族的特性。可以參考以下代碼:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
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
主要還是要關注這條語句:
self.__dict__ = self.__shared_state
通過這條語句實現了共享。
下面看以下惰性求值,lazy_evaluation模式:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Lazily-evaluated property pattern in Python.
https://en.wikipedia.org/wiki/Lazy_evaluation
References:
bottle
https://github.com/bottlepy/bottle/blob/cafc15419cbb4a6cb748e6ecdccf92893bb25ce5/bottle.py#L270
django
https://github.com/django/django/blob/ffd18732f3ee9e6f0374aff9ccf350d85187fac2/django/utils/functional.py#L19
pip
https://github.com/pypa/pip/blob/cb75cca785629e15efb46c35903827b3eae13481/pip/utils/__init__.py#L821
pyramimd
https://github.com/Pylons/pyramid/blob/7909e9503cdfc6f6e84d2c7ace1d3c03ca1d8b73/pyramid/decorator.py#L4
werkzeug
https://github.com/pallets/werkzeug/blob/5a2bf35441006d832ab1ed5a31963cbc366c99ac/werkzeug/utils.py#L35
"""
from __future__ import print_function
import functools
class lazy_property(object):
def __init__(self, function):
self.function = function
functools.update_wrapper(self, function)
def __get__(self, obj, type_):
if obj is None:
return self
val = self.function(obj)
obj.__dict__[self.function.__name__] = val
return val
class Person(object):
def __init__(self, name, occupation):
self.name = name
self.occupation = occupation
@lazy_property
def relatives(self):
# Get all relatives, let's assume that it costs much time.
relatives = "Many relatives."
return relatives
def main():
Jhon = Person('Jhon', 'Coder')
print(u"Name: {0} Occupation: {1}".format(Jhon.name, Jhon.occupation))
print(u"Before we access `relatives`:")
print(Jhon.__dict__)
print(u"Jhon's relatives: {0}".format(Jhon.relatives))
print(u"After we've accessed `relatives`:")
print(Jhon.__dict__)
if __name__ == '__main__':
main()
### OUTPUT ###
# Name: Jhon Occupation: Coder
# Before we access `relatives`:
# {'name': 'Jhon', 'occupation': 'Coder'}
# Jhon's relatives: Many relatives.
# After we've accessed `relatives`:
# {'relatives': 'Many relatives.', 'name': 'Jhon', 'occupation': 'Coder'}
pool模式非常的好理解,就是用Queue實現了一個生產者/消費者模型,queue模塊是線程安全的,下面的stackoverflow的鏈接
主要就是介紹了想要實現一個pool,里面存放一些網絡的連接,因為如果每次建立連接,系統開銷比較大。使用queue模塊非常合適。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
http://stackoverflow.com/questions/1514120/python-implementation-of-the-object-pool-design-pattern
"""
class ObjectPool(object):
def __init__(self, queue, auto_get=False):
self._queue = queue
self.item = self._queue.get() if auto_get else None
def __enter__(self):
if self.item is None:
self.item = self._queue.get()
return self.item
def __exit__(self, Type, value, traceback):
if self.item is not None:
self._queue.put(self.item)
self.item = None
def __del__(self):
if self.item is not None:
self._queue.put(self.item)
self.item = None
def main():
try:
import queue
except ImportError: # python 2.x compatibility
import Queue as queue
def test_object(queue):
pool = ObjectPool(queue, True)
print('Inside func: {}'.format(pool.item))
sample_queue = queue.Queue()
sample_queue.put('yam')
with ObjectPool(sample_queue) as obj:
print('Inside with: {}'.format(obj))
print('Outside with: {}'.format(sample_queue.get()))
sample_queue.put('sam')
test_object(sample_queue)
print('Outside func: {}'.format(sample_queue.get()))
if not sample_queue.empty():
print(sample_queue.get())
if __name__ == '__main__':
main()
### OUTPUT ###
# Inside with: yam
# Outside with: yam
# Inside func: sam
# Outside func: sam