From 2a3c1c5a3c7952c0444c6011bc5bdbe5f715812c Mon Sep 17 00:00:00 2001 From: bingor Date: Tue, 18 Oct 2022 18:06:15 +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-builder=E6=A8=A1=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../msb/builder/ComplexTerrainBuilder.java | 44 +++++++++++++ src/main/java/com/msb/builder/Main.java | 26 ++++++++ src/main/java/com/msb/builder/Person.java | 59 ++++++++++++++++++ src/main/java/com/msb/builder/Terrain.java | 61 +++++++++++++++++++ .../java/com/msb/builder/TerrainBuilder.java | 14 +++++ 5 files changed, 204 insertions(+) create mode 100644 src/main/java/com/msb/builder/ComplexTerrainBuilder.java create mode 100644 src/main/java/com/msb/builder/Main.java create mode 100644 src/main/java/com/msb/builder/Person.java create mode 100644 src/main/java/com/msb/builder/Terrain.java create mode 100644 src/main/java/com/msb/builder/TerrainBuilder.java diff --git a/src/main/java/com/msb/builder/ComplexTerrainBuilder.java b/src/main/java/com/msb/builder/ComplexTerrainBuilder.java new file mode 100644 index 0000000..93e5bf8 --- /dev/null +++ b/src/main/java/com/msb/builder/ComplexTerrainBuilder.java @@ -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; + } +} diff --git a/src/main/java/com/msb/builder/Main.java b/src/main/java/com/msb/builder/Main.java new file mode 100644 index 0000000..8bace65 --- /dev/null +++ b/src/main/java/com/msb/builder/Main.java @@ -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(); + } +} diff --git a/src/main/java/com/msb/builder/Person.java b/src/main/java/com/msb/builder/Person.java new file mode 100644 index 0000000..72167f3 --- /dev/null +++ b/src/main/java/com/msb/builder/Person.java @@ -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; + } +} \ No newline at end of file diff --git a/src/main/java/com/msb/builder/Terrain.java b/src/main/java/com/msb/builder/Terrain.java new file mode 100644 index 0000000..d0e33b1 --- /dev/null +++ b/src/main/java/com/msb/builder/Terrain.java @@ -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; + } +} diff --git a/src/main/java/com/msb/builder/TerrainBuilder.java b/src/main/java/com/msb/builder/TerrainBuilder.java new file mode 100644 index 0000000..9fe2fe8 --- /dev/null +++ b/src/main/java/com/msb/builder/TerrainBuilder.java @@ -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(); +}