Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2a4daaa

Browse files
author
deeper
committed
add state mode
1 parent e83e9dc commit 2a4daaa

File tree

6 files changed

+121
-3
lines changed

6 files changed

+121
-3
lines changed

‎src/com/ruoxu/pattern/state/Demo.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,22 @@
55
* 策略模式的行为是彼此独立,可相互替换的。
66
* 状态模式的行为是平行,不可替换的。
77
*
8-
*
9-
*
8+
* 使用场景:
9+
* 一个对象的行为取决于它的状态,并且它必须在运行时根据状态改变它的行为。
10+
* 代码中有大量if-else或者switch,且这些分支依赖于该对象的状态
1011
*/
1112
public class Demo {
12-
13+
public static void main(String[] args) {
14+
TvController tvController = new TvController();
15+
tvController.powerOn();
16+
17+
tvController.prevChannel();
18+
tvController.nextChannel();
19+
tvController.turnUp();
20+
tvController.turnDown();
21+
22+
tvController.powerOff();
23+
tvController.prevChannel();
24+
tvController.nextChannel();
25+
}
1326
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.ruoxu.pattern.state;
2+
3+
public interface PowerController {
4+
void powerOn();
5+
void powerOff();
6+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.ruoxu.pattern.state;
2+
/**
3+
* 关机状态,什么都不做
4+
*/
5+
public class PowerOffState implements TvState{
6+
7+
@Override
8+
public void prevChannel() {
9+
// nothing to do
10+
}
11+
12+
@Override
13+
public void nextChannel() {
14+
// nothing to do
15+
}
16+
17+
@Override
18+
public void turnUp() {
19+
// nothing to do
20+
}
21+
22+
@Override
23+
public void turnDown() {
24+
// nothing to do
25+
}
26+
27+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.ruoxu.pattern.state;
2+
/**
3+
* 开机状态
4+
*/
5+
public class PowerOnState implements TvState{
6+
7+
@Override
8+
public void prevChannel() {
9+
System.out.println("上一频道");
10+
}
11+
12+
@Override
13+
public void nextChannel() {
14+
System.out.println("下一频道");
15+
}
16+
17+
@Override
18+
public void turnUp() {
19+
System.out.println("调高音量");
20+
}
21+
22+
@Override
23+
public void turnDown() {
24+
System.out.println("调低音量");
25+
}
26+
27+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.ruoxu.pattern.state;
2+
3+
public class TvController implements PowerController{
4+
private TvState mTvState;
5+
6+
@Override
7+
public void powerOn() {
8+
setTvState(new PowerOnState());
9+
System.out.println("==== 开机了 ====");
10+
}
11+
12+
@Override
13+
public void powerOff() {
14+
setTvState(new PowerOffState());
15+
System.out.println("==== 关机了 ====");
16+
}
17+
18+
public void prevChannel() {
19+
mTvState.prevChannel();
20+
}
21+
22+
public void nextChannel() {
23+
mTvState.nextChannel();
24+
}
25+
26+
public void turnUp() {
27+
mTvState.turnUp();
28+
}
29+
30+
public void turnDown() {
31+
mTvState.turnDown();
32+
}
33+
34+
private void setTvState(TvState tvState){
35+
this.mTvState = tvState;
36+
}
37+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.ruoxu.pattern.state;
2+
3+
public interface TvState {
4+
void prevChannel();// 上一频道
5+
void nextChannel();// 下一频道
6+
void turnUp();// 调高音量
7+
void turnDown();// 调低音量
8+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /