1.组合模式 || 整体部分模式 --测试
package com.zhaoshuangjian.mode11_组合模式;
import com.zhaoshuangjian.mode11_组合模式.mode11.TreeNode;
import com.zhaoshuangjian.mode11_组合模式.mode11.login.LoginDiagram;
/**
* <p>组合模式 || 整体部分模式 --测试</p>
* 组合模式(Composite Pattern),又叫部分整体模式,
* 是用于把一组相似的对象当作一个单一的对象。组合模式依据树形结构来组合对象,
* 用来表示部分以及整体层次。这种类型的设计模式属于结构型模式,它创建了对象组的树形结构。
* 这种模式创建了一个包含自己对象组的类。该类提供了修改相同对象组的方式。
*
* @Author zhaoshuangjian 2023-03-25 下午21:44
*/
public class CompositeTest {
public static void paintDiagram(){
System.out.println("============开始============");
System.out.println(" / \\");
System.out.println(" 登录 退出");
System.out.println(" / \\ ");
System.out.println(" 首页 失败 ");
System.out.println(" / \\ ");
System.out.println("商品中心 用户中心 ");
System.out.println("============================");
}
public static void main(String[] args) {
// 登录流程图
paintDiagram();
LoginDiagram loginUML = new LoginDiagram("开始");
// 拿到根节点
TreeNode root = loginUML.getRoot();
// 添加一级子节点
TreeNode loginNode = root.addChildren("登录");
TreeNode exitNode = root.addChildren("退出");
// 添加二级子节点
TreeNode homeNode = loginNode.addChildren("首页");
loginNode.addChildren("失败");
// 添加三级子节点
homeNode.addChildren("商品中心");
homeNode.addChildren("用户中心");
// 遍历登录流程树状结构
loginUML.traverse(root);
}
}
2.树
package com.zhaoshuangjian.mode11_组合模式.mode11;
/**
* <p>树</p>
*
* @Author zhaoshuangjian 2023-03-25 下午21:44
*/
public abstract class AbstractTree {
private com.zhaoshuangjian.mode11_组合模式.mode11.TreeNode root = null;
public AbstractTree(){
}
public com.zhaoshuangjian.mode11_组合模式.mode11.TreeNode getRoot() {
return root;
}
public AbstractTree(String name){
root = new com.zhaoshuangjian.mode11_组合模式.mode11.TreeNode(name);
}
/**
* 抽象方法 == 树节点遍历,由具体的子类实现
*/
public abstract void traverse(com.zhaoshuangjian.mode11_组合模式.mode11.TreeNode node);
}
3.树节点
package com.zhaoshuangjian.mode11_组合模式.mode11;
import java.util.ArrayList;
import java.util.List;
/**
* <p>树节点</p>
*
* @Author zhaoshuangjian 2023-03-25 下午21:44
*/
public class TreeNode {
/**
* 节点名称
*/
private String name;
/**
* 节点的父节点
*/
private com.zhaoshuangjian.mode11_组合模式.mode11.TreeNode parent;
/**
* 节点的子节点
*/
private List<TreeNode> childrens = new ArrayList<>();
public TreeNode(){}
public TreeNode(String name){
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public com.zhaoshuangjian.mode11_组合模式.mode11.TreeNode getParent() {
return parent;
}
public void setParent(com.zhaoshuangjian.mode11_组合模式.mode11.TreeNode parent) {
this.parent = parent;
}
public List<TreeNode> getChildrens() {
return childrens;
}
public void setChildrens(List<TreeNode> childrens) {
this.childrens = childrens;
}
public void addChildren(com.zhaoshuangjian.mode11_组合模式.mode11.TreeNode node){
// 别忘设置节点的父
node.setParent(this);
this.childrens.add(node);
}
public com.zhaoshuangjian.mode11_组合模式.mode11.TreeNode addChildren(String node){
com.zhaoshuangjian.mode11_组合模式.mode11.TreeNode cNode = new com.zhaoshuangjian.mode11_组合模式.mode11.TreeNode(node);
// 别忘设置节点的父
cNode.setParent(this);
this.childrens.add(cNode);
return cNode;
}
public void removeChildren(com.zhaoshuangjian.mode11_组合模式.mode11.TreeNode node){
this.childrens.remove(node);
}
/**
* 是否是叶子
*/
public boolean isLeaf(){
return !(this.childrens.size()>0) && (this.parent != null);
}
/**
* 是否有父节点 == 如果没有,就是根节点
* @return
*/
public boolean hasParent(){return !(this.parent == null);}
/**
* 根节点没有父节点
* @return
*/
public boolean isRoot(){return this.parent == null;}
}
4.登录流程图 == 继承抽象树结构
package com.zhaoshuangjian.mode11_组合模式.mode11.login;
import com.zhaoshuangjian.mode11_组合模式.mode11.AbstractTree;
import com.zhaoshuangjian.mode11_组合模式.mode11.TreeNode;
import java.util.List;
/**
* <p>登录流程图 == 继承抽象树结构</p>
*
* @Author zhaoshuangjian 2023-03-25 下午21:44
*/
public class LoginDiagram extends AbstractTree {
public LoginDiagram(String name){
super(name);
}
@Override
public void traverse(TreeNode node) {
if(node.isRoot()){
System.out.println("根:"+node.getName()+",父节点:"+node.getParent());
}else if(node.isLeaf()){
System.out.println("叶子:"+node.getName()+",父节点:"+node.getParent().getName());
}else{
System.out.println("枝:"+node.getName()+",父节点:"+node.getParent().getName());
}
// 递归遍历
List<TreeNode> childrens = node.getChildrens();
for (TreeNode cNode : childrens) {
traverse(cNode) ;
}
}
}