admin管理员组文章数量:1613416
Python版
https://github/faif/python-patterns/blob/master/fundamental/delegation_pattern.py
#!/usr/bin/env python # -*- coding: utf-8 -*- """ Reference: https://en.wikipedia/wiki/Delegation_pattern Author: https://github/IuryAlves *TL;DR80 Allows object composition to achieve the same code reuse as inheritance. """ class Delegator(object): """ >>> delegator = Delegator(Delegate()) >>> delegator.do_something("nothing") 'Doing nothing' >>> delegator.do_anything() """ def __init__(self, delegate): self.delegate = delegate def __getattr__(self, name): def wrapper(*args, **kwargs): if hasattr(self.delegate, name): attr = getattr(self.delegate, name) if callable(attr): return attr(*args, **kwargs) return wrapper class Delegate(object): def do_something(self, something): return "Doing %s" % something if __name__ == '__main__': import doctest doctest.testmod()Python转载版
转载于:https://wwwblogs/demonzk/p/9035700.html
本文标签: 模式思想基础delegationpatternfundamental
版权声明:本文标题:【编程思想】【设计模式】【基础模式Fundamental】delegation_pattern 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/dianzi/1728643256a1167382.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论