Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
74 lines (55 loc) · 2.56 KB

File metadata and controls

74 lines (55 loc) · 2.56 KB
Copy raw file
Download raw file
Outline
Edit and raw actions

组合模式

我们把Composite模式看成一个复杂的属性结构,其实基本有三种角色:树干(定义一些操作树叶leaf的操作),树枝(树干上有很多树枝)和树叶(树干想要具体操作的对象) ,Composite模式帮我们实现:即它们在充当对象的时候,还是其他对象的容易,从而提供一致性

python的例子

class Trunk(object):
    '''树干'''
    def __str__(self):
        pass

    def subtree(self):
        pass

class Composite(Trunk):
    def __init__(self, left=None, right=None, length=None):
        self.left=left
        self.right=right
        self.length=length

    def __str__(self):
        # 这个结果是在调用subtree()的时候返回
        if self.length:
            return "(" + self.left.__str__() + ", " + self.right.__str__() + ")" + ": " + str(self.length) 
        else:
            return "(" + self.left.__str__() + ", " + self.right.__str__() + ")"

        # 这里其实就是一个技巧,通过这个函数返回下一级的对象,也就是它既是对象还可以是对象的容器
        def subtree(self):               
            return Composite(self.left, self.right)

class Leaf(Trunk):
    '''叶子类,它没办法继续延伸了'''
    def __init__(self, name, length=None):
        self.name = name
        self.length=length
        self.left = None
        self.right = None

    def __str__(self):
        return self.name + ": " + str(self.length)

    def subtree(self):
        return Leaf(self.name, self.length)


if __name__ == "__main__":
    # 只有叶子那么就直接返回__str__的拼装结果
    t1 = Leaf('A', 0.71399)
    print t1
    # 有个2个叶子的组合,返回的是2个叶子的对象的组合
    t2 = Composite(Leaf('B', -0.00804), 
        Leaf('C', 0.07470))
    print t2
    # 这个是嵌套的叶子的组合,树干上面有树枝,树枝上面有叶子
    t3 = Composite(Leaf('A', 0.71399),
        Composite(Leaf('B', -0.00804), 
                Leaf('C', 0.07470), 0.1533), 0.0666)

    print t3
    # 直接通过左右节点找到对应的叶子对象了
    t4 = t3.right.right.subtree()
    print t4
    # t3的左树其实就是叶子对象了
    t5 = t3.left.subtree()
    print t5
Morty Proxy This is a proxified and sanitized view of the page, visit original site.