Fork me on GitHub

设计模式-建造者模式

  将一个产品的内部属性和产品的生成过程分割开来,通过一个复杂的Builder构造过程生成具有不同属性的产品对象。参考:https://github.com/iluwatar/java-design-patterns

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/**
* 建造者模式-构建一个游戏英雄角色
*
* @author wangbing
* @version 1.0, 2018/5/19
*/
public final class Hero {

private final String name;

private final Profession profession;

private HairType hairType;

private HairColor hairColor;

private Armor armor;

private Weapon weapon;

public String getName() {
return name;
}

public Profession getProfession() {
return profession;
}

public HairType getHairType() {
return hairType;
}

public HairColor getHairColor() {
return hairColor;
}

public Armor getArmor() {
return armor;
}

public Weapon getWeapon() {
return weapon;
}

private Hero(Builder builder) {
this.profession = builder.profession;
this.name = builder.name;
this.hairType = builder.hairType;
this.hairColor = builder.hairColor;
this.armor = builder.armor;
this.weapon = builder.weapon;
}

public static class Builder {

private final String name;

private final Profession profession;

private HairType hairType;

private HairColor hairColor;

private Armor armor;

private Weapon weapon;

public Builder(Profession profession, String name) {
if (profession == null || name == null) {
throw new IllegalArgumentException("profession and name can not be null");
}
this.profession = profession;
this.name = name;
}

public Builder withHairType(HairType hairType) {
this.hairType = hairType;
return this;
}

public Builder withHairColor(HairColor hairColor) {
this.hairColor = hairColor;
return this;
}

public Builder withArmor(Armor armor) {
this.armor = armor;
return this;
}

public Builder withWeapon(Weapon weapon) {
this.weapon = weapon;
return this;
}

public Hero build() {
return new Hero(this);
}
}
}

1
2
3
4
5
6
7
8
9
10
11
12
/**
* 职业定义
*/
public enum Profession {

WARRIOR, THIEF, MAGE, PRIEST;

@Override
public String toString() {
return name().toLowerCase();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
/**
* 武器类型
*/
public enum Weapon {

DAGGER, SWORD, AXE, WARHAMMER, BOW;

@Override
public String toString() {
return name().toLowerCase();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/**
* 装备盔甲类型
*/
public enum Armor {

CLOTHES("clothes"), LEATHER("leather"), CHAIN_MAIL("chain mail"), PLATE_MAIL("plate mail");

private String title;

Armor(String title) {
this.title = title;
}

@Override
public String toString() {
return title;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/**
* 发型定义
*/
public enum HairType {

BALD("bald"), SHORT("short"), CURLY("curly"),

LONG_STRAIGHT("long straight"), LONG_CURLY("long curly");

private String title;

HairType(String title) {
this.title = title;
}

@Override
public String toString() {
return title;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
/**
* 发色定义
*/
public enum HairColor {

WHITE, BLOND, RED, BROWN, BLACK;

@Override
public String toString() {
return name().toLowerCase();
}
}
-------------本文结束感谢您的阅读-------------