设计模式(python实现)--组合模式(Composite)

2020-02-02  本文已影响0人  远行_2a22

Composite

动机(Motivation)

模式定义

将对象组合成树形结构以表示”部分-整体“的层次结构。Composite使得用户对单个对象和组合对象的使用具有一致性(稳定)。
——《设计模式》GoF

要点总结

例子

# -*- coding:utf-8 -*-


class Component(object):
    def __init__(self, node_name):
        self.node_name = node_name

    def process(self):
        pass


class TreeNode(Component):

    def __init__(self, node_name):
        super(TreeNode, self).__init__(node_name)
        self.node_list = []

    def process(self):
        self._process_cur_node()
        self._process_sub_node()

    def add_node(self, node):
        self.node_list.append(node)

    def remove_node(self, node):
        self.node_list.pop(node)

    def _process_cur_node(self):
        print('process cur node:', self.node_name)

    def _process_sub_node(self):
        for sub_node in self.node_list:
            sub_node.process()


class LeafNode(Component):

    def process(self):
        print('process leaf node:', self.node_name)


if __name__ == '__main__':
    root_node = TreeNode('root')
    tree_node1 = TreeNode('tree_node1')
    tree_node2 = TreeNode('tree_node2')
    tree_node3 = TreeNode('tree_node3')
    leaf_node1 = LeafNode('leaf_node1')
    leaf_node2 = LeafNode('leaf_node2')

    root_node.add_node(tree_node1)
    root_node.add_node(tree_node2)
    tree_node1.add_node(tree_node3)
    tree_node3.add_node(leaf_node1)
    tree_node3.add_node(leaf_node2)

    root_node.process()

输出

('process cur node:', 'root')
('process cur node:', 'tree_node1')
('process cur node:', 'tree_node3')
('process leaf node:', 'leaf_node1')
('process leaf node:', 'leaf_node2')
('process cur node:', 'tree_node2')

上一篇下一篇

猜你喜欢

热点阅读