fix:组合模式和访问者模式

上级 42dca96c
package com.study.design;
package com.study.design.items;
import com.study.design.items.node.ProductItem;
import java.util.ArrayList;
import java.util.List;
/**
* 多级目录mock数据
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/7/30 19:46
*/
public class MockDb {
public static ProductItem ProductItem = new ProductItem();
......
package com.study.design.items.node;
/**
* 抽象节点
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/7/30 19:48
*/
public abstract class AbstractProductItem {
/**
* 删除子节点
*
* @param item
*/
public abstract void removeChild(AbstractProductItem item);
/**
* 新增子节点
*
* @param item
*/
public abstract void addChild(AbstractProductItem item);
}
......@@ -4,16 +4,36 @@ import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class ProductItem extends AbstractProductItem{
/**
* 数据节点
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/7/30 19:47
*/
public class ProductItem extends AbstractProductItem {
/**
* id
*/
private int id;
/**
* 父id
*/
private int pid;
/**
* 名称
*/
private String name;
/**
* 子节点集合
*/
private List<ProductItem> child = new ArrayList<>();
@Override
public void removeChild(AbstractProductItem item) {
ProductItem removeItem = (ProductItem) item;
this.child = child.stream().filter(x->x.getId() != removeItem.getId()).collect(Collectors.toList());
this.child = child.stream().filter(x -> x.getId() != removeItem.getId()).collect(Collectors.toList());
}
@Override
......
package com.study.design.items.visitor;
import com.study.design.MockDb;
import com.study.design.items.MockDb;
import com.study.design.items.node.ProductItem;
import org.springframework.stereotype.Component;
/**
* 添加节点访问者
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/7/30 19:46
*/
@Component
public class AddItemVisitor implements ItemVisitor<ProductItem>{
// 入参是 id 2, pid为 1
public class AddItemVisitor implements ItemVisitor<ProductItem> {
@Override
public ProductItem visitor(ProductItem productItem) {
ProductItem currentItem = MockDb.ProductItem; // 从缓存来的 to do
if(productItem.getId() == currentItem.getId()) {
if (productItem.getId() == currentItem.getId()) {
throw new UnsupportedOperationException("根节点是唯一的。");
}
if(productItem.getPid() == currentItem.getId()) {
if (productItem.getPid() == currentItem.getId()) {
currentItem.addChild(productItem);
return currentItem;
}
......@@ -22,8 +30,8 @@ public class AddItemVisitor implements ItemVisitor<ProductItem>{
}
private void addChild(ProductItem productItem, ProductItem currentItem) {
for(ProductItem item : currentItem.getChild()) {
if(item.getId() == productItem.getPid()) {
for (ProductItem item : currentItem.getChild()) {
if (item.getId() == productItem.getPid()) {
item.addChild(productItem);
break;
} else {
......
package com.study.design.items.visitor;
import com.study.design.MockDb;
import com.study.design.items.MockDb;
import com.study.design.items.node.ProductItem;
import org.springframework.stereotype.Component;
/**
* 删除节点访问者
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/7/30 19:46
*/
@Component
public class DelItemVisitor implements ItemVisitor<ProductItem>{
// 入参是 id 2, pid为 1
public class DelItemVisitor implements ItemVisitor<ProductItem> {
@Override
public ProductItem visitor(ProductItem productItem) {
ProductItem currentItem = MockDb.ProductItem; // 从缓存来的 to do
if(productItem.getId() == currentItem.getId()) {
if (productItem.getId() == currentItem.getId()) {
throw new UnsupportedOperationException("根节点不能删。");
}
if(productItem.getPid() == currentItem.getId()) {
if (productItem.getPid() == currentItem.getId()) {
currentItem.removeChild(productItem);
return currentItem;
}
......@@ -22,8 +30,8 @@ public class DelItemVisitor implements ItemVisitor<ProductItem>{
}
private void delChild(ProductItem productItem, ProductItem currentItem) {
for(ProductItem item : currentItem.getChild()) {
if(item.getId() == productItem.getPid()) {
for (ProductItem item : currentItem.getChild()) {
if (item.getId() == productItem.getPid()) {
item.removeChild(productItem);
break;
} else {
......
......@@ -2,6 +2,20 @@ package com.study.design.items.visitor;
import com.study.design.items.node.ProductItem;
/**
* 访问者接口
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/7/30 19:46
*/
public interface ItemVisitor<T> {
/**
* 访问行为
*
* @param productItem
* @return
*/
T visitor(ProductItem productItem);
}
package com.study.design.service;
import com.study.design.MockDb;
import com.study.design.items.MockDb;
import com.study.design.items.node.ProductItem;
import com.study.design.items.visitor.AddItemVisitor;
import com.study.design.items.visitor.DelItemVisitor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* 树结构节点service
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/7/30 19:49
*/
@Service
public class ItemService {
@Autowired
private DelItemVisitor delItemVisitor;
@Autowired
private AddItemVisitor addItemVisitor;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册