<p>你可以用很多方法来回答这个问题,所以在没有太多上下文的情况下,我将坚持一个非常笼统的答案。你知道吗</p>
<p>这是<a href="https://en.wikipedia.org/wiki/State_pattern" rel="nofollow noreferrer">state pattern</a>的一个非常通用/粗略的实现/用例。有关实现,请参见<a href="https://github.com/faif/python-patterns/blob/master/behavioral/state.py" rel="nofollow noreferrer">this link</a>。你知道吗</p>
<pre><code>class MessageStatus(object):
def __init__(self, status_instance):
self._status_instance = status_instance # maintain a ptr to the class containing your attributes
def __call__(self):
# some code
if some_condition:
# more code
else:
message['status'] = self.cls.NEW.value
await self.status_instance.db.store_comment(message)
</code></pre>
<blockquote>
<p>The state pattern is a behavioral software design pattern that implements a state machine in an object-oriented way. With the state pattern, a state machine is implemented by implementing each individual state as a derived class of the state pattern interface, and implementing state transitions by invoking methods defined by the pattern's superclass.</p>
</blockquote>
<p>注意:根据前面的状态模式概要,如果您想使其与状态模式保持一致,我建议您重构代码,以便注释/警报状态类是泛型状态类的子类,泛型状态类将包含上面的逻辑。你知道吗</p>