java设计模式(结构型)之组合模式

第0章:简介

组合模式的定义:通过把叶子对象当成特殊的组合对象看待,从而对叶子对象和组合对象一视同仁,统统当成了Component对象,有机的统一了叶子对象和组合对象。

组合模式的本质:统一叶子对象和组合对象

参考:研磨设计模式(书籍),大话设计模式(书籍),图解设计模式(书籍)

模式图:

待补充

第1章:实践

第0节:透明性的实现

(1)组件对象(Component.java)

package com.mcc.core.designPattern.structure.composite.transparentComponent;


/**
 * 组件对象
 * 叶子对象和组合对象的父类,使叶子对象和组合对象具有一致性
 * 透明性的实现(把管理子组件的操作定义在Component中,客户端只需要面对Component,而无需关心具体的组件类型,这种实现方式就是透明性的实现。
 * 但是透明性的实现是以安全性为代价的,因为在Component中定义的一些方法,对于叶子对象来说是没有意义的,比如:增加、移除子组件对象。)
 *
 *
 * 组合模式的定义:通过把叶子对象当成特殊的组合对象看待,从而对叶子对象和组合对象一视同仁,统统当成了Component对象,有机的统一了叶子对象和组合对象。
 * 组合模式的本质:统一叶子对象和组合对象
 *
 *
 * @author <a href="mailto:[email protected]">menergy</a>
 *         My blog: https://blog.csdn.net/menergy
 *         Created by Administrator on 2018/4/26.
 */
public abstract class Component {

    //叶子对象和组合对象都有的方法,只是具体实现不同
    public abstract void method1();

    //叶子对象和组合对象都有的方法,只是具体实现不同
    //本例做输出自己及其子对象
    public abstract void method2();

    /**
     * 增加子对象
     * @param component
     */
    public void addChild(Component component){
        //缺省的实现,抛出例外,因为叶子对象没有这个功能,或者子组件没有实现这个功能
        throw new UnsupportedOperationException("对象不支持这个功能");
    }

    /**
     * 移除子对象
     * @param component
     */
    public void removeChild(Component component){
        //缺省的实现,抛出例外,因为叶子对象没有这个功能,或者子组件没有实现这个功能
        throw new UnsupportedOperationException("对象不支持这个功能");
    }

    /**
     * 获取子对象
     * @param index
     * @return
     */
    public Component getChildren(int index){
        //缺省的实现,抛出例外,因为叶子对象没有这个功能,或者子组件没有实现这个功能
        throw new UnsupportedOperationException("对象不支持这个功能");
    }


}
(2)组合对象(Composite.java)
package com.mcc.core.designPattern.structure.composite.transparentComponent;

import java.util.ArrayList;
import java.util.List;

/**
 * 组合对象
 * 表示“容器”的对象
 *
 * @author <a href="mailto:[email protected]">menergy</a>
 *         My blog: https://blog.csdn.net/menergy
 *         Created by Administrator on 2018/4/26.
 */
public class Composite extends Component {

    //用来存储组合对象中包含的子组件对象
    private List<Component> childComponents = null;

    @Override
    public void method1() {
        System.out.println("组合对象method方法");
    }

    @Override
    public void method2() {
        System.out.println("我是组合对象:" + this + ",下面是我的子树对象:");
        if(childComponents != null && childComponents.size() > 0){
            for(Component c : childComponents) {
                c.method2();
            }
            System.out.println("------------------------------------------");
        }

    }

    /**
     * 增加子对象
     * 复写父类方法
     * @param component
     */
    public void addChild(Component component){
        //延迟初始化
        if (childComponents == null) {
            childComponents = new ArrayList<Component>();
        }
        childComponents.add(component);
    }

    /**
     * 移除子对象
     * 复写父类方法
     * @param component
     */
    public void removeChild(Component component){
        if(childComponents != null){
            childComponents.remove(component);
        }
    }

    /**
     * 获取子对象
     * 复写父类方法
     * @param index
     * @return
     */
    public Component getChildren(int index){

        if (childComponents != null){
            if(index>=0 && index<childComponents.size()){
                return childComponents.get(index);
            }
        }
        return null;
    }


}

(3)叶子对象(Leaf.java

package com.mcc.core.designPattern.structure.composite.transparentComponent;

/**
 *  叶子对象
 *  表示“内容”的对象
 *
 * @author <a href="mailto:[email protected]">menergy</a>
 *         My blog: https://blog.csdn.net/menergy
 *         Created by Administrator on 2018/4/26.
 */
public class Leaf extends Component {
    @Override
    public void method1() {
        System.out.println("叶子对象method1方法");
    }

    @Override
    public void method2() {
        System.out.println("我是叶子对象:" + this);
    }
}
(4)客户端测试(Client.java)

package com.mcc.core.designPattern.structure.composite.transparentComponent;

/**
 * 客户端测试
 *
 * @author <a href="mailto:[email protected]">menergy</a>
 *         My blog: https://blog.csdn.net/menergy
 *         Created by Administrator on 2018/4/26.
 */
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);
        root.addChild(leaf1);
        c1.addChild(leaf2);
        c2.addChild(leaf3);
        c2.addChild(leaf4);

        //输出树结构,从左到右遍历树
        root.method2();

        //不安全性展示
        leaf1.addChild(leaf2);
    }
}

第1节:安全性的实现

(1)组件对象(Component.java)

package com.mcc.core.designPattern.structure.composite.securityComponent;


/**
 * 组件对象
 * 叶子对象和组合对象的父类,使叶子对象和组合对象具有一致性
 * 安全性的实现(把管理子组件的操作定义在Composite中,那么客户在使用叶子对象的时候,就不会发生使用添加子组件或是删除子组件的操作了,因为压根就没有这样的功能,这种实现方式是安全的。
 * 但是这样一来,客户端在使用的时候,就必须区分到底使用的是Composite对象,还是叶子对象,不同对象的功能是不一样的。也就是说,这种实现方式,对客户而言就不是透明的了。)
 *
 *
 * 组合模式的定义:通过把叶子对象当成特殊的组合对象看待,从而对叶子对象和组合对象一视同仁,统统当成了Component对象,有机的统一了叶子对象和组合对象。
 * 组合模式的本质:统一叶子对象和组合对象
 *
 *
 * @author <a href="mailto:[email protected]">menergy</a>
 *         My blog: https://blog.csdn.net/menergy
 *         Created by Administrator on 2018/4/26.
 */
public abstract class Component {

    //叶子对象和组合对象都有的方法,只是具体实现不同
    public abstract void method1();

    //叶子对象和组合对象都有的方法,只是具体实现不同
    //本例做输出自己及其子对象
    public abstract void method2();

}

(2)组合对象(Composite.java

package com.mcc.core.designPattern.structure.composite.securityComponent;

import java.util.ArrayList;
import java.util.List;

/**
 * 组合对象
 * 表示“容器”的对象
 *
 * @author <a href="mailto:[email protected]">menergy</a>
 *         My blog: https://blog.csdn.net/menergy
 *         Created by Administrator on 2018/4/26.
 */
public class Composite extends Component {

    //用来存储组合对象中包含的子组件对象
    private List<Component> childComponents = null;

    @Override
    public void method1() {
        System.out.println("组合对象method方法");
    }

    @Override
    public void method2() {
        System.out.println("我是组合对象:" + this + ",下面是我的子树对象:");
        if(childComponents != null && childComponents.size() > 0){
            for(Component c : childComponents) {
                c.method2();
            }
            System.out.println("------------------------------------------");
        }

    }

    /**
     * 增加子对象
     * @param component
     */
    public void addChild(Component component){
        //延迟初始化
        if (childComponents == null) {
            childComponents = new ArrayList<Component>();
        }
        childComponents.add(component);
    }

    /**
     * 移除子对象
     * @param component
     */
    public void removeChild(Component component){
        if(childComponents != null){
            childComponents.remove(component);
        }
    }

    /**
     * 获取子对象
     * @param index
     * @return
     */
    public Component getChildren(int index){

        if (childComponents != null){
            if(index>=0 && index<childComponents.size()){
                return childComponents.get(index);
            }
        }
        return null;
    }


}

(3)叶子对象(Leaf.java

package com.mcc.core.designPattern.structure.composite.securityComponent;

/**
 *  叶子对象
 *  表示“内容”的对象
 *
 * @author <a href="mailto:[email protected]">menergy</a>
 *         My blog: https://blog.csdn.net/menergy
 *         Created by Administrator on 2018/4/26.
 */
public class Leaf extends Component {
    @Override
    public void method1() {
        System.out.println("叶子对象method1方法");
    }

    @Override
    public void method2() {
        System.out.println("我是叶子对象:" + this);
    }
}

(4)客户端测试(Client.java

package com.mcc.core.designPattern.structure.composite.securityComponent;

/**
 * 客户端测试
 *
 * @author <a href="mailto:[email protected]">menergy</a>
 *         My blog: https://blog.csdn.net/menergy
 *         Created by Administrator on 2018/4/26.
 */
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);
        root.addChild(leaf1);
        c1.addChild(leaf2);
        c2.addChild(leaf3);
        c2.addChild(leaf4);

        //输出树结构,从左到右遍历树
        root.method2();
    }
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章