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