树叶构件(Leaf)角色
树叶不会再有下级。
package com.secondgod.composite;
import java.text.MessageFormat;
/\*\*
\* 树叶
\*
\* @author 二当家的白帽子 https://le-yi.blog.csdn.net/
\*/
public class Leaf implements Component {
/\*\*
\* 叶子对象的名字
\*/
private String name;
public Leaf(String name) {
this.name = name;
}
@Override
public void printStruct(String preStr) {
System.out.println(MessageFormat.format("{0}-{1}", preStr, name));
}
}
树枝构件(Composite)角色
树枝可以继续长出树枝或者树叶,所以要有addChild方法。
package com.secondgod.composite;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
/\*\*
\* 树枝
\*
\* @author 二当家的白帽子 https://le-yi.blog.csdn.net/
\*/
public class Composite implements Component {
/\*\*
\* 用来存储组合对象中包含的子组件对象
\*/
private List<Component> childComponents = new ArrayList<Component>();
/\*\*
\* 组合对象的名字
\*/
private String name;
public Composite(String name){
this.name = name;
}
/\*\*
\* 聚集管理方法,增加一个子构件对象
\* @param child 子构件对象
\*/
public void addChild(Component child){
childComponents.add(child);
}
@Override
public void printStruct(String preStr) {
// 先把自己输出
System.out.println(MessageFormat.format("{0}+{1}", preStr, name));
// 如果还包含有子组件,那么就输出这些子组件对象
if (this.childComponents != null) {
// 添加两个空格,表示向后缩进两个空格
preStr += " ";
// 输出当前对象的子对象
for (Component c : childComponents) {
// 递归输出每个子对象
c.printStruct(preStr);
}
}
}
}
使用
package com.secondgod.composite;
/\*\*
\* 测试
\*
\* @author 二当家的白帽子 https://le-yi.blog.csdn.net/
\*/
public class Client {
public static void main(String[]args){
Composite root = new Composite("生物");
Composite c1 = new Composite("动物");
Composite c2 = new Composite("植物");
Leaf leaf1 = new Leaf("猫猫");
Leaf leaf2 = new Leaf("狗狗");
Leaf leaf3 = new Leaf("大树");
Leaf leaf4 = new Leaf("小草");
root.addChild(c1);
root.addChild(c2);
c1.addChild(leaf1);
c1.addChild(leaf2);
c2.addChild(leaf3);
c2.addChild(leaf4);
root.printStruct("");
}
}
执行结果符合预期。
透明式合成模式
抽象构件(Component)角色
生长树枝和树叶的方法直接声明在抽象构件里。本例使用抽象类,其实也可以使用接口。
package com.secondgod.composite;
/\*\*
\* 抽象构件
\*
\* @author 二当家的白帽子 https://le-yi.blog.csdn.net/
\*/
public abstract class Component {
/\*\*
\* 输出自身的名称
\*/
public abstract void printStruct(String preStr);
/\*\*
\* 聚集管理方法,增加一个子构件对象
\* @param child 子构件对象
\*/
public void addChild(Component child){
/\*\*
\* 缺省实现,抛出异常,因为叶子对象没有此功能
\* 或者子组件没有实现这个功能
\*/
throw new UnsupportedOperationException("对象不支持此功能");
}
}
树叶构件(Leaf)角色
透明式的叶子从实现抽象构件改成继承抽象构件。如果抽象构件是接口,则需要平庸实现管理子构件的方法。
package com.secondgod.composite;
import java.text.MessageFormat;
/\*\*
\* 树叶
\*
\* @author 二当家的白帽子 https://le-yi.blog.csdn.net/
\*/
public class Leaf extends Component {
/\*\*
\* 叶子对象的名字
\*/
private String name;
public Leaf(String name) {
this.name = name;
}
@Override
public void printStruct(String preStr) {
System.out.println(MessageFormat.format("{0}-{1}", preStr, name));
}
}
树枝构件(Composite)角色
透明式的树枝也是从实现抽象构件改为继承抽象构件,这主要跟抽象构件是抽象类还是接口有关。
package com.secondgod.composite;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
/\*\*
\* 树枝
\*
\* @author 二当家的白帽子 https://le-yi.blog.csdn.net/
\*/
public class Composite extends Component {
/\*\*
\* 用来存储组合对象中包含的子组件对象
\*/
private List<Component> childComponents = new ArrayList<Component>();
/\*\*
\* 组合对象的名字
\*/
private String name;
public Composite(String name){
this.name = name;
}
/\*\*
\* 聚集管理方法,增加一个子构件对象
\* @param child 子构件对象
\*/
public void addChild(Component child){
childComponents.add(child);
}
@Override
public void printStruct(String preStr) {
// 先把自己输出
System.out.println(MessageFormat.format("{0}+{1}", preStr, name));
// 如果还包含有子组件,那么就输出这些子组件对象
if (this.childComponents != null) {
// 添加两个空格,表示向后缩进两个空格
preStr += " ";
// 输出当前对象的子对象
for (Component c : childComponents) {
// 递归输出每个子对象
c.printStruct(preStr);
}
}
}
}
![img](https://img-blog.csdnimg.cn/img_convert/8f62f951b1fb4589651ed1785f2fc827.png)
![img](https://img-blog.csdnimg.cn/img_convert/e5aac7ca70ba9699aec48091d402d6c2.png)
![img](https://img-blog.csdnimg.cn/img_convert/e1adec2e08c509aabf5f143d096e3b57.png)
**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!**
**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**
**[需要这份系统化资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618545628)**
5KvflKzE-1714410621762)]
[外链图片转存中...(img-OsA2ai1C-1714410621762)]
[外链图片转存中...(img-iPxHJreZ-1714410621763)]
**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!**
**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**
**[需要这份系统化资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618545628)**