File tree Expand file tree Collapse file tree 9 files changed +239
-0
lines changed
DesignPattern/src/main/java/com/design/visitor Expand file tree Collapse file tree 9 files changed +239
-0
lines changed Original file line number Diff line number Diff line change
1
+ package com .design .visitor ;
2
+
3
+ /**
4
+ * 计算机主板具体节点
5
+ *
6
+ * @author wliduo[i@dolyw.com]
7
+ * @date 2023/2/3 17:33
8
+ */
9
+ public class BoardComputerNodeImpl extends ComputerNode {
10
+ @ Override
11
+ public void accept (Visitor visitor ) {
12
+ visitor .visit (this );
13
+ }
14
+
15
+ @ Override
16
+ public double getPrice () {
17
+ return 1000 ;
18
+ }
19
+ }
Original file line number Diff line number Diff line change
1
+ package com .design .visitor ;
2
+
3
+ /**
4
+ * 计算机抽象节点
5
+ *
6
+ * @author wliduo[i@dolyw.com]
7
+ * @date 2023/2/3 17:33
8
+ */
9
+ public abstract class ComputerNode {
10
+
11
+ public abstract void accept (Visitor visitor );
12
+
13
+ /**
14
+ * 原始价格
15
+ * @return
16
+ */
17
+ public double getPrice () {
18
+ return 0 ;
19
+ }
20
+
21
+ }
Original file line number Diff line number Diff line change
1
+ package com .design .visitor ;
2
+
3
+ /**
4
+ * 计算机结构对象,也可以同时充当具体节点的作用
5
+ *
6
+ * @author wliduo[i@dolyw.com]
7
+ * @date 2023/2/3 17:33
8
+ */
9
+ public class ComputerStructure extends ComputerNode {
10
+
11
+ /*ComputerNode computerNode = new CpuComputerNodeImpl();
12
+ ComputerNode memoryComputerNode = new MemoryComputerNodeImpl();
13
+ ComputerNode boardComputerNode = new BoardComputerNodeImpl();*/
14
+
15
+ protected ComputerNode [] computerNodeArray ;
16
+
17
+ public ComputerStructure () {
18
+ computerNodeArray = new ComputerNode [] {new CpuComputerNodeImpl (),
19
+ new MemoryComputerNodeImpl (), new BoardComputerNodeImpl ()};
20
+ }
21
+
22
+ @ Override
23
+ public void accept (Visitor visitor ) {
24
+ /*this.computerNode.accept(visitor);
25
+ this.memoryComputerNode.accept(visitor);
26
+ this.boardComputerNode.accept(visitor);*/
27
+ for (ComputerNode computerNode : computerNodeArray ) {
28
+ computerNode .accept (visitor );
29
+ }
30
+ // 整个结构对象,也同时充当节点的作用就需要调用
31
+ visitor .visit (this );
32
+ }
33
+
34
+ @ Override
35
+ public double getPrice () {
36
+ // 计算机除去CPU内存主板后其他东西的总和
37
+ return 1000 ;
38
+ }
39
+ }
Original file line number Diff line number Diff line change
1
+ package com .design .visitor ;
2
+
3
+ /**
4
+ * 公司访问者,CPU 85折,内存7折,主板8折,其他6折
5
+ *
6
+ * @author wliduo[i@dolyw.com]
7
+ * @date 2023/2/3 17:33
8
+ */
9
+ public class CorpVisitorImpl implements Visitor {
10
+
11
+ private double totalPrice = 0.0 ;
12
+
13
+ @ Override
14
+ public void visit (CpuComputerNodeImpl cpuComputerNode ) {
15
+ totalPrice = totalPrice + cpuComputerNode .getPrice () * 0.85 ;
16
+ }
17
+
18
+ @ Override
19
+ public void visit (MemoryComputerNodeImpl memoryComputerNode ) {
20
+ totalPrice = totalPrice + memoryComputerNode .getPrice () * 0.7 ;
21
+ }
22
+
23
+ @ Override
24
+ public void visit (BoardComputerNodeImpl boardComputerNode ) {
25
+ totalPrice = totalPrice + boardComputerNode .getPrice () * 0.8 ;
26
+ }
27
+
28
+ /**
29
+ * 总结点本身触发
30
+ *
31
+ * @param computerStructure
32
+ */
33
+ public void visit (ComputerStructure computerStructure ) {
34
+ totalPrice = totalPrice * 0.6 ;
35
+ }
36
+
37
+ public double getTotalPrice () {
38
+ return totalPrice ;
39
+ }
40
+ }
Original file line number Diff line number Diff line change
1
+ package com .design .visitor ;
2
+
3
+ /**
4
+ * 计算机CPU具体节点
5
+ *
6
+ * @author wliduo[i@dolyw.com]
7
+ * @date 2023/2/3 17:33
8
+ */
9
+ public class CpuComputerNodeImpl extends ComputerNode {
10
+ @ Override
11
+ public void accept (Visitor visitor ) {
12
+ visitor .visit (this );
13
+ }
14
+
15
+ @ Override
16
+ public double getPrice () {
17
+ return 1500 ;
18
+ }
19
+ }
Original file line number Diff line number Diff line change
1
+ package com .design .visitor ;
2
+
3
+ /**
4
+ * 访问者模式,一个购买组装电脑各种零件,对个人及公司访问者不同优惠的例子
5
+ *
6
+ * @author wliduo[i@dolyw.com]
7
+ * @date 2023/2/3 17:33
8
+ */
9
+ public class Main {
10
+
11
+ public static void main (String [] args ) {
12
+ // 整个结构对象,也同时充当节点的作用
13
+ ComputerStructure computerStructure = new ComputerStructure ();
14
+ // 个人购买
15
+ PersonelVisitorImpl personelVisitor = new PersonelVisitorImpl ();
16
+ computerStructure .accept (personelVisitor );
17
+ System .out .println (personelVisitor .getTotalPrice ());
18
+ // 公司购买
19
+ CorpVisitorImpl corpVisitor = new CorpVisitorImpl ();
20
+ computerStructure .accept (corpVisitor );
21
+ System .out .println (corpVisitor .getTotalPrice ());
22
+ }
23
+
24
+ }
Original file line number Diff line number Diff line change
1
+ package com .design .visitor ;
2
+
3
+ /**
4
+ * 计算机内存条具体节点
5
+ *
6
+ * @author wliduo[i@dolyw.com]
7
+ * @date 2023/2/3 17:33
8
+ */
9
+ public class MemoryComputerNodeImpl extends ComputerNode {
10
+ @ Override
11
+ public void accept (Visitor visitor ) {
12
+ visitor .visit (this );
13
+ }
14
+
15
+ @ Override
16
+ public double getPrice () {
17
+ return 500 ;
18
+ }
19
+ }
Original file line number Diff line number Diff line change
1
+ package com .design .visitor ;
2
+
3
+ /**
4
+ * 个人访问者,CPU 95折,内存8折,主板9折,其他8折
5
+ *
6
+ * @author wliduo[i@dolyw.com]
7
+ * @date 2023/2/3 17:33
8
+ */
9
+ public class PersonelVisitorImpl implements Visitor {
10
+
11
+ private double totalPrice = 0.0 ;
12
+
13
+ @ Override
14
+ public void visit (CpuComputerNodeImpl cpuComputerNode ) {
15
+ totalPrice = totalPrice + cpuComputerNode .getPrice () * 0.95 ;
16
+ }
17
+
18
+ @ Override
19
+ public void visit (MemoryComputerNodeImpl memoryComputerNode ) {
20
+ totalPrice = totalPrice + memoryComputerNode .getPrice () * 0.8 ;
21
+ }
22
+
23
+ @ Override
24
+ public void visit (BoardComputerNodeImpl boardComputerNode ) {
25
+ totalPrice = totalPrice + boardComputerNode .getPrice ()* 0.9 ;
26
+ }
27
+
28
+ /**
29
+ * 计算机除去CPU内存主板后其他东西的总和
30
+ *
31
+ * @param computerStructure
32
+ */
33
+ public void visit (ComputerStructure computerStructure ) {
34
+ totalPrice = totalPrice * 0.8 ;
35
+ }
36
+
37
+ public double getTotalPrice () {
38
+ return totalPrice ;
39
+ }
40
+ }
Original file line number Diff line number Diff line change
1
+ package com .design .visitor ;
2
+
3
+ /**
4
+ * 访问者接口
5
+ *
6
+ * @author wliduo[i@dolyw.com]
7
+ * @date 2023/2/3 17:33
8
+ */
9
+ public interface Visitor {
10
+
11
+ void visit (CpuComputerNodeImpl cpuComputerNode );
12
+
13
+ void visit (MemoryComputerNodeImpl memoryComputerNode );
14
+
15
+ void visit (BoardComputerNodeImpl boardComputerNode );
16
+
17
+ void visit (ComputerStructure computerStructure );
18
+ }
You can’t perform that action at this time.
0 commit comments