0%

桥接模式

  桥接模式(Bridge)


参考:

🔗 《设计模式:可复用面向对象软件的基础》
🔗 《大话设计模式》

适配器(对象)模式

  适配器-对象模式(Adapter-Object)


参考:

🔗 《设计模式:可复用面向对象软件的基础》
🔗 《大话设计模式》

单例模式

  单例模式(Singleton)


参考:

🔗 《设计模式:可复用面向对象软件的基础》
🔗 《大话设计模式》

原型模式

  原型模式(Prototype),用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象。

原型模式结构图

  原型模式其实就是从一个对象再创建另外一个可定制的对象,而且不需要知道任何创建的细节。

1.3.x 案例

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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124

public class Test {
public static void main(String[] args) {
//构建简历,可以设置基本信息和工作经历等
Resume a = new Resume("大鸟");
a.setPersonalInfo("男","29");
a.setWorkExperience("1998-2000", "XX公司");

Resume b = new Resume("大鸟");
b.setPersonalInfo("男","29");
b.setWorkExperience("1998-2000", "XX公司");

Resume c = new Resume("大鸟");
c.setPersonalInfo("男","29");
c.setWorkExperience("1998-2000", "XX公司");

a.display();
b.display();
c.display();


//原型模式
System.out.println("---------------------原型模式");
ResumeCloneable aNew = new ResumeCloneable("大鸟");
aNew.setPersonalInfo("男","29");
aNew.setWorkExperience("1998-2000", "XX公司");

ResumeCloneable bNew = (ResumeCloneable) aNew.clone();
bNew.setWorkExperience("1998-2006", "XX公司");

ResumeCloneable cNew = (ResumeCloneable) aNew.clone();
cNew.setPersonalInfo("男","24");

aNew.display();
bNew.display();
cNew.display();

//这样的实现仅仅是浅克隆,对于对象属性域需要更改结构为深克隆
}
}

public class Resume {
private String name;
private String sex;
private String age;
private String timeArea;
private String company;

public Resume(String name) {
this.name = name;
}

//设置个人信息
public void setPersonalInfo(String sex,String age){
this.sex = sex;
this.age = age;
}

//设置工作经历
public void setWorkExperience(String timeArea, String company){
this.timeArea = timeArea;
this.company = company;
}

//显式
public void display(){
System.out.println("{" + name + "} {" + sex + "} {" + age + "}");
System.out.println("工作经历:{" + timeArea + "} {" + company + "}");
}
}

public abstract class ICloneable {
public abstract Object clone();
}

public class ResumeCloneable extends ICloneable {
private String name;
private String sex;
private String age;
private String timeArea;
private String company;

public ResumeCloneable(String name) {
this.name = name;
}

public ResumeCloneable(String name, String sex, String age, String timeArea, String company) {
this.name = name;
this.sex = sex;
this.age = age;
this.timeArea = timeArea;
this.company = company;
}

//设置个人信息
public void setPersonalInfo(String sex,String age){
this.sex = sex;
this.age = age;
}

//设置工作经历
public void setWorkExperience(String timeArea, String company){
this.timeArea = timeArea;
this.company = company;
}

//显式
public void display(){
System.out.println("{" + name + "} {" + sex + "} {" + age + "}");
System.out.println("工作经历:{" + timeArea + "} {" + company + "}");
}

@Override
public Object clone() {
return (Object) this.memberwiseClone();
}
private ResumeCloneable memberwiseClone(){
//创建一个新的对象,然后将当前对象的非静态字段复制到该新对象。
//如果字段是值类型的,则对该字段执行逐位复制。
//如果字段是引用类型,则复制引用但不复制引用的对象;
//因此,原始对象及其副本引用同一对象
return new ResumeCloneable(this.name,this.sex,this.age,this.timeArea,this.company);
}
}

1.3.x 代码结构

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
public abstract class Prototype {
private String id;

public Prototype(String id) {
this.id = id;
}

public String getId() {
return id;
}

public abstract Prototype clone();
}

public class ConcretePrototype1 extends Prototype {
public ConcretePrototype1(String id) {
super(id);
}

@Override
public Prototype clone() {
//创建当前对象的浅表副本
return (Prototype) this.memberwiseClone();
}

private ConcretePrototype1 memberwiseClone(){
//创建一个新的对象,然后将当前对象的非静态字段复制到该新对象。
//如果字段是值类型的,则对该字段执行逐位复制。
//如果字段是引用类型,则复制引用但不复制引用的对象;
//因此,原始对象及其副本引用同一对象
return new ConcretePrototype1(this.getId());
}
}

public class Main {
public static void main(String[] args) {
ConcretePrototype1 p1 = new ConcretePrototype1("I");
ConcretePrototype1 c1 = (ConcretePrototype1) p1.clone();
System.out.println("Cloned: {" + c1.getId() + "}");
}
}

参考:

🔗 《设计模式:可复用面向对象软件的基础》
🔗 《大话设计模式》

建造者模式

  建造者模式(Builder),也叫生成器模式,将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示

建造者模式结构图

  生成器可以将一个产品的内部表象与产品的生成过程分割开来,从而可以使一个建造过程生成具有不同的内部表象的产品对象。如果我们用了建造者模式,那么用户就只需指定需要建造的类型就可以得到它们,而具体建造的过程和细节就不需知道了。

1.2.x 适用场景

  建造者模式是在当创建复杂对象的算法应该独立于该对象的组成部分以及它们的装配方式时适用的模式。

1.2.x 代码结构

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

//产品类
public class Product {
List<String> parts = new ArrayList<>();

public void add(String part){
//增加产品部件
parts.add(part);
}

public void show(){
System.out.println("\n 产品 创建 ----");
for(String part : parts){
System.out.println(part);
}
}
}

//抽象建造者类,确定产品由两个部件PartA和PartB组成,并声明一个得到产品建造后结果的方法
public abstract class Builder {
public abstract void buildPartA();
public abstract void buildPartB();
public abstract Product getResult();
}

//具体建造者类
public class ConcreteBuilder1 extends Builder {
private Product product = new Product();

@Override
public void buildPartA() {
product.add("部件A");
}

@Override
public void buildPartB() {
product.add("部件B");
}

@Override
public Product getResult() {
return product;
}
}

public class ConcreteBuilder2 extends Builder {
private Product product = new Product();

@Override
public void buildPartA() {
product.add("部件X");
}

@Override
public void buildPartB() {
product.add("部件Y");
}

@Override
public Product getResult() {
return product;
}
}

//指挥者类
public class Director {

public void construct(Builder builder){
builder.buildPartA();
builder.buildPartB();
}
}

//客户端代码
public class Main {
public static void main(String[] args) {
Director director = new Director();
Builder b1 = new ConcreteBuilder1();
Builder b2 = new ConcreteBuilder2();

director.construct(b1);
Product p1 = b1.getResult();
p1.show();

director.construct(b2);
Product p2 = b2.getResult();
p2.show();

}
}


参考:

🔗 《设计模式:可复用面向对象软件的基础》
🔗 《大话设计模式》