JAVA的23种设计模式---组合模式

概要:

该文章参考了《设计模式之禅》一书及一些前辈的博客文章

1.该文章阐述了组合模式的基础原理及示例代码;
2.该文章适合初学设计模式的技术人员研习;
3.该文章有许多不足之处,请各位大咖指正,喷子绕道;

正文:

组合模式(合成模式、部分-整体模式):将对象组合成树形结构以表示“部分-整体”的层次结构,使得用户对单个对象和组合对象的使用具有一致性。

1.(安全模式)通用组合模式模板代码实现:

package com.csdn;
/**
 * 抽象构件
 * @author Administrator
 *
 */
public abstract class Component {
    //个体和整体都具有的共享
    public void doSomething(){
        //业务逻辑
    }
}
package com.csdn;

import java.util.ArrayList;
/**
 * 树枝构件
 * @author Administrator
 *
 */
public class Composite extends Component{
    //构件容器
    private ArrayList<Component> componentArrayList = new ArrayList<Component>();
    //增加一个叶子构件或树枝构件
    public void add(Component component){
        this.componentArrayList.add(component);
    }
    //删除一个叶子构件或树枝构件
    public void remove(Component component){
        this.componentArrayList.remove(component);
    }
    //获得分支下所有叶子构件或树枝构件
    public ArrayList<Component> getChildren(){
        return this.componentArrayList;
    }
}
package com.csdn;
/**
 * 树叶构件
 * @author Administrator
 *
 */
public class Leaf extends Component{
    public void doSomething(){
        //可以重写父类方法
    }
}
package com.csdn;
/**
 * 模拟场景
 * @author Administrator
 *
 */
public class Client {
    public static void main(String[] args) {
        //创建一个根节点
        Composite root = new Composite();
        root.doSomething();
        //创建一个树枝构件
        Composite branch = new Composite();
        //创建一个叶子节点
        Leaf leaf = new Leaf();
        //建立整体
        root.add(branch);
        branch.add(leaf);
    }
    //通过递归遍历树
    public static void display(Composite root){
        for(Component c:root.getChildren()){
            if(c instanceof Leaf){
                c.doSomething();
            }else{
                display((Composite)c);
            }
        }
    }
}

注:
a:组合模式包含了抽象构件角色、叶子构件、树枝构件这三种角色
b:安全模式的组合模式破坏了依赖倒转原则
c:只要是树形结构,只要是提现局部和整体关系且关系较深的时候,就考虑组合模式
d:组合模式的真实引用,它依靠了关系数据库的非对象储存性能,非常方便的保存了一个树形结构。
e:安全模式把树枝节点和树叶节点彻底分开,树枝节点拥有单独用来组合的方法,这种方法比较安全。

2.(透明模式)通用组合模式模板代码实现:

package com.csdn2;

import java.util.ArrayList;
/**
 * 抽象构件
 * @author Administrator
 *
 */
public abstract class Component {
    //个体和整体都具有的共享
    public void doSomething(){
        //业务逻辑
    }
    //增加一个叶子构件或树枝构件
    public abstract void add(Component component);
    //删除一个叶子构件或树枝构件
    public abstract void remove(Component component);
    //获得分支下所有叶子构件或树枝构件
    public abstract ArrayList<Component> getChildren();
}
package com.csdn2;

import java.util.ArrayList;
/**
 * 树枝构件
 * @author Administrator
 *
 */
public class Composite extends Component{
    //构件容器
    private ArrayList<Component> componentArrayList = new ArrayList<Component>();
    //增加一个叶子构件或树枝构件
    public void add(Component component){
        this.componentArrayList.add(component);
    }
    //删除一个叶子构件或树枝构件
    public void remove(Component component){
        this.componentArrayList.remove(component);
    }
    //获得分支下所有叶子构件或树枝构件
    public ArrayList<Component> getChildren(){
        return this.componentArrayList;
    }
}
package com.csdn2;

import java.util.ArrayList;
/**
 * 树叶节点
 * @author Administrator
 *
 */
public class Leaf extends Component{
    //空实现,抛不支持请求异常
    @Deprecated
    @Override
    public void add(Component component) throws UnsupportedOperationException{
        throw new UnsupportedOperationException();
    }
    @Deprecated
    @Override
    public void remove(Component component) throws UnsupportedOperationException{
        throw new UnsupportedOperationException();
    }
    @Deprecated
    @Override
    public ArrayList<Component> getChildren() throws UnsupportedOperationException{
        throw new UnsupportedOperationException();
    }
}
package com.csdn2;
/**
 * 模拟场景
 * @author Administrator
 *
 */
public class Client {
    public static void main(String[] args) {
        //创建一个根节点
        Composite root = new Composite();
        root.doSomething();
        //创建一个树枝构件
        Composite branch = new Composite();
        //创建一个叶子节点
        Leaf leaf = new Leaf();
        //建立整体
        root.add(branch);
        branch.add(leaf);
    }
    //通过递归遍历树
    public static void display(Component root){
        for(Component c:root.getChildren()){
            if(c instanceof Leaf){
                c.doSomething();
            }else{
                display(c);
            }
        }
    }
}

注:
a:@Deprecated注解用来告诉调用者调用该方法可能会出错
b:透明模式的组合模式遵循了依赖倒转原则,但不安全
c:组合模式从下级向上级遍历,需要在抽象构件中增加setParent和getParent两个方法分别来设置父节点和获取父节点,在树枝和树叶节点set其父节点

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值