状态模式——java实现
问题描述:
模拟实现课堂上的“银行账户”的实例,要求编写客户端测试代码模拟用户存款和取款,注意账户对象状态和行为的变化。
Java源代码:
1 //Account.java
2 package shiyan22;
3 /**
4 * @Description:环境类
5 * @author 张紫诺
6 *
7 */
8 public class Account {
9 private AccountState state;
10 private String name;
11 public Account(String name){
12 this.name=name;
13 state=new Greenstate(this);
14 System.out.println(name+"开户成功!初始金额:0元");
15 System.out.println("******************************");
16 }
17 public void setState(AccountState state){
18 this.state=state;
19 }
20 public AccountState getState() {
21 return this.state;
22 }
23
24 public String getName() {
25 return name;
26 }
27 public void setName(String name) {
28 this.name = name;
29 }
30 public void deposit(double money){
31 state.deposit(money);
32 }
33 public void withdraw(double money){
34 state.withdraw(money);
35 }
36 }
37 //AccountState.java
38 package shiyan22;
39 /**
40 * @Description:抽象状态类
41 * @author 张紫诺
42 *
43 */
44 public abstract class AccountState {
45 protected Account acc;
46 protected double balance;
47 protected String stateName;
48 public abstract void stateCheck(double balance);
49 public double getBalance() {
50 return balance;
51 }
52 public void setBalance(double balance) {
53 this.balance = balance;
54 }
55 public String getStateName() {
56 return stateName;
57 }
58 public void setStateName(String stateName) {
59 this.stateName = stateName;
60 }
61 public void deposit(double amount) {
62 System.out.println(acc.getName()+"存款"+amount+"元");
63 balance+=amount;
64 stateCheck(balance);
65 System.out.println("当前余额:"+balance+"元,当前状态:"+acc.getState().stateName);
66 }
67 public void withdraw(double amount) {
68 System.out.println(acc.getName()+"取款"+amount+"元");
69 balance-=amount;
70 stateCheck(balance);
71 System.out.println("当前余额:"+balance+"元,当前状态:"+acc.getState().stateName);
72 }
73 }
74 //Greenstate.java
75 package shiyan22;
76 /**
77 * @Description:正常状态类
78 * @author 张紫诺
79 *
80 */
81 public class Greenstate extends AccountState{
82 public Greenstate(AccountState state){
83 this.acc=state.acc;
84 this.balance=state.balance;
85 this.stateName="正常状态";
86 }
87 public Greenstate(Account acc){
88 this.balance=0;
89 this.acc=acc;
90 this.stateName="正常状态";
91 }
92 public void stateCheck(double balance){
93 if(balance<-1000){
94 acc.setState(new Redstate(this));
95 }else if(balance>=-1000&&balance<0){
96 acc.setState(new Yellowstate(this));
97 }
98 }
99 }
100 //Redstate.java
101 package shiyan22;
102 /**
103 * @Description:透支状态类
104 * @author 张紫诺
105 *
106 */
107 public class Redstate extends AccountState {
108
109 public Redstate(AccountState state) {
110 this.acc=state.acc;
111 this.balance=state.getBalance();
112 this.stateName="透支状态";
113 }
114
115 public void stateCheck(double balance) {
116 if(balance >= -1000 && balance < 0){
117 acc.setState(new Yellowstate(this));
118 }else if(balance >= 0){
119 acc.setState(new Greenstate(this));
120 }
121 }
122 public void withdraw(double amount) {
123 System.out.println("对不起,"+acc.getName()+",您不能取款");
124 System.out.println("当前余额:"+balance+"元,当前状态:"+acc.getState().stateName);
125 }
126 }
127 //Yellowstate.java
128 package shiyan22;
129 /**
130 * @Description:欠费状态类
131 * @author 张紫诺
132 *
133 */
134 public class Yellowstate extends AccountState {
135
136 public Yellowstate(AccountState state) {
137 this.acc=state.acc;
138 this.balance=state.getBalance();
139 this.stateName="欠费状态";
140 }
141
142 public void stateCheck(double balance) {
143 if(balance >=0 ){
144 acc.setState(new Greenstate(this));
145 }else if(balance < -1000){
146 acc.setState(new Redstate(this));
147 }
148 }
149 }
150 //Client.java
151 package shiyan22;
152 public class Client {
153 public static void main(String args[]){
154 Account acc=new Account("张三");
155 acc.deposit(2000);
156 System.out.println("---------------------------------------");
157 acc.withdraw(500);
158 System.out.println("---------------------------------------");
159 acc.withdraw(2000);
160 System.out.println("---------------------------------------");
161 acc.withdraw(600);
162 System.out.println("---------------------------------------");
163 acc.withdraw(200);
164 }
165 }

浙公网安备 33010602011771号