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

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