Skip to content

Commit dbcbc12

Browse files
committed
添加 类结构
1 parent 516f5c9 commit dbcbc12

File tree

4 files changed

+252
-0
lines changed

4 files changed

+252
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
/**
30+
* Memento
31+
*/
32+
public class Application {
33+
34+
private static final Logger LOGGER = LoggerFactory.getLogger(Application.class);
35+
36+
public static void main(String[] args) {
37+
38+
}
39+
}
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
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+
public Flower(FlowerType type, String name, int height, int weight) {
75+
this.type = type;
76+
this.name = name;
77+
this.height = height;
78+
this.weight = weight;
79+
}
80+
81+
public String getName() {
82+
return this.name;
83+
}
84+
85+
@Override
86+
public int getWeight() {
87+
return this.weight;
88+
}
89+
90+
@Override
91+
public int getHeight() {
92+
return this.height;
93+
}
94+
95+
@Override
96+
public FlowerType getType() {
97+
return this.type;
98+
}
99+
100+
public void setType(FlowerType type) {
101+
this.type = type;
102+
}
103+
104+
public void setHeight(int height) {
105+
this.height = height;
106+
}
107+
108+
public void setWeight(int weight) {
109+
this.weight = weight;
110+
}
111+
112+
private static class FlowerMemento implements Plant {
113+
114+
private FlowerType type;
115+
private int height;
116+
private int weight;
117+
118+
private FlowerMemento(FlowerType type, int height, int weight) {
119+
this.type = type;
120+
this.height = height;
121+
this.weight = weight;
122+
}
123+
124+
@Override
125+
public int getWeight() {
126+
return weight;
127+
}
128+
129+
@Override
130+
public int getHeight() {
131+
return height;
132+
}
133+
134+
@Override
135+
public FlowerType getType() {
136+
return type;
137+
}
138+
}
139+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
}
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)