Skip to content

Commit 7a993aa

Browse files
committed
行为型模式:备忘录模式
1 parent 21b05ce commit 7a993aa

File tree

1 file changed

+150
-0
lines changed

1 file changed

+150
-0
lines changed
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
package com.liebrother.designpatterns.memento;
2+
3+
/**
4+
* @author James
5+
* @date 2019/3/12
6+
*/
7+
public class MementoTest {
8+
9+
public static void main(String[] args) {
10+
Deployer deployer = new Deployer();
11+
deployer.setApp(new App("apply-system", "1.0.0"));
12+
13+
System.out.println("1. 暂停旧应用");
14+
deployer.stopApp();
15+
16+
System.out.println("2. 备份旧应用");
17+
Space aaa = new Space();
18+
aaa.setAppBackup(deployer.createAppBackup());
19+
20+
System.out.println("3. 拷贝新应用到服务器");
21+
deployer.setApp(new App("apply-system", "2.0.0"));
22+
deployer.showApp();
23+
24+
System.out.println("4. 启动新应用");
25+
deployer.startApp();
26+
27+
System.out.println("5. 有异常,暂停新应用");
28+
deployer.stopApp();
29+
30+
System.out.println("6. 回滚旧应用,拷贝备份的旧应用到服务器");
31+
deployer.setAppBackup(aaa.getAppBackup());
32+
deployer.showApp();
33+
34+
System.out.println("7. 启动备份的旧应用");
35+
deployer.startApp();
36+
}
37+
38+
}
39+
40+
41+
/**
42+
* 系统发布
43+
* 1. 关闭应用
44+
* 2. 备份应用
45+
* 3. 拷贝新应用
46+
* 4. 启动应用
47+
*
48+
* 回滚
49+
* 5. 关闭应用
50+
* 6. 恢复应用
51+
* 7. 启动应用
52+
*/
53+
54+
class App {
55+
private String content;
56+
private String version;
57+
58+
public App(String content, String version) {
59+
this.content = content;
60+
this.version = version;
61+
}
62+
63+
public String getContent() {
64+
return content;
65+
}
66+
67+
public void setContent(String content) {
68+
this.content = content;
69+
}
70+
71+
public String getVersion() {
72+
return version;
73+
}
74+
75+
public void setVersion(String version) {
76+
this.version = version;
77+
}
78+
79+
@Override
80+
public String toString() {
81+
return "App{" +
82+
"content='" + content + '\'' +
83+
", version='" + version + '\'' +
84+
'}';
85+
}
86+
}
87+
88+
89+
class AppBackup {
90+
91+
private App app;
92+
93+
public AppBackup(App app) {
94+
this.app = app;
95+
}
96+
97+
public App getApp() {
98+
return app;
99+
}
100+
101+
public void setApp(App app) {
102+
this.app = app;
103+
}
104+
}
105+
106+
class Deployer {
107+
108+
private App app;
109+
110+
public App getApp() {
111+
return app;
112+
}
113+
114+
public void setApp(App app) {
115+
this.app = app;
116+
}
117+
118+
public AppBackup createAppBackup() {
119+
return new AppBackup(app);
120+
}
121+
122+
public void setAppBackup(AppBackup appBackup) {
123+
this.app = appBackup.getApp();
124+
}
125+
126+
public void showApp() {
127+
System.out.println(this.app.toString());
128+
}
129+
130+
public void stopApp() {
131+
System.out.println("暂停应用:" + this.app.toString());
132+
}
133+
134+
public void startApp() {
135+
System.out.println("启动应用:" + this.app.toString());
136+
}
137+
138+
}
139+
140+
class Space {
141+
private AppBackup appBackup;
142+
143+
public AppBackup getAppBackup() {
144+
return appBackup;
145+
}
146+
147+
public void setAppBackup(AppBackup appBackup) {
148+
this.appBackup = appBackup;
149+
}
150+
}

0 commit comments

Comments
 (0)