设计模式~~~组合模式

学习难度:※※※
使用频率:※※※※

在现实生活中,存在很多“部分-整体”的关系,例如,大学中的部门与学院、总公司中的部门与分公司、学习用品中的书与书包、生活用品中的衣月艮与衣柜以及厨房中的锅碗瓢盆等。

在软件开发中也是这样,例如,文件系统中的文件与文件夹、窗体程序中的简单控件与容器控件等。对这些简单对象与复合对象的处理,如果用组合模式来实现会很方便。

一、 定义

有时又叫作部分-整体模式,它是一种将对象组合成树状的层次结构的模式,用来表示“部分-整体”的关系,使用户对单个对象和组合对象具有一致的访问性。

二、 优点和缺点

优点:

  1. 组合模式使得客户端代码可以一致地处理单个对象和组合对象,无须关心自己处理的是单个对象,还是组合对象,这简化了客户端代码;
  2. 更容易在组合体内加入新的对象,客户端不会因为加入了新的对象而更改源代码,满足“开闭原则”;

缺点:

  • 设计较复杂,客户端需要花更多时间理清类之间的层次关系;
  • 不容易限制容器中的构件;
  • 不容易用继承的方法来增加构件的新功能;

三、 应用场景

  1. 在需要表示一个对象整体与部分的层次结构的场合。
  2. 要求对用户隐藏组合对象与单个对象的不同,用户可以用统一的接口使用组合结构中的所有对象的场合。

四、 组合模式的结构和使用

组合模式的结构不是很复杂

4.1 组合模式的结构

组合模式包含以下主要角色。

  • 抽象构件(Component)角色:它的主要作用是为树叶构件和树枝构件声明公共接口,并实现它们的默认行为。在透明式的组合模式中抽象构件还声明访问和管理子类的接口;在安全式的组合模式中不声明访问和管理子类的接口,管理工作由树枝构件完成。
  • 树叶构件(Leaf)角色:是组合中的叶节点对象,它没有子节点,用于实现抽象构件角色中 声明的公共接口。
  • 树枝构件(Composite)角色:是组合中的分支节点对象,它有子节点。它实现了抽象构件角色中声明的接口,它的主要作用是存储和管理子部件,通常包含 Add()、Remove()、GetChild() 等方法。

4.2 代码伺候 (组合模式的使用)

组合模式分为透明式的组合模式和安全式的组合模式。

(1) 透明方式:在该方式中,由于抽象构件声明了所有子类中的全部方法,所以客户端无须区别树叶对象和树枝对象,对客户端来说是透明的。但其缺点是:树叶构件本来没有 Add()、Remove() 及 GetChild() 方法,却要实现它们(空实现或抛异常),这样会带来一些安全性问题

透明式

(2) 安全方式:在该方式中,将管理子构件的方法移到树枝构件中,抽象构件和树叶构件没有对子对象的管理方法,这样就避免了上一种方式的安全性问题,但由于叶子和分支有不同的接口,客户端在调用时要知道树叶对象和树枝对象的存在,所以失去了透明性。
安全式

下面给出透明式的组合模式的实现代码,与安全式的组合模式的实现代码类似,只要对其做简单修改就可以了。

package composite;
import java.util.ArrayList;
public class CompositePattern
{
    public static void main(String[] args)
    {
        Component c0=new Composite(); 
        Component c1=new Composite(); 
        Component leaf1=new Leaf("1"); 
        Component leaf2=new Leaf("2"); 
        Component leaf3=new Leaf("3");          
        c0.add(leaf1); 
        c0.add(c1);
        c1.add(leaf2); 
        c1.add(leaf3);          
        c0.operation(); 
    }
}
//抽象构件
interface Component
{
    public void add(Component c);
    public void remove(Component c);
    public Component getChild(int i);
    public void operation();
}
//树叶构件
class Leaf implements Component
{
    private String name;
    public Leaf(String name)
    {
        this.name=name;
    }
    public void add(Component c){ }           
    public void remove(Component c){ }   
    public Component getChild(int i)
    {
        return null;
    }   
    public void operation()
    {
        System.out.println("树叶"+name+":被访问!"); 
    }
}
//树枝构件
class Composite implements Component
{
    private ArrayList<Component> children=new ArrayList<Component>();   
    public void add(Component c)
    {
        children.add(c);
    }   
    public void remove(Component c)
    {
        children.remove(c);
    }   
    public Component getChild(int i)
    {
        return children.get(i);
    }   
    public void operation()
    {
        for(Object obj:children)
        {
            ((Component)obj).operation();
        }
    }    
}

五、 应用实例

案例:实现当用户在商店购物后,显示其所选商品信息,并计算所选商品总价的功能。

说明:假如高先生到柳林“燎原大厦”购物,用 1 个红色小袋子装了 2 包芝麻饼特产(单价 10 元)、1 张柳林地图(单价 9.9 元);用 1 个白色小袋子装了 2 包红枣(单价 30 元)和 3 包碗团(单价30 元);用 1 个中袋子装了前面的红色小袋子和 1 块煤炭(单价 380 元);用 1 个大袋子装了前面的中袋子、白色小袋子和 1 双李宁牌运动鞋(单价 198 元)。

最后“大袋子”中的内容有:{1 双李宁牌运动鞋(单价 198 元)、白色小袋子{2 包红枣(单价 30 元)、3 包碗团(单价 30 元)}、中袋子{1 块煤炭(单价 380 元)、红色小袋子{2 包芝麻饼特产(单价10 元)、1 张柳林地图(单价 9.9 元)}}},现在要求编程显示高先生放在大袋子中的所有商品信息并计算要支付的总价。
在这里插入图片描述

package struct.Composite;

/**
 * @Author Gordon
 * @Date 2020/10/13 11:29
 * @Motto: Only practice 72 changes, can laugh at 81 difficult
 */

//抽象构件   物品
public interface Articles {
    public void show();

    public double calculate();// 计算
}


//树叶构件      商品
public class Goods implements Articles{
    private String name;  //商品名字
    private int num;//商品数量
    private double price;//商品价格

    public Goods(String name, int num, double price) {
        this.name = name;
        this.num = num;
        this.price = price;
    }

    @Override
    public void show() {
        System.out.println("商品名称:"+name+"数量:"+num+"单价:"+price+"元!!!!");
    }

    @Override
    public double calculate() {
        return num*price;
    }
}



//树枝构件   袋子
public class Bags implements Articles{
    private String name;//名字
    ArrayList<Articles> articles = new ArrayList<>();

    public Bags(String name) {
        this.name = name;
    }

    public void add(Articles a){
        articles.add(a);
    }
    public void remove(Articles a){
        articles.remove(a);
    }
    public Articles getchild(int i){
        return articles.get(i);
    }

    @Override
    public void show() {
        for (Articles article : articles) {
            article.show();
        }
    }

    @Override
    public double calculate() {
        double a = 0;
        for (Articles article : articles) {
            a += article.calculate();
        }
        return a;
    }
}



public class Test {

    public static void main(String[] args)
    {
        double s=0;
        Bags BigBag,mediumBag,smallRedBag,smallWhiteBag;
        Goods sp;
        BigBag=new Bags("大袋子");
        mediumBag=new Bags("中袋子");
        smallRedBag=new Bags("红色小袋子");
        smallWhiteBag=new Bags("白色小袋子");
        sp=new Goods("芝麻饼特产",2,10);
        smallRedBag.add(sp);
        sp=new Goods("柳林地图",1,9.9);
        smallRedBag.add(sp);
        sp=new Goods("柳林红枣",2,30);
        smallWhiteBag.add(sp);
        sp=new Goods("柳林碗团",3,30);
        smallWhiteBag.add(sp);
        sp=new Goods("煤炭",1,380);
        mediumBag.add(sp);
        mediumBag.add(smallRedBag);
        sp=new Goods("李宁牌运动鞋",1,888);
        BigBag.add(sp);
        BigBag.add(smallWhiteBag);
        BigBag.add(mediumBag);
        System.out.println("您选购的商品有:");
        BigBag.show();
        s = BigBag.calculate();
        System.out.println("要支付的总价是:"+s+"元");
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Peak_Gao

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值