Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Kwan的解忧杂货铺@新空间代码工作室
design
提交
6ed493c9
D
design
项目概览
Kwan的解忧杂货铺@新空间代码工作室
/
design
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
design
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
6ed493c9
编写于
7月 30, 2023
作者:
Kwan的解忧杂货铺@新空间代码工作室
🐭
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix:组合模式和访问者模式
上级
42dca96c
变更
7
隐藏空白更改
内联
并排
Showing
7 changed file
with
103 addition
and
19 deletion
+103
-19
src/main/java/com/study/design/items/MockDb.java
src/main/java/com/study/design/items/MockDb.java
+9
-1
src/main/java/com/study/design/items/node/AbstractProductItem.java
...java/com/study/design/items/node/AbstractProductItem.java
+19
-0
src/main/java/com/study/design/items/node/ProductItem.java
src/main/java/com/study/design/items/node/ProductItem.java
+22
-2
src/main/java/com/study/design/items/visitor/AddItemVisitor.java
...n/java/com/study/design/items/visitor/AddItemVisitor.java
+15
-7
src/main/java/com/study/design/items/visitor/DelItemVisitor.java
...n/java/com/study/design/items/visitor/DelItemVisitor.java
+15
-7
src/main/java/com/study/design/items/visitor/ItemVisitor.java
...main/java/com/study/design/items/visitor/ItemVisitor.java
+14
-0
src/main/java/com/study/design/service/ItemService.java
src/main/java/com/study/design/service/ItemService.java
+9
-2
未找到文件。
src/main/java/com/study/design/MockDb.java
→
src/main/java/com/study/design/
items/
MockDb.java
浏览文件 @
6ed493c9
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
();
...
...
src/main/java/com/study/design/items/node/AbstractProductItem.java
浏览文件 @
6ed493c9
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
);
}
}
src/main/java/com/study/design/items/node/ProductItem.java
浏览文件 @
6ed493c9
...
@@ -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
...
...
src/main/java/com/study/design/items/visitor/AddItemVisitor.java
浏览文件 @
6ed493c9
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
{
...
...
src/main/java/com/study/design/items/visitor/DelItemVisitor.java
浏览文件 @
6ed493c9
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
{
...
...
src/main/java/com/study/design/items/visitor/ItemVisitor.java
浏览文件 @
6ed493c9
...
@@ -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
);
}
}
src/main/java/com/study/design/service/ItemService.java
浏览文件 @
6ed493c9
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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录