复合设计模式叶子管理
Composite Design Pattern Leaf Management
我看到的大多数复合设计模式的描述都让 Composite
实现了 add()
和 remove()
方法,并在 Leaf
中保留了这些方法未实现对象。例如,关于该主题的 Wiki Page 中的图表明了这一点,它与 GoF 图大致相同。
关于在父 Component
class 中实现这些,GoF 有以下说法:
Defining the child management interface at the root of the class hierarchy gives you transparency, because you can treat all components uniformly. It costs you safety, however, because clients may try to do meaningless things like add and remove objects from leaves.
我同意 Leaf
工具 remove()
处理起来很奇怪。 (你自己删除了吗?你必须实现某种 NullLeaf
对象吗?)但由于模式的重点是使 Leaf
s 和 Composite
s 的行为方式与我相同不明白为什么 add()
可以在 Component
.
中实现
我的问题: 为什么不能 Component
至少实现 add()
?这样做会违反任何关键的设计原则吗?这样做不再使它成为复合设计模式吗?下面是 Python 中的一个例子,它抓住了我试图在自己的工作中实现的本质:
class Component:
def add(self, other):
# in Python, may be more natural to define __add__
c = Composite()
c.children = self.children + other.children
return c
class Leaf(Component):
def __init__(self):
self.children = [self] # possibly strange?
def operation(self):
# operation specific to this leaf
class Composite(Component):
def __init__(self):
self.children = []
def operation(self):
for child in self.children:
child.operation()
"example usage":
>>> l1 = Leaf()
>>> l2 = Leaf()
>>> c = l1.add(l2) # c is a `Composite` instance
>>> c.operation()
Why can't Component at least implement add()? Would doing so violate
any key design principles? Does doing so no longer make this a
composite design pattern?
您自己回答了问题,如您所写
Defining the child management interface at the root of the class
hierarchy gives you transparency, because you can treat all components
uniformly. It costs you safety, however, because clients may try to do
meaningless things like add and remove objects from leaves.
将子管理方法添加到界面中是一种权衡。当您这样做时,您将获得统一处理层次结构中所有对象的好处。但是对于叶子,您必须决定一种策略来处理没有意义的界面。可以做的是为这些方法创建空的实现,或者在调用它们时抛出异常。另一种方法是创建一个定义 add()
和 remove()
方法的抽象基础 class Component
,然后只在 Composite
中使用该实现并使用空实现覆盖 Leaf
中的方法。所有这些实现都是 Composite
模式变体。恕我直言,这些模式不应逐字阅读,而应作为指导原则让您为自己量身定制实施。
我看到的大多数复合设计模式的描述都让 Composite
实现了 add()
和 remove()
方法,并在 Leaf
中保留了这些方法未实现对象。例如,关于该主题的 Wiki Page 中的图表明了这一点,它与 GoF 图大致相同。
关于在父 Component
class 中实现这些,GoF 有以下说法:
Defining the child management interface at the root of the class hierarchy gives you transparency, because you can treat all components uniformly. It costs you safety, however, because clients may try to do meaningless things like add and remove objects from leaves.
我同意 Leaf
工具 remove()
处理起来很奇怪。 (你自己删除了吗?你必须实现某种 NullLeaf
对象吗?)但由于模式的重点是使 Leaf
s 和 Composite
s 的行为方式与我相同不明白为什么 add()
可以在 Component
.
我的问题: 为什么不能 Component
至少实现 add()
?这样做会违反任何关键的设计原则吗?这样做不再使它成为复合设计模式吗?下面是 Python 中的一个例子,它抓住了我试图在自己的工作中实现的本质:
class Component:
def add(self, other):
# in Python, may be more natural to define __add__
c = Composite()
c.children = self.children + other.children
return c
class Leaf(Component):
def __init__(self):
self.children = [self] # possibly strange?
def operation(self):
# operation specific to this leaf
class Composite(Component):
def __init__(self):
self.children = []
def operation(self):
for child in self.children:
child.operation()
"example usage":
>>> l1 = Leaf()
>>> l2 = Leaf()
>>> c = l1.add(l2) # c is a `Composite` instance
>>> c.operation()
Why can't Component at least implement add()? Would doing so violate any key design principles? Does doing so no longer make this a composite design pattern?
您自己回答了问题,如您所写
Defining the child management interface at the root of the class hierarchy gives you transparency, because you can treat all components uniformly. It costs you safety, however, because clients may try to do meaningless things like add and remove objects from leaves.
将子管理方法添加到界面中是一种权衡。当您这样做时,您将获得统一处理层次结构中所有对象的好处。但是对于叶子,您必须决定一种策略来处理没有意义的界面。可以做的是为这些方法创建空的实现,或者在调用它们时抛出异常。另一种方法是创建一个定义 add()
和 remove()
方法的抽象基础 class Component
,然后只在 Composite
中使用该实现并使用空实现覆盖 Leaf
中的方法。所有这些实现都是 Composite
模式变体。恕我直言,这些模式不应逐字阅读,而应作为指导原则让您为自己量身定制实施。