一个实用的基于python的实现各种设计模式的通用代码库

PyPattyrn 是一个实用的 Python 包,它汇集了各种设计模式的通用代码,以便于开发者们在自己的项目中轻松、快速地应用这些模式。设计模式是解决特定问题的经验证的方法,虽然不能直接转换为代码,但它们的实现往往具有很多共性。

通过使用 PyPattyrn 包,您可以:

  1. 减少重复代码:不必在每个项目中从头编写通用代码,PyPattyrn 已经为您准备好了。
  2. 提高开发效率:利用已验证的设计模式,使您的项目更加模块化、可维护和可扩展。
  3. 学习设计模式:PyPattyrn 提供的代码示例可以帮助您更好地理解设计模式的工作原理。

要在项目中使用 PyPattyrn,请遵循以下步骤:

  1. 安装 PyPattyrn:使用 pip 命令安装 PyPattyrn 包:pip install PyPattyrn
  2. 导入所需的模块:在 Python 代码中,导入相应的模块以使用 PyPattyrn 提供的类和函数。
  3. 使用 PyPattyrn 代码:根据需要调用 PyPattyrn 提供的类和函数,来实现设计模式。

需要注意的是,虽然 PyPattyrn 提供了大量设计模式的通用代码,但并不意味着您应该在所有情况下都使用它们。项目的具体需求和场景可能需要您根据实际情况进行调整和优化。

总之,PyPattyrn 是一个宝贵的资源,它可以帮助您更高效地将设计模式应用到 Python 项目中。通过减少重复代码、提高开发效率以及学习最佳实践,您可以更好地构建高质量、可扩展的软件系统。

安装


pip install pypattyrn

或者

git clone https://github.com/tylerlaberge/PyPattyrn.git
cd PyPattyrn
python setup.py install

例子


行为模式

处理对象之间通信的模式。


责任链模式

沿着对象链传递请求,直到请求被处理。from pypattyrn.behavioral.chain import Chain, ChainLink class ConcreteChainLinkThree(ChainLink): # This object is a ChainLink def handle(self, request): # Implement the handle method. if request == ‘handle_three’: return “Handled in chain link three” else: return self.successor_handle(request) # If this ChainLink can’t handle the request, # ask its successor to handle it. # (Has no successor so will raise AttributeError) # (This exception is caught and will call a Chains fail method) class ConcreteChainLinkTwo(ChainLink): # This object is a ChainLink def __init__(self): # Override init to set a successor on initialization. super().__init__() # first call ChainLinks init self.set_successor(ConcreteChainLinkThree()) # Set the successor of this chain link # to a ConcreteChainLinkThree instance. def handle(self, request): # Implement the handle method. if request == ‘handle_two’: return “Handled in chain link two” else: return self.successor_handle(request) # If this ChainLink can’t handle a request # ask its successor to handle it # (the ConcreteChainLinkThree instance). class ConcreteChainLinkOne(ChainLink): # This object is a ChainLink def __init__(self): super().__init__() self.set_successor(ConcreteChainLinkTwo()) # Set the successor of this ChainLink # to a ConcreteChainLinkTwo instance. def handle(self, request): # Implement the handle method. if request == ‘handle_one’: return “Handled in chain link one” else: return self.successor_handle(request) # If this ChainLink can’t handle a request # ask its successor to handle it # (the ConcreteChainLinkTwo instance). class ConcreteChain(Chain): # This object is a Chain def __init__(self): # Override init to initialize a Chain with the starting chain link. super().__init__(ConcreteChainLinkOne()) # Initialize this Chain with a start chain link. # (a ConcreteChainLinkOne instance) def fail(self): # Implement the fail method, this is called if no chain links could handle a request. return ‘Fail’ chain = ConcreteChain() assert “Handled in chain link one” == chain.handle(“handle_one”) assert “Handled in chain link two” == chain.handle(“handle_two”) assert “Handled in chain link three” == chain.handle(“handle_three”) assert “Fail” == chain.handle(‘handle_four’)

命令模式

将用于执行操作的信息封装到对象中。from pypattyrn.behavioral.command import Receiver, Command, Invoker class Thermostat(Receiver): # This object is a Receiver. # Contains methods for commands to use. def raise_temp(self, amount): return “Temperature raised by {0} degrees”.format(amount) def lower_temp(self, amount): return “Temperature lowered by {0} degrees”.format(amount) class RaiseTempCommand(Command): # This object is a Command. def __init__(self, receiver, amount=5): # Override init to include a temperature amount argument. super().__init__(receiver) self.amount = amount def execute(self): # Implement the execute method. return self._receiver.action(‘raise_temp’, self.amount) # Call on the Receiver’s action method with # the name of the method to call and args. # (Raises the temperature by 5 degrees.) def unexecute(self): # Implement the unexecute method. return self._receiver.action(‘lower_temp’, self.amount) # Calls on the Receiver to lower # the temperature by 5 degrees. class LowerTempCommand(Command): # This object is a Command. def __init__(self, receiver, amount=5): super().__init__(receiver) self.amount = amount def execute(self): return self._receiver.action(‘lower_temp’, self.amount) # Call on the receiver to lower the # temperature by 5 degrees. def unexecute(self): return self._receiver.action(‘raise_temp’, self.amount) # Call on the receiver to raise the # temperature by 5 degrees. class Worker(Invoker): # This object is the Invoker def __init__(self): # Override init to initialize an Invoker with Commands to accept. super().__init__([LowerTempCommand, RaiseTempCommand]) # Initialize the Invoker with the # LowerTempCommand and RaiseTempCommand classes. thermostat = Thermostat() # Create a Receiver. worker = Worker() # Create an Invoker. assert “Temperature lowered by 5 degrees” == worker.execute(LowerTempCommand(thermostat)) # Have the Invoker execute a LowerTempCommand # which uses the thermostat Receiver. assert “Temperature raised by 5 degrees” == worker.execute(RaiseTempCommand(thermostat)) # Have the Invoker execute a RaiseTempCommand # which uses the thermostat Receiver. assert “Temperature lowered by 5 degrees” == worker.undo() # Undo the previous command (Calls the RaiseTempCommand unexecute method.)

迭代器模式

一种顺序访问集合中元素的方法。from pypattyrn.behavioral.iterator import Iterable, Iterator class Counter(Iterable): # This object is an Iterable def __init__(self, max): self.count = 0 self.max = max def __next__(self): # Implement the __next__ method. self.count += 1 # Increment the count if self.count > self.max: raise StopIteration() # make sure to raise StopIteration at the appropriate time. else: return self.count – 1 # Return the count class CounterIterator(Iterator): # This object is an Iterator def __init__(self): # Override init to initialize an Iterator with the Counter object. super().__init__(Counter(10)) # Initialize the iterator with a Counter that goes to 10. counter_iterator = CounterIterator() # Create a CounterIterator. for count in counter_iterator: # You can loop through it how you would expect. print(count) # 0, 1, 2, 3, …, 9

中介者模式

用于管理许多对象之间通信的中介。from pypattyrn.behavioral.mediator import Mediator class Dog(object): sound = ” def set_sound(self, sound): self.sound = sound class Cat(object): sound = ” def set_sound(self, sound): self.sound = sound dog = Dog() cat = Cat() mediator = Mediator() # Create a Mediator object. mediator.connect(‘set_dog_sound’, dog.set_sound) # Connect the signal ‘set_dog_sound’ to the dog.set_sound method. mediator.connect(‘set_cat_sound’, cat.set_sound) # Connect the signal ‘set_cat_sound’ to the cat.set_sound method. mediator.connect(‘set_sound’, dog.set_sound) # Also connect the signal ‘set_sound’ to the dog.set_sound method. mediator.connect(‘set_sound’, cat.set_sound) # Also connect the signal ‘set_sound’ to the cat.set_sound method. mediator.signal(‘set_sound’, ‘foo’) # Send out the signal ‘set_sound’ # with an argument to be passed to the connected methods. # (Sets dog.sound and cat.sound to ‘foo’) assert ‘foo’ == dog.sound assert ‘foo’ == cat.sound mediator.signal(‘set_dog_sound’, ‘woof’) # Send out the signal ‘set_dog_sound’ # (Sets dog.sound to ‘woof’) mediator.signal(‘set_cat_sound’, ‘meow’) # Send out the signal ‘set_cat_sound’ # (Sets cat.sound to ‘meow’) assert ‘woof’ == dog.sound assert ‘meow’ == cat.sound mediator.disconnect(‘set_sound’, dog.set_sound) # Disconnect the method dog.sound from the signal ‘set_sound’ mediator.signal(‘set_sound’, ‘bar’) # Send out the signal ‘set_sound’ # (Only sets cat.sound to ‘bar’ because we disconnected dog.sound) assert ‘woof’ == dog.sound assert ‘bar’ == cat.sound

纪念品图案

保存对象的状态并在稍后回滚到该状态。from pypattyrn.behavioral.memento import Originator class Cat(Originator): # This object is an Originator def __init__(self, name): self.name = name cat = Cat(‘Tom’) # Initialize a Cat with the name ‘Tom’ assert cat.name == ‘Tom’ cat_memento = cat.commit() # Save the cats current state to a Memento object. cat.name = ‘jerry’ # Change the cats name to ‘jerry’ assert cat.name == ‘jerry’ cat.rollback(cat_memento) # Restore the cats state to the memento object we saved. assert ‘Tom’ == cat.name # The cats name was changed back to ‘Tom’ as expected.

空对象模式

将不存在的对象封装为具有中性行为的对象。from pypattyrn.behavioral.null import Null # You can initialize a Null object with any parameters. try: Null() Null(‘value’) Null(‘value’, param=’value’) except: raise AssertionError() null = Null() # Attempting to set parameters won’t do anything and won’t error try: null.foo = ‘foo’ null.foo.bar = ‘bar’ except: raise AssertionError() # Calling on a null object in any way will just return that null object. assert null == null() assert null == null(‘value’) assert null == null(‘value’, param=’value’) assert null == null.foo assert null == null.foo.bar assert null == null.foo.bar.method1() assert null == null.foo.bar.method1().method2(‘foo’, bar=’bar’).attr3 assert str(null) == ” # Null objects string representation is the empty string. assert repr(null) == ” # Null objects repr is the empty string. assert bool(null) is False # Null object evaluates to False as a boolean. # Trying to delete attributes doesn’t do anything and won’t error. try: del null.foo del null.foo.bar except: raise AssertionError()

观察者模式

当单个对象的状态发生变化时通知许多对象。from pypattyrn.behavioral.observer import Observable, Observer class ConcreteObservable(Observable): # This object is an Observable. _kinda_private_var = ‘I am kinda private’ __private_var = ‘I am more private’ def change_state(self, **kwargs): # Some method to change this objects state and notify observers. for key, value in kwargs.items(): setattr(self, key, value) # Change the objects state. self.notify() # Notify all observers of the change in state. # Will call the update method of each attached observer with kwargs that come # from this objects __dict__ attribute, minus any private variables (starts with __ or _) class ConcreteObserver(Observer): # This object is an Observer. updated_state = None def update(self, **state): # Implement the update method. # Called when an attached observable calls its notify method. self.updated_state = state observable = ConcreteObservable() # Create an Observable observer_1 = ConcreteObserver() # Create Observers observer_2 = ConcreteObserver() observer_3 = ConcreteObserver() observable.attach(observer_1) # Attach each of the Observers to the Observable observable.attach(observer_2) observable.attach(observer_3) observable.change_state(foo=’foo’, bar=’bar’) # Change the Observable’s state. # Also calls notify which will call each Observers update method. expected_state = {‘foo’: ‘foo’, ‘bar’: ‘bar’} # Make sure each Observers state was changed accordingly when the notify method was called by the Observable. assert sorted(expected_state.keys()) == sorted(observer_1.updated_state.keys()) and \ sorted(expected_state.values()) == sorted(observer_1.updated_state.values()) assert sorted(expected_state.keys()) == sorted(observer_2.updated_state.keys()) and \ sorted(expected_state.values()) == sorted(observer_2.updated_state.values()) assert sorted(expected_state.keys()) == sorted(observer_3.updated_state.keys()) and \ sorted(expected_state.values()) == sorted(observer_3.updated_state.values()) observable.detach(observer_1) # Detach Observer 1 from the Observable observable.change_state(bar=’foobar’) # Change the Observables state. expected_state_2 = {‘foo’: ‘foo’, ‘bar’: ‘foobar’} # Make sure each Observers state was changed accordingly when notify was called by the Observable, # Except for observer_1 because we detached that Observer from the Observable. assert sorted(expected_state_2.keys()) != sorted(observer_1.updated_state.keys()) or \ sorted(expected_state_2.values()) != sorted(observer_1.updated_state.values()) assert sorted(expected_state_2.keys()) == sorted(observer_2.updated_state.keys()) and \ sorted(expected_state_2.values()) == sorted(observer_2.updated_state.values()) assert sorted(expected_state_2.keys()) == sorted(observer_3.updated_state.keys()) and \ sorted(expected_state_2.values()) == sorted(observer_3.updated_state.values())

访客模式

向现有类添加新操作而不修改它。from pypattyrn.behavioral.visitor import Visitee, Visitor class A(Visitee): # This object is a Visitee pass class B(A): # This object is an A, which also makes this a Visitee. pass class C(B): # This object is a B, which also makes this a Visitee. pass class D(Visitee): # This object is a Visitee pass class NodeVisitor(Visitor): # This object is a Visitor def generic_visit(self, node, *args, **kwargs): # Implement the generic visit method. # This is called when there is no visit_ method # implemented for a particular visitee. return ‘generic_visit ‘ + node.__class__.__name__ def visit_b(self, node, *args, **kwargs): # Called when this object visits a Visitee of class ‘B’. return ‘visit_b ‘ + node.__class__.__name__ def visit_d(self, node, *args, **kwargs): # Called when this object visits a Visitee of class ‘D’. return ‘visit_d {0} args: {1} kwargs: {2}’.format(node.__class__.__name__, args, kwargs) # Create Visitee’s node_a = A() node_b = B() node_c = C() node_d = D() # Create Visitor node_visitor = NodeVisitor() assert ‘generic_visit A’ == node_a.accept(node_visitor) # Visit node_a with the Visitor. # Since the visitor does not have a visit_a method, # generic_visit is called. assert ‘visit_b B’ == node_b.accept(node_visitor) # Visit node_b with the Visitor. # Calls the visitors visit_b method. assert ‘visit_b C’ == node_c.accept(node_visitor) # Visit node_c with the Visitor. # Even though the visitor does not have a visit_c method, # since node_c inherits B, the Visitors visit_b method is called. # Visit node_d with the Visitor. Calls the visitors visit_d method. assert “visit_d D args: (‘foo’, ‘bar’) kwargs: {‘foobar’: ‘foobar’}” == node_d.accept(node_visitor, ‘foo’, ‘bar’, foobar=’foobar’)


创作模式

处理对象创建的模式。


建造者模式

将对象构造与其表示分开。from pypattyrn.creational.builder import Builder, Director class Building(object): # The object being constructed. def __init__(self): self.floor = None self.size = None def __repr__(self): return ‘Floor: {0.floor} | Size: {0.size}’.format(self) class HomeBuilder(Builder): # A base Builder class for constructing homes. def __init__(self): super().__init__(Building()) # Initialize the Builder class with a Building instance. self._register(‘floor’, self._build_floor) # Register the keyword ‘floor’ with the _build_floor method. self._register(‘size’, self._build_size) # Register the keyword ‘size’ with the _build_size method. def _build_floor(self): pass def _build_size(self): pass class HouseBuilder(HomeBuilder): # A concrete HomeBuilder class for constructing houses def _build_floor(self): self.constructed_object.floor = ‘One’ # Alter the Building’s floor attribute. def _build_size(self): self.constructed_object.size = ‘Big’ # Alter the Building’s size attribute. class FlatBuilder(HomeBuilder): # A concrete HomeBuilder class for constructing flats def _build_floor(self): self.constructed_object.floor = ‘More than one’ # Alter the Building’s floor attribute. def _build_size(self): self.constructed_object.size = ‘Small’ # Alter the Building’s size attribute. class HomeDirector(Director): # A Director class for managing home construction. def construct(self): self.builder.build(‘floor’) # Build the floor part of the Building by using the keyword ‘floor’ self.builder.build(‘size’) # Build the size part of the Building by using the keyword ‘size’ home_director = HomeDirector() home_director.builder = HouseBuilder() # Use the house builder. home_director.construct() # Construct the house. house = home_director.get_constructed_object() # Get the constructed house. print(repr(house)) #Floor: One | Size: Big home_director.builder = FlatBuilder() # Use the flat builder. home_director.construct() # Construct the flat. house = home_director.get_constructed_object() # Get the constructed flat. print(repr(house)) #Floor: More than one | Size: Small

工厂模式

用于创建对象的接口。from pypattyrn.creational.factory import Factory # This is just an interface class Cat(object): def speak(self): print(‘meow’) class Dog(object): def speak(self): print(‘woof’) class AnimalFactory(Factory): # A factory class for creating animals. def create(self, animal_type): # Implement the abstract create method. if animal_type == ‘cat’: return Cat() elif animal_type == ‘dog’: return Dog() else: return None animal_factory = AnimalFactory() cat = animal_factory.create(‘cat’) dog = animal_factory.create(‘dog’) cat.speak() # ‘meow’ dog.speak() # ‘woof’

抽象工厂模式

从一系列工厂创建一个实例。from pypattyrn.creational.factory import Factory, AbstractFactory class Cat(object): def speak(self): print(‘meow’) class Dog(object): def speak(self): print(‘woof’) class Ant(object): def march(self): print(‘march’) class Fly(object): def fly(self): print(‘fly’) class AnimalFactory(Factory): # A factory class for creating animals. def create(self, animal_type): # Implement the abstract create method. if animal_type == ‘cat’: return Cat() elif animal_type == ‘dog’: return Dog() else: return None class InsectFactory(Factory): # A factory class for creating insects. def create(self, insect_type): # Implement the abstract create method. if insect_type == ‘ant’: return Ant() elif insect_type == ‘fly’: return Fly() else: return None class CreatureFactory(AbstractFactory): # A Factory class for creating creatures. def __init__(self): super().__init__() self._register(‘insect_factory’, InsectFactory()) # Register an InsectFactory with a keyword. self._register(‘animal_factory’, AnimalFactory()) # Register an AnimalFactory with a keyword. def create(self, creature_type): # Implement the Abstract create method. if creature_type == ‘cat’ or creature_type == ‘dog’: return self._factories[‘animal_factory’].create(creature_type) # Use the AnimalFactory elif creature_type == ‘ant’ or creature_type == ‘fly’: return self._factories[‘insect_factory’].create(creature_type) # Use the InsectFactory else: return None creature_factory = CreatureFactory() cat = creature_factory.create(‘cat’) dog = creature_factory.create(‘dog’) ant = creature_factory.create(‘ant’) fly = creature_factory.create(‘fly’) cat.speak() # ‘meow’ dog.speak() # ‘woof’ ant.march() # ‘march’ fly.fly() # ‘fly’

对象池模式

提供一个实例化对象池,可以检出并返回这些对象,而不是一直创建新对象。from pypattyrn.creational.pool import Reusable, Pool class Dog(Reusable): def __init__(self, sound): self.sound = sound super().__init__() class DogPool(Pool): def __init__(self): super().__init__(Dog, ‘woof’) dog_pool = DogPool() dog_one = dog_pool.acquire() dog_two = dog_pool.acquire() dog_two.sound = ‘meow’ dog_pool.release(dog_one) dog_three = dog_pool.acquire() dog_pool.release(dog_two) dog_four = dog_pool.acquire() assert id(dog_one) == id(dog_three) assert id(dog_two) == id(dog_four) assert dog_one.sound == dog_two.sound assert dog_three.sound == dog_four.sound assert dog_one.sound == dog_four.sound

原型模式

克隆一个对象以产生新的对象。from pypattyrn.creational.prototype import Prototype class Point(Prototype): def __init__(self, x, y): self.x = x self.y = y def move(self, x, y): self.x += x self.y += y point_one = Point(15, 15) point_two = point_one.prototype(z=20) point_three = point_two.prototype() assert point_one.x == point_two.x assert point_one.y == point_two.y assert not hasattr(point_one, ‘z’) assert hasattr(point_two, ‘z’) assert point_two.z == 20 assert point_three.__dict__ == point_two.__dict__ from math import sqrt def distance_to(this, other): return sqrt((this.x – other.x) ** 2 + (this.y – other.y) ** 2) point_four = point_three.prototype(distance_to=distance_to) assert hasattr(point_four, ‘distance_to’) assert point_four.distance_to(point_three) == 0

单例模式

确保类只存在一个实例。from pypattyrn.creational.singleton import Singleton class DummySingletonOne(object, metaclass=Singleton): def __init__(self): pass class DummySingletonTwo(object, metaclass=Singleton): def __init__(self): pass dummy_class_one_instance_one = DummySingletonOne() dummy_class_one_instance_two = DummySingletonOne() dummy_class_two_instance_one = DummySingletonTwo() dummy_class_two_instance_two = DummySingletonTwo() assert id(dummy_class_one_instance_one) == id(dummy_class_one_instance_two) assert id(dummy_class_two_instance_one) == id(dummy_class_two_instance_two) assert id(dummy_class_one_instance_one) != id(dummy_class_two_instance_one) assert id(dummy_class_one_instance_two) != id(dummy_class_two_instance_two)


结构模式

处理对象组合的模式


适配器模式

将对象包装到客户端期望的接口中。from pypattyrn.structural.adapter import Adapter class Dog(object): def __init__(self): self.name = “Dog” def bark(self): return “woof!” class Cat(object): def __init__(self): self.name = “Cat” def meow(self): return “meow!” cat = Cat() dog = Dog() cat_adapter = Adapter(cat, make_noise=cat.meow) dog_adapter = Adapter(dog, make_noise=dog.bark) assert cat_adapter.make_noise == cat.meow assert dog_adapter.make_noise == dog.bark assert cat_adapter.make_noise() == ‘meow!’ assert dog_adapter.make_noise() == ‘woof!’ assert cat_adapter.name == ‘Cat’ assert dog_adapter.name == ‘Dog’ assert cat_adapter.meow() == ‘meow!’ assert dog_adapter.bark() == ‘woof!’ assert cat_adapter.original_dict() == cat.__dict__ assert dog_adapter.original_dict() == dog.__dict__ bad_cat_adapter = Adapter(cat, foo=cat.name) try: bad_cat_adapter.foo except AttributeError: pass else: raise AssertionError() bad_dog_adapter = Adapter(dog, make_noise=cat.meow) try: bad_dog_adapter.make_noise() except AttributeError: pass else: raise AssertionError()

复合图案

将对象组成一个可以统一处理的对象树结构。from pypattyrn.structural.composite import Composite class Component(object): def do_something(self): pass class Leaf(Component): def __init__(self): self.did_something = False def do_something(self): self.did_something = True leaf_one = Leaf() leaf_two = Leaf() leaf_three = Leaf() composite_one = Composite(Component) composite_two = Composite(Component) composite_three = Composite(Component) composite_one.add_component(leaf_one) composite_two.add_component(leaf_two) composite_three.add_component(leaf_three) composite_two.add_component(composite_three) composite_one.add_component(composite_two) assert set() == {leaf_one, composite_two}.symmetric_difference(composite_one.components) assert set() == {leaf_two, composite_three}.symmetric_difference(composite_two.components) assert set() == {leaf_three}.symmetric_difference(composite_three.components) composite_two.remove_component(composite_three) assert set() == {leaf_two}.symmetric_difference(composite_two.components) assert not leaf_one.did_something assert not leaf_two.did_something assert not leaf_three.did_something composite_one.do_something() assert leaf_one.did_something assert leaf_two.did_something assert not leaf_three.did_something

装饰模式

将附加功能附加到函数上。import time from pypattyrn.structural.decorator import DecoratorSimple, DecoratorComplex, CallWrapper class TimeThis(DecoratorSimple): def __call__(self, *args, **kwargs): start = time.time() result = self.func(*args, **kwargs) end = time.time() – start return result, end class SlowClass(object): @TimeThis def slow_function(self, n): time.sleep(n) return ‘foo’ slow_class = SlowClass() result = slow_class.slow_function(1) assert result[0] == ‘foo’ assert 1 <= result[1] <= 2 class Alert(DecoratorComplex): def __init__(self, alert_time): self.alert_time = alert_time @CallWrapper def __call__(self, func, *args, **kwargs): start = time.time() return_val = func(*args, **kwargs) end = time.time() – start if end > self.alert_time: return return_val, True return return_val, False class SlowClass(object): @Alert(1) def slow_function_true(self, n): time.sleep(n) return n @Alert(1) def slow_function_false(self, n): return n slow_class = SlowClass() assert (2, True) == slow_class.slow_function_true(2) assert (10, False) == slow_class.slow_function_false(10)

享元模式

与其他类似对象共享数据以提高效率。from pypattyrn.structural.flyweight import FlyweightMeta class Card(object, metaclass=FlyweightMeta): def __init__(self, suit, value): self.suit = suit self.value = value three_of_spades = Card(‘Spade’, 3) four_of_spades = Card(‘Spade’, 4) three_of_spades_two = Card(‘Spade’, 3) assert id(three_of_spades) == id(three_of_spades_two) assert id(three_of_spades) != id(four_of_spades)

留下评论

您的邮箱地址不会被公开。 必填项已用 * 标注