目录
    • 抽象构件(component)角色
    • 树叶构件(leaf)角色
    • 树枝构件(composite)角色
    • 抽象构件(component)角色
    • 树叶构件(leaf)角色
    • 树枝构件(composite)角色

    什么是合成模式

    以下是互联网的解释。

    合成模式属于对象的结构模式,有时又叫做“部分——整体”模式。合成模式将对象组织到树结构中,可以用来描述整体与部分的关系。合成模式可以使客户端将单纯元素与复合元素同等看待。

    经常会出现有树结构的情况 , 其中由单独的对象或者单独对象组成的合成对象组成 , 此时就需要利用一种方式来完成树结构的构建工作 .
    合成模式提供一个树结构中所有对象的统一接口 , 规范树中单独对象和合成对象的构建过程 , 合成模式更像一个数据结构 .

    合成模式的实现方式分为透明式和安全式 , 主要区别在于管理方法是在抽象构件中声明, 还是直接在树枝构件中定义.

    • 透明式 , 管理方法在抽象构件中声明 , 同时树叶节点需要用平庸的方式实现管理方法
    • 安全式 , 在树枝构件中直接定义管理方法 , 这样避免在树叶构件中进行定义 .

    设计模式和编程语言无关,但是二当家的依然用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对象,具体的类型对于客户端而言是透明的,是无须关心的。因为无论树叶还是树枝,均符合一个固定的接口。

    到底使用安全式还是透明式需要看需求,大家看着办吧。

    以上就是java合成模式之神奇的树结构的详细内容,更多关于java合成模式的资料请关注www.887551.com其它相关文章!