坦克大战(一期)-设计模式-builder模式

DesignPatterns
bingor 2 years ago
parent 79b5f7ecf5
commit 2a3c1c5a3c

@ -0,0 +1,44 @@
package com.msb.builder;/**
* @Author bingor
* @Date 2022/10/18 16:34
* @Description: com.msb.builder
* @Version: 1.0
*/
/**
*@ClassName ComplexTerrainBuilder
*@Description TODO
*@Author bingor
*@Date 2022/10/18 16:34
*@Version 3.0
*/
public class ComplexTerrainBuilder implements TerrainBuilder {
private Terrain terrain = new Terrain();
@Override
public TerrainBuilder buildWall() {
Wall wall = new Wall(10, 10, 50, 50);
terrain.setWall(wall);
return this;
}
@Override
public TerrainBuilder buildFort() {
Fort fort = new Fort(10, 10, 50, 50);
terrain.setFort(fort);
return this;
}
@Override
public TerrainBuilder buildMine() {
Mine mine = new Mine(10, 10, 50, 50);
terrain.setMine(mine);
return this;
}
@Override
public Terrain build() {
return terrain;
}
}

@ -0,0 +1,26 @@
package com.msb.builder;/**
* @Author bingor
* @Date 2022/10/18 16:42
* @Description: com.msb.builder
* @Version: 1.0
*/
/**
*@ClassName Main
*@Description TODO
*@Author bingor
*@Date 2022/10/18 16:42
*@Version 3.0
*/
public class Main {
public static void main(String[] args) {
TerrainBuilder terrainBuilder = new ComplexTerrainBuilder();
Terrain terrain = terrainBuilder.buildWall().buildFort().buildMine().build();
Person person = new Person.PersonBuilder().buildBasicInfo(1, "bingor")
.buildAge(33)
.buildScore(100)
.buildLocation("机场路", "8号")
.build();
}
}

@ -0,0 +1,59 @@
package com.msb.builder;/**
* @Author bingor
* @Date 2022/10/18 16:51
* @Description: com.msb.builder
* @Version: 1.0
*/
/**
*@ClassName Person
*@Description TODO
*@Author bingor
*@Date 2022/10/18 16:51
*@Version 3.0
*/
public class Person {
private int id;
private int age;
private String name;
private int score;
private Location location;
public static class PersonBuilder {
Person person = new Person();
public PersonBuilder buildBasicInfo(int id, String name) {
person.id = id;
person.name = name;
return this;
}
public PersonBuilder buildAge(int age) {
person.age = age;
return this;
}
public PersonBuilder buildScore(int score) {
person.score = score;
return this;
}
public PersonBuilder buildLocation(String street, String roomNo) {
person.location = new Location(street, roomNo);
return this;
}
public Person build() {
return person;
}
}
}
class Location {
private String street;
private String roomNo;
public Location(String street, String roomNo) {
this.street = street;
this.roomNo = roomNo;
}
}

@ -0,0 +1,61 @@
package com.msb.builder;/**
* @Author bingor
* @Date 2022/10/18 15:46
* @Description: com.msb.builder
* @Version: 1.0
*/
/**
*@ClassName Terrain
*@Description TODO
*@Author bingor
*@Date 2022/10/18 15:46
*@Version 3.0
*/
public class Terrain {
private Wall wall;
private Fort fort;
private Mine mine;
public void setWall(Wall wall) {
this.wall = wall;
}
public void setFort(Fort fort) {
this.fort = fort;
}
public void setMine(Mine mine) {
this.mine = mine;
}
}
class Wall {
private int x, y, width, height;
public Wall(int x, int y, int width, int height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
}
class Fort {
private int x, y, width, height;
public Fort(int x, int y, int width, int height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
}
class Mine {
private int x, y, width, height;
public Mine(int x, int y, int width, int height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
}

@ -0,0 +1,14 @@
package com.msb.builder;
/**
* @Author bingor
* @Date 2022/10/18 16:33
* @Description: com.msb.builder
* @Version: 1.0
*/
public interface TerrainBuilder {
public TerrainBuilder buildWall();
public TerrainBuilder buildFort();
public TerrainBuilder buildMine();
public Terrain build();
}
Loading…
Cancel
Save