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 3c87461d3315968446d4e788ed9fbde3e81b2ae0..237d6767777216dfeab04a2450d603f783a7e92e 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 1c53b1389f291da651b6bcd32fde77c8f2052a53..9c8a82dbf6f7759cf8f739b76842d6fd10d994bc 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 d03d17f41fba881f689e6231c472f5cc1eb37a9d..d5a3e464c7eb6ad65626c6cdc6b6d6773cd1cb1d 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 658348252793b0ea4cdfe39086928fae27dd98bd..a2eea51031afbd5f1cb34d314e2acfe082d23dca 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 08dbb6abf8546b3ac333102dde3278fe10fd1ebf..2d41ae20b472adf907143661ce5d0bcce570660d 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 ef12cbd8ce771c0ae12abfec9832a15fca4fb5f0..c6a6dc5011b423519071624e9afdff556f09c397 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 cf36d020983e40e161f3f7f35d5faab0e5e66271..90b02856c49b9a068c4d63c193c92fe617fc3948 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;