From 6ed493c935834da42db4e3feaab4ec77a1f6dde8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A7=A6=E8=8B=B1=E6=9D=B0?= <327782001@qq.com> Date: Sun, 30 Jul 2023 20:01:34 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E7=BB=84=E5=90=88=E6=A8=A1=E5=BC=8F?= =?UTF-8?q?=E5=92=8C=E8=AE=BF=E9=97=AE=E8=80=85=E6=A8=A1=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/study/design/{ => items}/MockDb.java | 10 +++++++- .../items/node/AbstractProductItem.java | 19 +++++++++++++++ .../study/design/items/node/ProductItem.java | 24 +++++++++++++++++-- .../design/items/visitor/AddItemVisitor.java | 22 +++++++++++------ .../design/items/visitor/DelItemVisitor.java | 22 +++++++++++------ .../design/items/visitor/ItemVisitor.java | 14 +++++++++++ .../com/study/design/service/ItemService.java | 11 +++++++-- 7 files changed, 103 insertions(+), 19 deletions(-) rename src/main/java/com/study/design/{ => items}/MockDb.java (88%) diff --git a/src/main/java/com/study/design/MockDb.java b/src/main/java/com/study/design/items/MockDb.java similarity index 88% rename from src/main/java/com/study/design/MockDb.java rename to src/main/java/com/study/design/items/MockDb.java index 3c87461..237d676 100644 --- a/src/main/java/com/study/design/MockDb.java +++ b/src/main/java/com/study/design/items/MockDb.java @@ -1,10 +1,18 @@ -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(); diff --git a/src/main/java/com/study/design/items/node/AbstractProductItem.java b/src/main/java/com/study/design/items/node/AbstractProductItem.java index 1c53b13..9c8a82d 100644 --- a/src/main/java/com/study/design/items/node/AbstractProductItem.java +++ b/src/main/java/com/study/design/items/node/AbstractProductItem.java @@ -1,6 +1,25 @@ 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); } diff --git a/src/main/java/com/study/design/items/node/ProductItem.java b/src/main/java/com/study/design/items/node/ProductItem.java index d03d17f..d5a3e46 100644 --- a/src/main/java/com/study/design/items/node/ProductItem.java +++ b/src/main/java/com/study/design/items/node/ProductItem.java @@ -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 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 diff --git a/src/main/java/com/study/design/items/visitor/AddItemVisitor.java b/src/main/java/com/study/design/items/visitor/AddItemVisitor.java index 6583482..a2eea51 100644 --- a/src/main/java/com/study/design/items/visitor/AddItemVisitor.java +++ b/src/main/java/com/study/design/items/visitor/AddItemVisitor.java @@ -1,19 +1,27 @@ 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{ - // 入参是 id 2, pid为 1 +public class AddItemVisitor implements ItemVisitor { + @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{ } 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 { diff --git a/src/main/java/com/study/design/items/visitor/DelItemVisitor.java b/src/main/java/com/study/design/items/visitor/DelItemVisitor.java index 08dbb6a..2d41ae2 100644 --- a/src/main/java/com/study/design/items/visitor/DelItemVisitor.java +++ b/src/main/java/com/study/design/items/visitor/DelItemVisitor.java @@ -1,19 +1,27 @@ 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{ - // 入参是 id 2, pid为 1 +public class DelItemVisitor implements ItemVisitor { + @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{ } 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 { diff --git a/src/main/java/com/study/design/items/visitor/ItemVisitor.java b/src/main/java/com/study/design/items/visitor/ItemVisitor.java index ef12cbd..c6a6dc5 100644 --- a/src/main/java/com/study/design/items/visitor/ItemVisitor.java +++ b/src/main/java/com/study/design/items/visitor/ItemVisitor.java @@ -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 { + /** + * 访问行为 + * + * @param productItem + * @return + */ T visitor(ProductItem productItem); } diff --git a/src/main/java/com/study/design/service/ItemService.java b/src/main/java/com/study/design/service/ItemService.java index cf36d02..90b0285 100644 --- a/src/main/java/com/study/design/service/ItemService.java +++ b/src/main/java/com/study/design/service/ItemService.java @@ -1,17 +1,24 @@ 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; -- GitLab