Skip to content

Commit 4fa2eb2

Browse files
committed
添加状态模式代码
1 parent 0ecc20a commit 4fa2eb2

File tree

5 files changed

+259
-0
lines changed

5 files changed

+259
-0
lines changed

src/State(状态)/README.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,9 @@
1111
-- ������˾������һ��ֽ����Ϸ�������ڸ���Ϸ�������û���ɫ�������ż�(Primary)��������(Secondary)�����ּ�(Professional)�͹ǻҼ�(Final)���ֵȼ�����ɫ�ĵȼ�����������Ӧ����Ϸʤ�������ӻ��֣�ʧ����۳����֡����ż��������������Ϸ����play() ����������������Ϸʤ�����ּӱ�����doubleScore()�����ּ��������������������ӻ��ƹ���changeCards()���ǻҼ��ڸ��ּ�������������͵�����˵��ƹ���peekCards()����ʹ��״̬ģʽ����Ƹ�ϵͳ��
1212
-- �����Զ�����4��״̬�������ء��ߡ�ֹͣ��ÿ��״ִ̬�в�ͬ�ķ����ܹ��ı�״̬�����Ҳ�ͬ��״ֻ̬����ִ���ض��ķ����������貹������쳣״̬��
1313
���ŵ㣺
14+
-- ״̬ģʽ�����ض�״̬��ص���Ϊ�ֲ�����һ��״̬�У����ҽ���ͬ״̬����Ϊ�ָ�������㡰��һְ��ԭ�򡱡�
15+
-- ���ٶ������໥����������ͬ��״̬��������Ķ����л�ʹ��״̬ת����ø�����ȷ���Ҽ��ٶ������໥������
16+
-- �����ڳ������չ��ͨ�������µ���������׵������µ�״̬��ת����
1417
��ȱ�㣺
18+
-- ״̬ģʽ��ʹ�ñ�Ȼ������ϵͳ���������ĸ�����
19+
-- ״̬ģʽ�Ľṹ��ʵ�ֶ���Ϊ���ӣ����ʹ�ò����ᵼ�³���ṹ�ʹ���Ļ��ҡ�

src/State(状态)/common.java

Whitespace-only changes.

src/State(状态)/common.js

Whitespace-only changes.

src/State(状态)/common.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
interface State {
2+
handle(context: Context): void;
3+
}
4+
5+
class ConcreteStateA implements State {
6+
public handle(context: Context): void {
7+
console.log("`handle` method of ConcreteStateA is being called!");
8+
context.State = new ConcreteStateB();
9+
}
10+
}
11+
12+
class ConcreteStateB implements State {
13+
public handle(context: Context): void {
14+
console.log("`handle` method of ConcreteStateB is being called!");
15+
context.State = new ConcreteStateA();
16+
}
17+
}
18+
19+
class Context {
20+
private state: State;
21+
constructor(state: State) {
22+
this.state = state;
23+
}
24+
get State(): State {
25+
return this.state;
26+
}
27+
set State(state: State) {
28+
this.state = state;
29+
}
30+
public request(): void {
31+
console.log("request is being called!");
32+
this.state.handle(this);
33+
}
34+
}
35+
36+
class Client {
37+
public static main(): void {
38+
const context: Context = new Context(
39+
new ConcreteStateA()
40+
);
41+
context.request();
42+
context.request();
43+
context.request();
44+
context.request();
45+
context.request();
46+
context.request();
47+
context.request();
48+
context.request();
49+
}
50+
}
51+
Client.main()
52+
53+
// request is being called!
54+
// `handle` method of ConcreteStateA is being called!
55+
// request is being called!
56+
// `handle` method of ConcreteStateB is being called!
57+
// request is being called!
58+
// `handle` method of ConcreteStateA is being called!
59+
// request is being called!
60+
// `handle` method of ConcreteStateB is being called!
61+
// request is being called!
62+
// `handle` method of ConcreteStateA is being called!
63+
// request is being called!
64+
// `handle` method of ConcreteStateB is being called!
65+
// request is being called!
66+
// `handle` method of ConcreteStateA is being called!
67+
// request is being called!
68+
// `handle` method of ConcreteStateB is being called!

src/State(状态)/demo.ts

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
// 账户有几种状态:正常,透支,受限
2+
3+
// 状态抽象类
4+
abstract class State {
5+
private name: string;
6+
protected acc: Account;
7+
constructor(name: string) {
8+
this.name = name;
9+
}
10+
getName() {
11+
return this.name;
12+
}
13+
abstract deposit(amount: number);
14+
abstract withdraw(amount: number);
15+
abstract computeInterest();
16+
abstract stateCheck();
17+
}
18+
19+
// 正常状态类
20+
class NormalState extends State {
21+
acc: Account;
22+
constructor(acc: Account) {
23+
super('正常');
24+
this.acc = acc;
25+
}
26+
deposit(amount: number) {
27+
this.acc.setBalance(this.acc.getBalance() + amount);
28+
this.stateCheck();
29+
}
30+
withdraw(amount: number) {
31+
this.acc.setBalance(this.acc.getBalance() - amount);
32+
this.stateCheck();
33+
}
34+
computeInterest() {
35+
console.log('正常状态,无须支付利息');
36+
}
37+
// 状态转换
38+
stateCheck() {
39+
if (this.acc.getBalance() > -2000 && this.acc.getBalance() <= 0) {
40+
this.acc.setState(new OverdraftState(this.acc));
41+
} else if (this.acc.getBalance() == -2000) {
42+
this.acc.setState(new RestrictedState(this.acc));
43+
} else if (this.acc.getBalance() < -2000) {
44+
console.log('操作受限');
45+
}
46+
}
47+
}
48+
49+
// 透支状态
50+
class OverdraftState extends State {
51+
acc: Account;
52+
constructor(acc: Account) {
53+
super('透支');
54+
this.acc = acc;
55+
}
56+
deposit(amount: number) {
57+
this.acc.setBalance(this.acc.getBalance() + amount);
58+
this.stateCheck();
59+
}
60+
withdraw(amount: number) {
61+
this.acc.setBalance(this.acc.getBalance() - amount);
62+
this.stateCheck();
63+
}
64+
computeInterest() {
65+
console.log('计算利息');
66+
}
67+
// 状态转换
68+
stateCheck() {
69+
if (this.acc.getBalance() > 0) {
70+
this.acc.setState(new NormalState(this.acc));
71+
} else if (this.acc.getBalance() == -2000) {
72+
this.acc.setState(new RestrictedState(this.acc));
73+
} else if (this.acc.getBalance() < -2000) {
74+
console.log('操作受限');
75+
}
76+
}
77+
}
78+
79+
// 受限状态
80+
class RestrictedState extends State {
81+
acc: Account;
82+
constructor(acc: Account) {
83+
super('受限');
84+
this.acc = acc;
85+
}
86+
deposit(amount: number) {
87+
this.acc.setBalance(this.acc.getBalance() + amount);
88+
this.stateCheck();
89+
}
90+
withdraw(ammount: number) {
91+
console.log('账号受限,取款失败');
92+
}
93+
computeInterest() {
94+
console.log('计算利息');
95+
}
96+
// 状态转换
97+
stateCheck() {
98+
if (this.acc.getBalance() > 0) {
99+
this.acc.setState(new NormalState(this.acc));
100+
} else if (this.acc.getBalance() > -2000) {
101+
this.acc.setState(new OverdraftState(this.acc));
102+
}
103+
}
104+
}
105+
106+
// 账户类,代表状态模式中的环境
107+
class Account {
108+
private name: string;
109+
private state: State;
110+
// 余额
111+
private balance = 0;
112+
// 初始时为正常状态
113+
constructor(name: string) {
114+
this.name = name;
115+
this.state = new NormalState(this);
116+
console.log(`用户 ${this.name} 开户,余额为 ${this.balance}`);
117+
console.log('--------');
118+
}
119+
getBalance(): number {
120+
return this.balance;
121+
}
122+
setBalance(balance: number) {
123+
this.balance = balance;
124+
}
125+
setState(state: State) {
126+
this.state = state;
127+
}
128+
// 存款
129+
deposit(amount: number) {
130+
this.state.deposit(amount);
131+
console.log(`存款 ${amount}`);
132+
console.log(`余额为 ${this.balance}`);
133+
console.log(`账户状态为 ${this.state.getName()}`);
134+
console.log('--------');
135+
}
136+
// 取款
137+
withdraw(amount: number) {
138+
this.state.withdraw(amount);
139+
console.log(`取款 ${amount}`);
140+
console.log(`余额为 ${this.balance}`);
141+
console.log(`账户状态为 ${this.state.getName()}`);
142+
console.log('--------');
143+
}
144+
// 结算利息
145+
computeInterest() {
146+
this.state.computeInterest();
147+
}
148+
}
149+
150+
class Client {
151+
public static main(): void {
152+
const acc = new Account('Bob');
153+
acc.deposit(1000);
154+
acc.withdraw(2000);
155+
acc.deposit(3000);
156+
acc.withdraw(4000);
157+
acc.withdraw(1000);
158+
acc.computeInterest();
159+
}
160+
}
161+
Client.main()
162+
163+
// 用户 Bob 开户,余额为 0
164+
// --------
165+
// 存款 1000
166+
// 余额为 1000
167+
// 账户状态为 正常
168+
// --------
169+
// 取款 2000
170+
// 余额为 -1000
171+
// 账户状态为 透支
172+
// --------
173+
// 存款 3000
174+
// 余额为 2000
175+
// 账户状态为 正常
176+
// --------
177+
// 取款 4000
178+
// 余额为 -2000
179+
// 账户状态为 受限
180+
// --------
181+
// 账号受限,取款失败
182+
// 取款 1000
183+
// 余额为 -2000
184+
// 账户状态为 受限
185+
// --------
186+
// 计算利息

0 commit comments

Comments
 (0)