Skip to content

Commit 78b3e35

Browse files
authored
Merge pull request JamesZBL#10 from JamesZBL/feature
完成状态模式
2 parents a28b3d7 + 13d6f14 commit 78b3e35

File tree

21 files changed

+977
-3
lines changed

21 files changed

+977
-3
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,6 @@
7070

7171
* Strategy (策略模式)
7272

73-
* Template Method (模板方法)
73+
* Template Method (模板方法)
74+
75+
* Visitor(访问者模式)

decorator/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,9 @@ public class HammerSmithOperation implements Operation {
111111
/**
112112
* Decorator
113113
*/
114-
public class Application {
114+
public class me.zbl.ovserver.Application {
115115
116-
private static final Logger LOGGER = LoggerFactory.getLogger(Application.class);
116+
private static final Logger LOGGER = LoggerFactory.getLogger(me.zbl.ovserver.Application.class);
117117
118118
public static void main(String[] args) {
119119
LOGGER.info("仅由木匠制作锤把");

memento/pom.xml

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
MIT License
5+
6+
Copyright (c) 2017 James
7+
8+
Permission is hereby granted, free of charge, to any person obtaining a copy
9+
of this software and associated documentation files (the "Software"), to deal
10+
in the Software without restriction, including without limitation the rights
11+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
copies of the Software, and to permit persons to whom the Software is
13+
furnished to do so, subject to the following conditions:
14+
15+
The above copyright notice and this permission notice shall be included in all
16+
copies or substantial portions of the Software.
17+
18+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24+
SOFTWARE.
25+
26+
-->
27+
<project xmlns="http://maven.apache.org/POM/4.0.0"
28+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
29+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
30+
<parent>
31+
<artifactId>java_design_patterns</artifactId>
32+
<groupId>com.github.JamesZBL</groupId>
33+
<version>1.11.9-SNAPSHOT</version>
34+
</parent>
35+
<modelVersion>4.0.0</modelVersion>
36+
37+
<artifactId>memento</artifactId>
38+
<dependencies>
39+
<dependency>
40+
<groupId>junit</groupId>
41+
<artifactId>junit</artifactId>
42+
<scope>test</scope>
43+
</dependency>
44+
<dependency>
45+
<groupId>org.mockito</groupId>
46+
<artifactId>mockito-core</artifactId>
47+
<scope>test</scope>
48+
</dependency>
49+
</dependencies>
50+
51+
</project>
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**
2+
* MIT License
3+
* <p>
4+
* Copyright (c) 2017 James
5+
* <p>
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
* <p>
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
* <p>
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
package me.zbl.memento;
25+
26+
import org.slf4j.Logger;
27+
import org.slf4j.LoggerFactory;
28+
29+
import java.util.Stack;
30+
31+
/**
32+
* Memento
33+
*/
34+
public class Application {
35+
36+
private static final Logger LOGGER = LoggerFactory.getLogger(Application.class);
37+
38+
public static void main(String[] args) {
39+
Stack<Plant> stack = new Stack<>();
40+
41+
Flower flower = new Flower(FlowerType.SEED, "水仙花", 1, 2);
42+
stack.add(flower.getMemento());
43+
LOGGER.info(flower.toString());
44+
flower.growing();
45+
stack.add(flower.getMemento());
46+
LOGGER.info(flower.toString());
47+
flower.growing();
48+
stack.add(flower.getMemento());
49+
LOGGER.info(flower.toString());
50+
flower.growing();
51+
stack.add(flower.getMemento());
52+
LOGGER.info(flower.toString());
53+
flower.growing();
54+
stack.add(flower.getMemento());
55+
LOGGER.info(flower.toString());
56+
flower.growing();
57+
stack.add(flower.getMemento());
58+
LOGGER.info(flower.toString());
59+
60+
while (stack.size() > 0) {
61+
flower.setMemento(stack.pop());
62+
LOGGER.info(flower.toString());
63+
}
64+
}
65+
}
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
/**
2+
* MIT License
3+
* <p>
4+
* Copyright (c) 2017 James
5+
* <p>
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
* <p>
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
* <p>
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
package me.zbl.memento;
25+
26+
/**
27+
* 花
28+
*/
29+
public class Flower implements Plant {
30+
31+
private FlowerType type;
32+
private final String name;
33+
private int height;
34+
private int weight;
35+
36+
public void growing() {
37+
setWeight(getWeight() * 2);
38+
setHeight(getHeight() * 3);
39+
switch (type) {
40+
case SEED: {
41+
setType(FlowerType.BURGEON);
42+
break;
43+
}
44+
case BURGEON: {
45+
setType(FlowerType.BUD);
46+
break;
47+
}
48+
case BUD: {
49+
setType(FlowerType.BLOOM);
50+
break;
51+
}
52+
case BLOOM: {
53+
setType(FlowerType.DEAD);
54+
setHeight(0);
55+
setWeight(0);
56+
break;
57+
}
58+
default:
59+
break;
60+
}
61+
}
62+
63+
FlowerMemento getMemento() {
64+
return new FlowerMemento(getType(), getHeight(), getWeight());
65+
}
66+
67+
void setMemento(Plant plant) {
68+
FlowerMemento flowerMemento = (FlowerMemento) plant;
69+
setType(flowerMemento.getType());
70+
setHeight(flowerMemento.getHeight());
71+
setWeight(flowerMemento.getWeight());
72+
}
73+
74+
@Override
75+
public String toString() {
76+
return String.format("名称:%s\t状态:%s\t质量:%d克\t高度:%d厘米", getName(), getType(), getWeight(), getHeight());
77+
}
78+
79+
public Flower(FlowerType type, String name, int height, int weight) {
80+
this.type = type;
81+
this.name = name;
82+
this.height = height;
83+
this.weight = weight;
84+
}
85+
86+
public String getName() {
87+
return this.name;
88+
}
89+
90+
@Override
91+
public int getWeight() {
92+
return this.weight;
93+
}
94+
95+
@Override
96+
public int getHeight() {
97+
return this.height;
98+
}
99+
100+
@Override
101+
public FlowerType getType() {
102+
return this.type;
103+
}
104+
105+
public void setType(FlowerType type) {
106+
this.type = type;
107+
}
108+
109+
public void setHeight(int height) {
110+
this.height = height;
111+
}
112+
113+
public void setWeight(int weight) {
114+
this.weight = weight;
115+
}
116+
117+
private static class FlowerMemento implements Plant {
118+
119+
private FlowerType type;
120+
private int height;
121+
private int weight;
122+
123+
private FlowerMemento(FlowerType type, int height, int weight) {
124+
this.type = type;
125+
this.height = height;
126+
this.weight = weight;
127+
}
128+
129+
@Override
130+
public int getWeight() {
131+
return weight;
132+
}
133+
134+
@Override
135+
public int getHeight() {
136+
return height;
137+
}
138+
139+
@Override
140+
public FlowerType getType() {
141+
return type;
142+
}
143+
}
144+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* MIT License
3+
* <p>
4+
* Copyright (c) 2017 James
5+
* <p>
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
* <p>
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
* <p>
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
package me.zbl.memento;
25+
26+
/**
27+
* 花朵的状态
28+
*/
29+
public enum FlowerType {
30+
31+
SEED("种子"), BURGEON("发芽"), BUD("花苞"), BLOOM("开放"), DEAD("凋零");
32+
33+
private String name;
34+
35+
FlowerType(String name) {
36+
this.name = name;
37+
}
38+
39+
@Override
40+
public String toString() {
41+
return name;
42+
}
43+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* MIT License
3+
* <p>
4+
* Copyright (c) 2017 James
5+
* <p>
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
* <p>
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
* <p>
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
package me.zbl.memento;
25+
26+
/**
27+
* 植物接口
28+
*/
29+
public interface Plant {
30+
31+
int getWeight();
32+
33+
int getHeight();
34+
35+
FlowerType getType();
36+
}

0 commit comments

Comments
 (0)