首页 > 解决方案 > 使用组合而不是继承时如何在 Python 中的兄弟对象之间进行通信

问题描述

我有一个由 2 个(不同的)子对象组成的父对象。2 个子实例需要通信。例如,假设 child1 需要向 child2 发送一些东西:

import children

class Parent:
    def __init__(self):
        self.child1 = children.Child1(self.q)
        self.child2 = children.Child2(self.q)

parent = Parent()
parent.child1.send("string")

是否有推荐的模式来实现这一目标?


我能想到的最好的办法是在两个对象之间创建一个队列。这可行,但它需要接收对象来运行线程。例如:

父.py

import children
import queue
import time

class Parent:
    def __init__(self):
        
        self.q = queue.Queue()
        self.child1 = children.Child1(self.q)
        self.child2 = children.Child2(self.q)

parent = Parent()
parent.child1.send("string")
time.sleep(1)

儿童.py

import threading

class Child1:

    def __init__(self, q):
        self.q = q

    def send(self, item):
        self.q.put(item)

class Child2:

    def __init__(self, q):
        self.q = q
        self.receiver = threading.Thread(target=self.worker, daemon=True).start()

    def worker(self):
        """Process the queue"""
        while True:
            item = self.q.get()
            print(f"{item} received")

实际上,我在队列中发送的“项目”是一个函数名和一个参数列表。这基本上是这里描述的命令模式。但我不喜欢接收器线程的需要。

如果可以允许一个对象直接调用另一个对象中的方法,我会更喜欢。如果它们之间存在继承关系,并且有一个共同的父级,我可能会使用super()它:

class Child1:

    def send(self, function, arguments):
        super().child2.function(arguments)

但是在我的例子中没有继承:只是组合。有没有更好的办法?

标签: pythonmethodscomposition

解决方案


只需使用对父级的引用来构造子级:

class Child1:
    def __init__(self, parent):
        self.parent = parent

    def send(self, msg):
        self.parent.child2.print_out(msg)

class Child2:
    def __init__(self, parent):
        self.parent = parent

    def print_out(self, msg):
        print(msg)

class Parent:
    def __init__(self):
        self.child1 = Child1(self)
        self.child2 = Child2(self)

parent = Parent()
parent.child1.send("foo")

推荐阅读