gpt4 book ai didi

How to design Composite Pattern with Lazy Initialization and Null Object Pattern while SOLID Principles are followed? [closed](如何在遵循可靠原则的情况下,用惰性初始化和空对象模式设计复合模式?[已关闭])

翻译 作者:bug小助手 更新时间:2023-10-26 21:40:25 24 4
gpt4 key购买 nike




An Python example of tree shows the initial idea of Composite Pattern and Lazy Initialization.

一个树的Python示例展示了复合模式和延迟初始化的初始思想。


class Node:

def __init__(self, val):
self._val = val
self._lchild = None
self._rchild = None

def set_lchild(self, node):
self._lchild = node

def set_rchild(self, node):
self._rchild = node


def sum(self):
sum = self._val
if self._lchild is not None:
sum += self._lchild.sum()
if self._rchild is not None:
sum += self._rchild.sum()
return sum


root = Node(1)
lchild = Node(2)
root.set_lchild(lchild)
root.sum()


To avoid null check, Null Object Pattern is introduced.

为了避免空检查,引入了空对象模式。


class Node(ABC):
def __init__(self, val):
self._val = val
self._lchild = NullNode(0)
self._rchild = NullNode(0)

def set_lchild(self, node):
self._lchild = node

def set_rchild(self, node):
self._rchild = node

@abstractmethod
def is_null(self):
return NotImplemented

@abstractmethod
def sum(self):
return NotImplemented



class RegularNode(Node):

def is_null(self):
return False


def sum(self):
sum = self._val
if not self._lchild.is_null():
sum += self._lchild.sum()
if not self._rchild.is_null():
sum += self._rchild.sum()
return sum


class NullNode(Node):
def is_null(self):
return True

def sum(self):
return 0


root = RegularNode(1)
lchild = RegularNode(2)
root.set_lchild(lchild)
root.sum()

To obey Liskov Substitution Principle, the example is revised.

为了遵守利斯科夫替换原理,对算例进行了修正。



class Node(ABC):

@abstractmethod
def is_null(self):
return NotImplemented

@abstractmethod
def sum(self):
return NotImplemented

class RegularNode(Node):

def __init__(self, val):
self._val = val
self._lchild = NullNode()
self._rchild = NullNode()

def set_lchild(self, node):
self._lchild = node

def set_rchild(self, node):
self._rchild = node

def is_null(self):
return False

def sum(self):
sum = self._val
if not self._lchild.is_null():
sum += self._lchild.sum()
if not self._rchild.is_null():
sum += self._rchild.sum()
return sum

class NullNode(Node):

def is_null(self):
return True

def sum(self):
return 0

root = RegularNode(1)
lchild = RegularNode(2)
root.set_lchild(lchild)
root.sum()

This makes RegularNode depend on its sibling NullNode.

这使得RegularNode依赖于其同级NullNode。


And I'm not sure whether the dependency violates Dependency Inversion Principle.

而且我不确定这种依赖是否违反了依赖倒置原则。


If so, how to satisfy requirements above while no more SOLID Principle is violated?

如果是这样的话,如何在不违反更可靠的原则的情况下满足上述要求?


In the examples above, any attemption will violate an SOLID principle.

在上面的例子中,任何尝试都将违反坚实的原则。


The best scenario is no SOLID Principle is violated.
Otherwise, which principle would be OK to compromise in normal cases?

最好的情况是没有可靠的原则被违反。否则,在正常情况下,哪种原则可以妥协?


更多回答

Please try Code Review instead.

请改为尝试代码审查。

优秀答案推荐

The whole point of the Null Object Pattern is to avoid checking whether something is null or not. Instead, the null object does nothing, or returns values appropriate for the "nothing" case.

Null对象模式的全部意义在于避免检查某物是否为空。相反,空对象什么也不做,或者返回适合“Nothing”情况的值。


In your case, you've already done this with the sum method by declaring the NullNode implementation to be

在您的例子中,您已经通过将NullNode实现声明为


    def sum(self):
return 0

So the sum method of RegularNode no longer needs to check anything about whether the left or right child is null. Instead, the Null Object Pattern lets us simplify the sum to:

因此,RegularNode的sum方法不再需要检查Left或Right子级是否为空。相反,空对象模式使我们可以将总和简化为:


    def sum(self):
sum = self._val
sum += self._lchild.sum()
sum += self._rchild.sum()
return sum

This works because adding 0 has no effect.

这是可行的,因为添加0没有任何效果。


The is_null method goes away. All that is left in the base Node class is the abstract method sum. Nice and clean.

Is_NULL方法消失了。在基本Node类中剩下的只有抽象方法sum。又好又干净。


It's fine for RegularNode to depend on NullNode because the dependency is one way.

RegularNode依赖于NullNode是很好的,因为依赖是单向的。


更多回答

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com