抽象构件
public abstract class Component {
//个体和整体都具有的共享
public void do Something(){
//编写业务逻辑
}
}
public class Composite extends Component {
//构件容器
private Array List<component> component Array List = new Array List<component>()
//增加一个叶子构件或树枝构件
public void add(Component component){
this.component Array List.add(component);
}
//删除一个叶子构件或树枝构件
public void remove(Component component){
this.component Array List.remove(component);
}
//获得分支下的所有叶子构件和树枝构件
public Array List<component> get Children(){
return this.component Array List;
}
}
树叶构件
public class Leaf extends Component {
/*
*
可以覆写父类方法
* public void do Something(){
*
* }
*/
}
public class Client {
public static void main(String[] args) {
//创建一个根节点
Composite root = new Composite();
root.do Something();
//创建一个树枝构件
Composite branch = new Composite();
//创建一个叶子节点
Leaf leaf = new Leaf();
//建立整体
root.add(branch);
branch.add(leaf);
}
//通过递归遍历树
public static void display(Composite root){
for(Component c:root.get Children()){
if(c instanceof Leaf){ //叶子节点
c.do Something();
}else{ //树枝节点
display((Composite)c);
}
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有