5 Star 4 Fork 0

Gitee 极速下载 / python-patterns

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/faif/python-patterns
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
adapter.py 3.27 KB
一键复制 编辑 原始数据 按行查看 历史
rednafi 提交于 2020-06-28 01:01 . Type checked adapter pattern
"""
*What is this pattern about?
The Adapter pattern provides a different interface for a class. We can
think about it as a cable adapter that allows you to charge a phone
somewhere that has outlets in a different shape. Following this idea,
the Adapter pattern is useful to integrate classes that couldn't be
integrated due to their incompatible interfaces.
*What does this example do?
The example has classes that represent entities (Dog, Cat, Human, Car)
that make different noises. The Adapter class provides a different
interface to the original methods that make such noises. So the
original interfaces (e.g., bark and meow) are available under a
different name: make_noise.
*Where is the pattern used practically?
The Grok framework uses adapters to make objects work with a
particular API without modifying the objects themselves:
http://grok.zope.org/doc/current/grok_overview.html#adapters
*References:
http://ginstrom.com/scribbles/2008/11/06/generic-adapter-class-in-python/
https://sourcemaking.com/design_patterns/adapter
http://python-3-patterns-idioms-test.readthedocs.io/en/latest/ChangeInterface.html#adapter
*TL;DR
Allows the interface of an existing class to be used as another interface.
"""
from typing import Callable, TypeVar
T = TypeVar("T")
class Dog:
def __init__(self) -> None:
self.name = "Dog"
def bark(self) -> str:
return "woof!"
class Cat:
def __init__(self) -> None:
self.name = "Cat"
def meow(self) -> str:
return "meow!"
class Human:
def __init__(self) -> None:
self.name = "Human"
def speak(self) -> str:
return "'hello'"
class Car:
def __init__(self) -> None:
self.name = "Car"
def make_noise(self, octane_level: int) -> str:
return f"vroom{'!' * octane_level}"
class Adapter:
"""Adapts an object by replacing methods.
Usage
------
dog = Dog()
dog = Adapter(dog, make_noise=dog.bark)
"""
def __init__(self, obj: T, **adapted_methods: Callable):
"""We set the adapted methods in the object's dict."""
self.obj = obj
self.__dict__.update(adapted_methods)
def __getattr__(self, attr):
"""All non-adapted calls are passed to the object."""
return getattr(self.obj, attr)
def original_dict(self):
"""Print original object dict."""
return self.obj.__dict__
def main():
"""
>>> objects = []
>>> dog = Dog()
>>> print(dog.__dict__)
{'name': 'Dog'}
>>> objects.append(Adapter(dog, make_noise=dog.bark))
>>> objects[0].__dict__['obj'], objects[0].__dict__['make_noise']
(<...Dog object at 0x...>, <bound method Dog.bark of <...Dog object at 0x...>>)
>>> print(objects[0].original_dict())
{'name': 'Dog'}
>>> cat = Cat()
>>> objects.append(Adapter(cat, make_noise=cat.meow))
>>> human = Human()
>>> objects.append(Adapter(human, make_noise=human.speak))
>>> car = Car()
>>> objects.append(Adapter(car, make_noise=lambda: car.make_noise(3)))
>>> for obj in objects:
... print("A {0} goes {1}".format(obj.name, obj.make_noise()))
A Dog goes woof!
A Cat goes meow!
A Human goes 'hello'
A Car goes vroom!!!
"""
if __name__ == "__main__":
import doctest
doctest.testmod(optionflags=doctest.ELLIPSIS)
Python
1
https://gitee.com/mirrors/python-patterns.git
git@gitee.com:mirrors/python-patterns.git
mirrors
python-patterns
python-patterns
master

搜索帮助