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();
}
}
原文:https://www.cnblogs.com/zhuxudong/p/10164351.html