java 设计模式实战,合成模式之神奇的树结构
- 非常感谢你阅读本文,欢迎【👍点赞】【⭐收藏】【📝评论】~
 - 放弃不难,但坚持一定很酷!希望我们大家都能每天进步一点点!🎉
 
什么是合成模式
以下是互联网的解释。
合成模式属于对象的结构模式,有时又叫做“部分——整体”模式。合成模式将对象组织到树结构中,可以用来描述整体与部分的关系。合成模式可以使客户端将单纯元素与复合元素同等看待。
经常会出现有树结构的情况 , 其中由单独的对象或者单独对象组成的合成对象组成 , 此时就需要利用一种方式来完成树结构的构建工作 .
合成模式提供一个树结构中所有对象的统一接口 , 规范树中单独对象和合成对象的构建过程 , 合成模式更像一个数据结构 .
合成模式的实现方式分为透明式和安全式 , 主要区别在于管理方法是在抽象构件中声明, 还是直接在树枝构件中定义.
- 透明式 , 管理方法在抽象构件中声明 , 同时树叶节点需要用平庸的方式实现管理方法 .
 - 安全式 , 在树枝构件中直接定义管理方法 , 这样避免在树叶构件中进行定义 .
 
设计模式和编程语言无关,但是二当家的依然用Java语言去实战举例。
安全式合成模式

-  
抽象构件(Component)角色:这是一个抽象角色,它给参加组合的对象定义出公共的接口及其默认行为,可以用来管理所有的子对象。合成对象通常把它所包含的子对象当做类型为Component的对象。在安全式的合成模式里,构件角色并不定义出管理子对象的方法,这一定义由树枝构件对象给出。
 -  
树叶构件(Leaf)角色:树叶对象没有下级子对象,定义出参加组合的原始对象的行为。
 -  
树枝构件(Composite)角色:代表参加组合的有下级子对象的对象,并给出树枝构件对象的行为。
 
抽象构件(Component)角色
抽象构件声明了叶子和树枝都应该有的行为。
package com.secondgod.composite;
/**
 * 抽象构件
 *
 * @author 二当家的白帽子 https://le-yi.blog.csdn.net/
 */
public interface Component {
    /**
     * 输出自身的名称
     */
    void printStruct(String preStr);
}
 
 树叶构件(Leaf)角色
树叶不会再有下级。
package com.secondgod.composite;
import java.text.MessageFormat;
/**
 * 树叶
 *
 * @author 二当家的白帽子 https://le-yi.blog.csdn.net/
 */
public class Leaf implements Component {
    /**
     * 叶子对象的名字
     */
    private String name;
    public Leaf(String name) {
        this.name = name;
    }
    @Override
    public void printStruct(String preStr) {
        System.out.println(MessageFormat.format("{0}-{1}", preStr, name));
    }
}
 
 树枝构件(Composite)角色
树枝可以继续长出树枝或者树叶,所以要有addChild方法。
package com.secondgod.composite;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
/**
 * 树枝
 *
 * @author 二当家的白帽子 https://le-yi.blog.csdn.net/
 */
public class Composite implements Component {
    /**
     * 用来存储组合对象中包含的子组件对象
     */
    private List<Component> childComponents = new ArrayList<Component>();
    /**
     * 组合对象的名字
     */
    private String          name;
    public Composite(String name){
        this.name = name;
    }
    /**
     * 聚集管理方法,增加一个子构件对象
     * @param child 子构件对象
     */
    public void addChild(Component child){
        childComponents.add(child);
    }
    @Override
    public void printStruct(String preStr) {
        // 先把自己输出
        System.out.println(MessageFormat.format("{0}+{1}", preStr, name));
        // 如果还包含有子组件,那么就输出这些子组件对象
        if (this.childComponents != null) {
            // 添加两个空格,表示向后缩进两个空格
            preStr += "  ";
            // 输出当前对象的子对象
            for (Component c : childComponents) {
                // 递归输出每个子对象
                c.printStruct(preStr);
            }
        }
    }
}
 
 使用
package com.secondgod.composite;
/**
 * 测试
 *
 * @author 二当家的白帽子 https://le-yi.blog.csdn.net/
 */
public class Client {
    public static void main(String[]args){
        Composite root = new Composite("生物");
        Composite c1 = new Composite("动物");
        Composite c2 = new Composite("植物");
        Leaf leaf1 = new Leaf("猫猫");
        Leaf leaf2 = new Leaf("狗狗");
        Leaf leaf3 = new Leaf("大树");
        Leaf leaf4 = new Leaf("小草");
        root.addChild(c1);
        root.addChild(c2);
        c1.addChild(leaf1);
        c1.addChild(leaf2);
        c2.addChild(leaf3);
        c2.addChild(leaf4);
        root.printStruct("");
    }
}
 
 
执行结果符合预期。
透明式合成模式

抽象构件(Component)角色
生长树枝和树叶的方法直接声明在抽象构件里。本例使用抽象类,其实也可以使用接口。
package com.secondgod.composite;
/**
 * 抽象构件
 *
 * @author 二当家的白帽子 https://le-yi.blog.csdn.net/
 */
public abstract class Component {
    /**
     * 输出自身的名称
     */
    public abstract void printStruct(String preStr);
    /**
     * 聚集管理方法,增加一个子构件对象
     * @param child 子构件对象
     */
    public void addChild(Component child){
        /**
         * 缺省实现,抛出异常,因为叶子对象没有此功能
         * 或者子组件没有实现这个功能
         */
        throw new UnsupportedOperationException("对象不支持此功能");
    }
}
 
 树叶构件(Leaf)角色
透明式的叶子从实现抽象构件改成继承抽象构件。如果抽象构件是接口,则需要平庸实现管理子构件的方法。
package com.secondgod.composite;
import java.text.MessageFormat;
/**
 * 树叶
 *
 * @author 二当家的白帽子 https://le-yi.blog.csdn.net/
 */
public class Leaf extends Component {
    /**
     * 叶子对象的名字
     */
    private String name;
    public Leaf(String name) {
        this.name = name;
    }
    @Override
    public void printStruct(String preStr) {
        System.out.println(MessageFormat.format("{0}-{1}", preStr, name));
    }
}
 
 树枝构件(Composite)角色
透明式的树枝也是从实现抽象构件改为继承抽象构件,这主要跟抽象构件是抽象类还是接口有关。
package com.secondgod.composite;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
/**
 * 树枝
 *
 * @author 二当家的白帽子 https://le-yi.blog.csdn.net/
 */
public class Composite extends Component {
    /**
     * 用来存储组合对象中包含的子组件对象
     */
    private List<Component> childComponents = new ArrayList<Component>();
    /**
     * 组合对象的名字
     */
    private String          name;
    public Composite(String name){
        this.name = name;
    }
    /**
     * 聚集管理方法,增加一个子构件对象
     * @param child 子构件对象
     */
    public void addChild(Component child){
        childComponents.add(child);
    }
    @Override
    public void printStruct(String preStr) {
        // 先把自己输出
        System.out.println(MessageFormat.format("{0}+{1}", preStr, name));
        // 如果还包含有子组件,那么就输出这些子组件对象
        if (this.childComponents != null) {
            // 添加两个空格,表示向后缩进两个空格
            preStr += "  ";
            // 输出当前对象的子对象
            for (Component c : childComponents) {
                // 递归输出每个子对象
                c.printStruct(preStr);
            }
        }
    }
}
 
 使用
客户端在使用时,变量可以都声明为抽象构件。
package com.secondgod.composite;
/**
 * 测试
 *
 * @author 二当家的白帽子 https://le-yi.blog.csdn.net/
 */
public class Client {
    public static void main(String[]args){
        Component root = new Composite("生物");
        Component c1 = new Composite("动物");
        Component c2 = new Composite("植物");
        Component leaf1 = new Leaf("猫猫");
        Component leaf2 = new Leaf("狗狗");
        Component leaf3 = new Leaf("大树");
        Component leaf4 = new Leaf("小草");
        root.addChild(c1);
        root.addChild(c2);
        c1.addChild(leaf1);
        c1.addChild(leaf2);
        c2.addChild(leaf3);
        c2.addChild(leaf4);
        root.printStruct("");
    }
}
 
 可以看出,客户端无需再区分操作的是树枝对象(Composite)还是树叶对象(Leaf)了;对于客户端而言,操作的都是Component对象。
安全式和透明式
-  
安全式:从客户端使用合成模式上看是否更安全,如果是安全的,那么就不会有发生误操作的可能,能访问的方法都是被支持的。
 -  
透明式:从客户端使用合成模式上,是否需要区分到底是“树枝对象”还是“树叶对象”。如果是透明的,那就不用区分,对于客户而言,都是Compoent对象,具体的类型对于客户端而言是透明的,是无须关心的。因为无论树叶还是树枝,均符合一个固定的接口。
 
到底使用安全式还是透明式需要看需求,大家看着办吧。
- 点赞
 - 收藏
 - 关注作者
 
            
           
评论(0)