1. 程式人生 > >組合模式【結構模式】

組合模式【結構模式】

組合模式

Composite objects into tree structures to represent part-whole hierarchies.
Composite lets clients treat individual objects and compositions of objects uniformly.
將物件組合成樹形結構以表示"整體-部分"的層次結構,
組合模式允許客戶端統一處理單個物件或組合物件。
@Slf4j
public class Composite {
    /**
     * 組合模式:
     * Composite objects into tree structures to represent part-whole hierarchies.
     * Composite lets clients treat individual objects and compositions of objects uniformly.
     * 將物件組合成樹形結構以表示"整體-部分"的層次結構,組合模式允許客戶端統一處理單個物件或組合物件。
     */
    @Test
    public void all() {
        final CTO cto = new CTO("kristy", 100);
        cto.setArea("杭州");

        cto.addLeaf(buildSecretary("s1", 500, "吃飯"));
        cto.addLeaf(buildSecretary("s2", 500, "睡覺"));
        cto.addLeaf(buildSecretary("s3", 500, "打豆豆"));

        final Manager m1 = new Manager("m1", 500);
        m1.addLeaf(new Worker("w1", 30));
        m1.addLeaf(new Worker("w2", 40));
        m1.addLeaf(new Worker("w3", 50));

        cto.addLeaf(m1);

        final Manager m2 = new Manager("m2", 400);
        m2.addLeaf(new Worker("w4", 60));
        m2.addLeaf(new Worker("w5", 70));
        m2.addLeaf(buildSecretary("s4", 300, "摸魚"));

        cto.addLeaf(m2);
        show(cto);
    }

    private static Secretary buildSecretary(String name, double salary, String extend) {
        final Secretary s3 = new Secretary(name, salary);
        s3.setExtend(extend);
        return s3;
    }

    public static void show(IBranch root) {
        log.info(root.info());
        root.subLeaies().forEach(sub -> {
            if (sub instanceof IBranch) {
                show((IBranch) sub);
            } else {
                log.info(sub.info());
            }
        });
    }

}

/**
 * 1)參加組合的所有物件的共有屬性或方法,可以是抽象類
 */
interface ICorp {
    String info();
}

/**
 * 2)葉子構件介面
 */
interface ILeaf extends ICorp {
}

/**
 * 3)樹枝構件介面
 */
interface IBranch extends ICorp {
    void addLeaf(ICorp leaf);

    void removeLeaf(ICorp leaf);

    List<ICorp> subLeaies();
}

/**
 * 4)共有元件介面的核心實現
 */
@AllArgsConstructor
abstract class Emp implements ICorp {
    protected final String name;
    protected final double salary;

    @Override
    public String info() {
        return new StringJoiner("-").add(name).add(String.valueOf(salary)).toString();
    }
}

/**
 * 5)葉子實現
 */
class Worker extends Emp implements ILeaf {
    public Worker(String name, double salary) {
        super(name, salary);
    }
}

/**
 * 6)樹枝實現
 */
class Manager extends Emp implements IBranch {
    public Manager(String name, double salary) {
        super(name, salary);
    }

    @Default
    private final List<ICorp> leavies = Lists.newArrayList();

    @Override
    public void addLeaf(ICorp leaf) {
        leavies.add(leaf);
    }

    @Override
    public List<ICorp> subLeaies() {
        return leavies;
    }

    @Override
    public void removeLeaf(ICorp leaf) {
        leavies.remove(leaf);
    }
}

/**
 * 7)特化樹枝實現
 */
@Data
class CTO extends Manager {
    public CTO(String name, double salary) {
        super(name, salary);
    }

    private String area;

    @Override
    public String info() {
        return new StringJoiner("-").add(super.info()).add(getArea()).toString();
    }
}

/**
 * 8)特化葉子實現
 */
@Data
class Secretary extends Emp implements ILeaf {
    private String extend;

    public Secretary(String name, double salary) {
        super(name, salary);
    }

    @Override
    public String info() {
        return new StringJoiner("-").add(super.info()).add(extend).toString();
    }
}