组合模式

2020-08-12  本文已影响0人  潜心之力

一、模式简介

定义:将对象组合成树状的层次结构,用来表示“部分-整体”的关系,使用户对单个对象和组合对象具有一致的访问性。
场景:在需要表示一个对象整体与部分的层次结构的场合。对用户隐藏组合对象与单个对象的不同,用户可以用统一的接口使用组合结构中的所有对象的场合。

二、模式实现

public interface FoodComponent {
    String name();
    Double price();
    boolean add(FoodComponent component);
    boolean remove(FoodComponent component);
    void display();
}

public class PlusComposite implements FoodComponent {
    private List<FoodComponent> list = new ArrayList<>();
    private String name;

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

    @Override
    public String name() {
        return name;
    }

    @Override
    public Double price() {
        BigDecimal total = BigDecimal.ZERO;
        for (FoodComponent component : list) {
            total = total.add(new BigDecimal(String.valueOf(component.price())));
        }
        return total.doubleValue();
    }

    @Override
    public void display() {
        System.out.println(name()+"包含以下信息:");
        for (FoodComponent component : list) {
            component.display();
        }
    }

    public boolean add(FoodComponent component) {
        return list.add(component);
    }

    public boolean remove(FoodComponent component) {
        return list.remove(component);
    }
}

public class MealComposite implements FoodComponent {
    private List<FoodComponent> list = new ArrayList<>();
    private String name;

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

    @Override
    public String name() {
        return name;
    }

    @Override
    public Double price() {
        BigDecimal total = BigDecimal.ZERO;
        for (FoodComponent component : list) {
            total = total.add(new BigDecimal(String.valueOf(component.price())));
        }
        return total.doubleValue();
    }

    @Override
    public void display() {
        System.out.println(name()+"包含以下信息:");
        for (FoodComponent component : list) {
            component.display();
        }
    }

    public boolean add(FoodComponent component) {
        return list.add(component);
    }

    public boolean remove(FoodComponent component) {
        return list.remove(component);
    }
}

public class CokeLeaf implements FoodComponent {
    private String name;

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

    @Override
    public String name() {
        return name;
    }

    @Override
    public Double price() {
        return 5.00;
    }

    @Override
    public boolean add(FoodComponent component) {
        return false;
    }

    @Override
    public boolean remove(FoodComponent component) {
        return false;
    }

    @Override
    public void display() {
        System.out.println(name() + ":" + price());
    }
}

public class HamburgLeaf implements FoodComponent {
    private String name;

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

    @Override
    public String name() {
        return name;
    }

    @Override
    public Double price() {
        return 10.00;
    }

    @Override
    public boolean add(FoodComponent component) {
        return false;
    }

    @Override
    public boolean remove(FoodComponent component) {
        return false;
    }

    @Override
    public void display() {
        System.out.println(name() + ":" + price());
    }
}

树枝构件和树叶构件通过实现抽象构件的默认行为来展现各自的特征,树枝构件属于容器构件,可以容纳多个树枝构件和树叶构件,树叶构件属于展示构件,它只包含自身以及展示最终的效果。

FoodComponent plus = new PlusComposite("加强版容量套餐");
FoodComponent meal = new MealComposite("标准版容量套餐");
FoodComponent coke = new CokeLeaf("百事可乐单品");
FoodComponent hamburg = new HamburgLeaf("炸鸡汉堡单品");
plus.add(coke);
plus.add(meal);
meal.add(hamburg);
plus.display();
上一篇下一篇

猜你喜欢

热点阅读