From 7a789cda65195cef3614c39a4b50646d8a37e677 Mon Sep 17 00:00:00 2001 From: bingor Date: Mon, 17 Oct 2022 20:10:02 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9D=A6=E5=85=8B=E5=A4=A7=E6=88=98=EF=BC=88?= =?UTF-8?q?=E4=B8=80=E6=9C=9F=EF=BC=89-=E8=AE=BE=E8=AE=A1=E6=A8=A1?= =?UTF-8?q?=E5=BC=8F-Visitor=E8=AE=BF=E9=97=AE=E8=80=85=E6=A8=A1=E5=BC=8F1?= =?UTF-8?q?.0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/msb/visitor/Board.java | 25 ++++++++++ .../java/com/msb/visitor/CompanyVisitor.java | 38 +++++++++++++++ src/main/java/com/msb/visitor/Computer.java | 46 +++++++++++++++++++ .../java/com/msb/visitor/ComputerPart.java | 18 ++++++++ src/main/java/com/msb/visitor/Cpu.java | 26 +++++++++++ src/main/java/com/msb/visitor/Memory.java | 25 ++++++++++ .../java/com/msb/visitor/PersonelVisitor.java | 40 ++++++++++++++++ src/main/java/com/msb/visitor/Visitor.java | 14 ++++++ 8 files changed, 232 insertions(+) create mode 100644 src/main/java/com/msb/visitor/Board.java create mode 100644 src/main/java/com/msb/visitor/CompanyVisitor.java create mode 100644 src/main/java/com/msb/visitor/Computer.java create mode 100644 src/main/java/com/msb/visitor/ComputerPart.java create mode 100644 src/main/java/com/msb/visitor/Cpu.java create mode 100644 src/main/java/com/msb/visitor/Memory.java create mode 100644 src/main/java/com/msb/visitor/PersonelVisitor.java create mode 100644 src/main/java/com/msb/visitor/Visitor.java diff --git a/src/main/java/com/msb/visitor/Board.java b/src/main/java/com/msb/visitor/Board.java new file mode 100644 index 0000000..a3c2fe7 --- /dev/null +++ b/src/main/java/com/msb/visitor/Board.java @@ -0,0 +1,25 @@ +package com.msb.visitor;/** + * @Author bingor + * @Date 2022/10/17 19:42 + * @Description: com.msb.visitor + * @Version: 1.0 + */ + +/** + *@ClassName Board + *@Description TODO + *@Author bingor + *@Date 2022/10/17 19:42 + *@Version 3.0 + */ +public class Board extends ComputerPart { + @Override + public void accept(Visitor visitor) { + visitor.visitBoard(this); + } + + @Override + public double getPrice() { + return 800; + } +} diff --git a/src/main/java/com/msb/visitor/CompanyVisitor.java b/src/main/java/com/msb/visitor/CompanyVisitor.java new file mode 100644 index 0000000..9995f31 --- /dev/null +++ b/src/main/java/com/msb/visitor/CompanyVisitor.java @@ -0,0 +1,38 @@ +package com.msb.visitor;/** + * @Author bingor + * @Date 2022/10/17 19:44 + * @Description: com.msb.visitor + * @Version: 1.0 + */ + +/** + *@ClassName CompanyVisitor + *@Description TODO + *@Author bingor + *@Date 2022/10/17 19:44 + *@Version 3.0 + */ +public class CompanyVisitor implements Visitor { + + double totalPrice = 0.0; + + @Override + public void visitCpu(Cpu cpu) { + this.totalPrice += cpu.getPrice() * 0.7; + } + + @Override + public void visitMemory(Memory memory) { + this.totalPrice += memory.getPrice() * 0.8; + } + + @Override + public void visitBoard(Board board) { + this.totalPrice += board.getPrice() * 0.7; + } + + @Override + public double getTotalPrice() { + return totalPrice; + } +} diff --git a/src/main/java/com/msb/visitor/Computer.java b/src/main/java/com/msb/visitor/Computer.java new file mode 100644 index 0000000..4816b3b --- /dev/null +++ b/src/main/java/com/msb/visitor/Computer.java @@ -0,0 +1,46 @@ +package com.msb.visitor;/** + * @Author bingor + * @Date 2022/10/17 19:48 + * @Description: com.msb.visitor + * @Version: 1.0 + */ + +/** + *@ClassName Computer + *@Description TODO + *@Author bingor + *@Date 2022/10/17 19:48 + *@Version 3.0 + */ +public class Computer { + private Cpu cpu; + private Memory memory; + private Board board; + + public Computer(Cpu cpu, Memory memory, Board board) { + this.cpu = cpu; + this.memory = memory; + this.board = board; + } + + public void accept(Visitor visitor) { + this.cpu.accept(visitor); + this.board.accept(visitor); + this.memory.accept(visitor); + } + + public static void main(String[] args) { + + Computer computer = new Computer(new Cpu(), new Memory(), new Board()); + + Visitor personelVisitor = new PersonelVisitor(); + computer.accept(personelVisitor); + System.out.println(personelVisitor.getTotalPrice()); + + Visitor companyVisitor = new CompanyVisitor(); + computer.accept(companyVisitor); + System.out.println(companyVisitor.getTotalPrice()); + + } + +} diff --git a/src/main/java/com/msb/visitor/ComputerPart.java b/src/main/java/com/msb/visitor/ComputerPart.java new file mode 100644 index 0000000..d74db35 --- /dev/null +++ b/src/main/java/com/msb/visitor/ComputerPart.java @@ -0,0 +1,18 @@ +package com.msb.visitor;/** + * @Author bingor + * @Date 2022/10/17 19:38 + * @Description: com.msb.visitor + * @Version: 1.0 + */ + +/** + *@ClassName ComputerPart + *@Description TODO + *@Author bingor + *@Date 2022/10/17 19:38 + *@Version 3.0 + */ +public abstract class ComputerPart { + public abstract void accept(Visitor visitor); + public abstract double getPrice(); +} diff --git a/src/main/java/com/msb/visitor/Cpu.java b/src/main/java/com/msb/visitor/Cpu.java new file mode 100644 index 0000000..9f490cf --- /dev/null +++ b/src/main/java/com/msb/visitor/Cpu.java @@ -0,0 +1,26 @@ +package com.msb.visitor;/** + * @Author bingor + * @Date 2022/10/17 19:40 + * @Description: com.msb.visitor + * @Version: 1.0 + */ + +/** + *@ClassName Cpu + *@Description TODO + *@Author bingor + *@Date 2022/10/17 19:40 + *@Version 3.0 + */ +public class Cpu extends ComputerPart { + + @Override + public void accept(Visitor visitor) { + visitor.visitCpu(this); + } + + @Override + public double getPrice() { + return 1000; + } +} diff --git a/src/main/java/com/msb/visitor/Memory.java b/src/main/java/com/msb/visitor/Memory.java new file mode 100644 index 0000000..c6e1a41 --- /dev/null +++ b/src/main/java/com/msb/visitor/Memory.java @@ -0,0 +1,25 @@ +package com.msb.visitor;/** + * @Author bingor + * @Date 2022/10/17 19:42 + * @Description: com.msb.visitor + * @Version: 1.0 + */ + +/** + *@ClassName Memory + *@Description TODO + *@Author bingor + *@Date 2022/10/17 19:42 + *@Version 3.0 + */ +public class Memory extends ComputerPart { + @Override + public void accept(Visitor visitor) { + visitor.visitMemory(this); + } + + @Override + public double getPrice() { + return 900; + } +} diff --git a/src/main/java/com/msb/visitor/PersonelVisitor.java b/src/main/java/com/msb/visitor/PersonelVisitor.java new file mode 100644 index 0000000..fe50a5f --- /dev/null +++ b/src/main/java/com/msb/visitor/PersonelVisitor.java @@ -0,0 +1,40 @@ +package com.msb.visitor;/** + * @Author bingor + * @Date 2022/10/17 19:43 + * @Description: com.msb.visitor + * @Version: 1.0 + */ + +import org.omg.IOP.TAG_ORB_TYPE; + +/** + *@ClassName PersonelVisitor + *@Description TODO + *@Author bingor + *@Date 2022/10/17 19:43 + *@Version 3.0 + */ +public class PersonelVisitor implements Visitor { + + double totalPrice = 0.0; + + @Override + public void visitCpu(Cpu cpu) { + this.totalPrice += cpu.getPrice() * 0.8; + } + + @Override + public void visitMemory(Memory memory) { + this.totalPrice += memory.getPrice() * 0.9; + } + + @Override + public void visitBoard(Board board) { + this.totalPrice += board.getPrice() * 0.8; + } + + @Override + public double getTotalPrice() { + return totalPrice; + } +} diff --git a/src/main/java/com/msb/visitor/Visitor.java b/src/main/java/com/msb/visitor/Visitor.java new file mode 100644 index 0000000..52c4a2c --- /dev/null +++ b/src/main/java/com/msb/visitor/Visitor.java @@ -0,0 +1,14 @@ +package com.msb.visitor; + +/** + * @Author bingor + * @Date 2022/10/17 19:39 + * @Description: com.msb.visitor + * @Version: 1.0 + */ +public interface Visitor { + public void visitCpu(Cpu cpu); + public void visitMemory(Memory memory); + public void visitBoard(Board board); + public double getTotalPrice(); +}