首页 > 其他分享 >结构型设计模式-组合(对象树) Composite

结构型设计模式-组合(对象树) Composite

时间:2023-09-06 14:00:55浏览次数:32  
标签:function Leaf Composite Component component public 设计模式 节点 结构型

简介

组合模式又叫对象树,将对象按照树形结构串起来,呈现出局部和整体相同的特性。

树中每个节点都由两部分组成,首先节点有自己的业务数据,其次节点内可以含有子节点集合。

比如盒子中可以放物品,也可以放其他小盒子,小盒子里又可以放物品和其他更小的盒子。

当计算盒子的物品价格时,只需要将盒子里本身的物品价格,加上小盒子里所有物品价格即可,递归处理。

类图

角色

  • 抽象节点 Component

    定义一个节点的基础方法,如内部添加子节点等

  • 具体节点之叶子节点 Leaf

    叶子节点没有子节点

  • 具体节点之非叶子节点 Composite/Container

    非叶子节点,可添加子节点

或者也可以不区分是否是叶子节点,都视为节点即可

类图

图中显示,Leaf 和 Composite 都实现 Component 接口。

Composite 可添加或删除子节点,execute 则指派调用子节点的 execute 方法。

Leaf 的 execute 是真正执行逻辑的地方

类图

代码

<?php

abstract class Component
{
    protected $parent;

    public function setParent(Component $parent)
    {
        $this->parent = $parent;
    }

    public function getParent(): Component
    {
        return $this->parent;
    }

    public function add(Component $component): void { }

    public function remove(Component $component): void { }

    public function isComposite(): bool
    {
        return false;
    }

    abstract public function operation(): string;
}

class Leaf extends Component
{
    public function operation(): string
    {
        return "Leaf";
    }
}

class Composite extends Component
{
    protected $children;

    public function __construct()
    {
        $this->children = new \SplObjectStorage();
    }

    public function add(Component $component): void
    {
        $this->children->attach($component);
        $component->setParent($this);
    }

    public function remove(Component $component): void
    {
        $this->children->detach($component);
        $component->setParent(null);
    }

    public function isComposite(): bool
    {
        return true;
    }

    public function operation(): string
    {
        $results = [];
        foreach ($this->children as $child) {
            $results[] = $child->operation();
        }

        return "Branch(" . implode("+", $results) . ")";
    }
}

function clientCode(Component $component)
{
    echo "RESULT: " . $component->operation();
}

// 只有一个叶子节点
$simple = new Leaf();
echo "Client: I've got a simple component:\n";
clientCode($simple);

echo "\n";

// 构建一个tree
$tree = new Composite();
$branch1 = new Composite();
$branch1->add(new Leaf());
$branch1->add(new Leaf());
$branch2 = new Composite();
$branch2->add(new Leaf());
$tree->add($branch1);
$tree->add($branch2);
echo "Client: Now I've got a composite tree:\n";
clientCode($tree);

// 合并两个tree
function clientCode2(Component $component1, Component $component2)
{
    if ($component1->isComposite()) {
        $component1->add($component2);
    }
    echo "RESULT: " . $component1->operation();
}

echo "\n";
echo "Client: I don't need to check the components classes even when managing the tree:\n";
clientCode2($tree, $simple);

output:

Client: I've got a simple component:
RESULT: Leaf
Client: Now I've got a composite tree:
RESULT: Branch(Branch(Leaf+Leaf)+Branch(Leaf))
Client: I don't need to check the components classes even when managing the tree:
RESULT: Branch(Branch(Leaf+Leaf)+Branch(Leaf)+Leaf)

本文由mdnice多平台发布

标签:function,Leaf,Composite,Component,component,public,设计模式,节点,结构型
From: https://www.cnblogs.com/caipi/p/17682147.html

相关文章

  • 结构型设计模式-桥接(模块化) Bridge
    简介桥接模式可将一系列紧密相关的、水平等级的类,转变为组合关系,形成垂直等级关系。如抽象类Color、Shape,分别有RedColor、BlueColor、CircleShape、SquareShape的实现类,那么想创建红色方形,则可以将Shape类中持有Color引用,动态向Shape中注入Color实现即可。否则分别......
  • 结构型设计模式-适配器 Adapter
    结构型设计模式-适配器Adapterdate:April13,2021slug:design-pattern-adapterstatus:Publishedtags:设计模式type:Page简介适配器模式是一种结构型设计模式,它能使接口不兼容的对象能够相互合作角色Client接口/Target目标接口用户使用的接口Adaptee被......
  • 创建型设计模式-原型 Prototype
    简介原型模式支持按照一个对象为模板,创建出另一个一模一样的对象。简单说就是把A对象的属性,都赋值到B上,注意必须是深拷贝,即clone后的AB关联的对象是不同的对象。角色抽象原型类定义clone方法具体实现类实现clone方法类图代码classPrototype{p......
  • 创建型设计模式-单例 Singleton
    简介全局共用一个对象,好处是对象只实例化一次,减少资源占用角色单例类返回本类的唯一一个实例代码classSingleton{privatestatic$instances=[];protectedfunction__construct(){}protectedfunction__clone(){}publicfunction__wa......
  • 学习笔记-设计模式-创建型模式-工厂模式
    工厂模式工厂模式是一种创建者设计模式,细分之下可以分成三类简单工厂模式,工厂方法模式和抽象工厂模式。简单工厂模式最简单的工厂模式,它采用静态方法的方式来决定应该应该生产什么商品。publicclassStoreFactory{publicstaticICommoditygetCommodityService(Integ......
  • 【23种设计模式】桥接模式(七)
    前言【桥接模式】是【结构型】设计模式的第二个模式,也有叫【桥模式】的,英文名称:BridgePattern。大家第一次看到这个名称会想到什么呢?我第一次看到这个模式根据名称猜肯定是连接什么东西的。因为桥在我们现实生活中经常是连接着A地和B地,再往后来发展,桥引申为一种纽带,比如:丝绸之......
  • 单例设计模式
    单例设计模式基本介绍类的单例设计模式,就是采取一定的方式保证在整个的软件系统中,对某个类只能有一个对象实例存在,且类提供一个静态方法,用以获取该对象。例如Hibernate的SessionFactory,它是sql会话工厂,这个对象一定是很重的(创建需要加载很多资源和时间),一般情况下,只需要一个Se......
  • 设计模式- 单例模式
    单例模式是一种创建型设计模式,可以保证一个类型有且只有一个实例存在单例模式的适用于什么场景当一个类在程序运行期间只需要一个实例的时候,就可以考虑将其做成单例模式例如一些全局的配置,用来储存程序运行期间全局的共享配置,或者可以做一个简单的消息管道,程序中所有......
  • 设计模式备忘录+命令模式实现Word撤销恢复操作
    文章目录前言思路代码实现uml类图总结前言最近学习设计模式行为型的模式,学到了备忘录模式提到这个模式可以记录一个对象的状态属性值,用于下次复用,于是便想到了我们在Windows系统上使用的撤销操作,于是便想着使用这个模式进行一次模仿复现思路以下是按照备忘录和命令模式结合的思路......
  • 设计模式行为模式-访问者模式
    文章目录介绍基本原理1.定义访问者模式2.角色及其职责2.1元素(Element)2.2具体元素(ConcreteElement)2.3访问者(Visitor)2.4具体访问者(ConcreteVisitor)2.5对象结构(ObjectStructure)3.访问者模式的工作流程适用场景1.对象结构稳定,但需要经常增加新的操作2.处理复杂的对象结构,并......