坦克大战(一期)-设计模式-Visitor访问者模式1.0

DesignPatterns
bingor 2 years ago
parent e13be0a36b
commit 7a789cda65

@ -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;
}
}

@ -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;
}
}

@ -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());
}
}

@ -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();
}

@ -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;
}
}

@ -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;
}
}

@ -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;
}
}

@ -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();
}
Loading…
Cancel
Save