当前位置: 移动技术网 > IT编程>开发语言>c# > Python设计模式编程中的备忘录模式与对象池模式示例

Python设计模式编程中的备忘录模式与对象池模式示例

2019年07月18日  | 移动技术网IT编程  | 我要评论

memento备忘录模式
备忘录模式一个最好想象的例子:undo! 它对对象的一个状态进行了'快照', 在你需要的时候恢复原貌。做前端会有一个场景:你设计一个表单,当点击提交会对表单内容 验证,这个时候你就要对用户填写的数据复制下来,当用户填写的不正确或者格式不对等问题, 就可以使用快照数据恢复用户已经填好的,而不是让用户重新来一遍,不是嘛?

python的例子
这里实现了一个事务提交的例子

import copy

def memento(obj, deep=false):

  # 对你要做快照的对象做快照
  state = (copy.copy if deep else copy.deepcopy)(obj.__dict__)
  def restore():
    obj.__dict__ = state
  return restore

class transaction:

  deep = false
  def __init__(self, *targets):
    self.targets = targets
    self.commit()
  # 模拟事务提交,其实就是初始化给每个对象往self.targets赋值
  def commit(self):
    self.states = [memento(target, self.deep) for target in self.targets]
  # 回滚其实就是调用memento函数,执行其中的闭包,将__dict__恢复
  def rollback(self):
    for state in self.states:
      state()

# 装饰器的方式给方法添加这个事务的功能
def transactional(method):
  # 这里的self其实就是要保存的那个对象,和类的实例无关
  def wrappedmethod(self, *args, **kwargs):
    state = memento(self)
    try:
      return method(self, *args, **kwargs)
    except:
      # 和上面的回滚一样,异常就恢复
      state()
      raise
  return wrappedmethod

class numobj(object):
  def __init__(self, value):
    self.value = value
  def __repr__(self):
    return '<%s: %r>' % (self.__class__.__name__, self.value)
  def increment(self):
    self.value += 1

  @transactional
  def dostuff(self):
    # 赋值成字符串,再自增长肯定会报错的
    self.value = '1111'
    self.increment()

if __name__ == '__main__':

  n = numobj(-1)
  print n
  t = transaction(n)
  try:
    for i in range(3):
      n.increment()
      print n
    # 这里事务提交会保存状态从第一次的-1到2
    t.commit()
    print '-- commited'
    for i in range(3):
      n.increment()
      print n
    n.value += 'x' # will fail
    print n
  except:
    # 回滚只会回顾到上一次comit成功的2 而不是-1
    t.rollback()
    print '-- rolled back'
  print n
  print '-- now doing stuff ...'
  try:
    n.dostuff()
  except:
    print '-> doing stuff failed!'
    import traceback
    traceback.print_exc(0)
    pass
  # 第二次的异常回滚n还是2, 整个过程都是修改numobj的实例对象
  print n

注意
当你要保存的状态很大,可能会浪费大量内存


对象池模式
在开发中,我们总是用到一些和'池'相关的东西,比如 内存池,连接池,对象池,线程池.. 这里说的对象池其实也就是一定数量已经创建好的对象的集合。为什么要用对象池? 创建对象是要付出代价的(我暂时还没有研究过底层,只说我工作中体会的), 比如pymongo就自带线程池,这样用完就放回到池里再被重用,岂不是节省了创建的花费?

python的例子
我这里实现了个线程安全的简单的对象池

import queue
import types
import threading
from contextlib import contextmanager

class objectpool(object):

  def __init__(self, fn_cls, *args, **kwargs):
    super(objectpool, self).__init__()
    self.fn_cls = fn_cls
    self._myinit(*args, **kwargs)

  def _myinit(self, *args, **kwargs):
    self.args = args
    self.maxsize = int(kwargs.get("maxsize",1))
    self.queue = queue.queue()
  def _get_obj(self):
    # 因为传进来的可能是函数,还可能是类
    if type(self.fn_cls) == types.functiontype:
      return self.fn_cls(self.args)
    # 判断是经典或者新类
    elif type(self.fn_cls) == types.classtype or type(self.fn_cls) == types.typetype:
      return apply(self.fn_cls, self.args)
    else:
      raise "wrong type"

  def borrow_obj(self):
    # 这个print 没用,只是在你执行的时候告诉你目前的队列数,让你发现对象池的作用
    print self.queue._qsize()
    # 要是对象池大小还没有超过设置的最大数,可以继续放进去新对象
    if self.queue.qsize()<self.maxsize and self.queue.empty():
      self.queue.put(self._get_obj())
    # 都会返回一个对象给相关去用
    return self.queue.get() 
  # 回收
  def recover_obj(self,obj):
    self.queue.put(obj)

# 测试用函数和类
def echo_func(num):
  return num

class echo_cls(object):
  pass

# 不用构造含有__enter__, __exit__的类就可以使用with,当然你可以直接把代码放到函数去用
@contextmanager
def poolobj(pool):
  obj = pool.borrow_obj()
  try:
    yield obj
  except exception, e:
    yield none
  finally:
    pool.recover_obj(obj)

obj = objectpool(echo_func, 23, maxsize=4)
obj2 = objectpool(echo_cls, maxsize=4)

class mythread(threading.thread):

  def run(self):
    # 为了实现效果,我搞了个简单的多线程,2个with放在一个地方了,只为测试用
    with poolobj(obj) as t:
      print t
    with poolobj(obj2) as t:
      print t

if __name__ == '__main__':
  threads = []
  for i in range(200):
    t = mythread()
    t.start()
    threads.append(t)
  for t in threads:
    t.join(true)

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网