实验21
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package shiyan21; import java.util.ArrayList; public class Gufen extends Subject{ public void up(){ System.out.println("----股份上涨100%----"); for(Observer guan:observers){ guan.buy(); } } public void down(){ System.out.println("----股份下跌100%----"); for(Observer guan:observers){ guan.cry(); } } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package shiyan21; public class Gumin implements Observer{ private String name; public Gumin(String a){name=a;} public void buy(){ System.out.println("股民"+name+"买股票"); } public void cry(){ System.out.println("股民"+name+"不哭都不行了"); } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package shiyan21; public class main { public static void main(String[] args) { Gufen f=new Gufen(); Gumin g1=new Gumin("UZI"); Gumin g2=new Gumin("MLXG"); f.attach(g1); f.attach(g2); f.up(); f.down(); } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package shiyan21; public interface Observer { public void buy(); public void cry(); }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package shiyan21; import java.util.ArrayList; public abstract class Subject { protected ArrayList<Observer> observers; public Subject(){ observers=new ArrayList<>(); } public void attach(Observer a){ observers.add(a); } public void detach(Observer a){ observers.remove(a); } public abstract void up(); public abstract void down(); }
实验22
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<iostream> using namespace std; class Account; class AccountState { public: Account* acc; double balance; public: virtual void stateCheck() = 0; void deposit(double amount); virtual void withdraw(double amount); }; class Account { private: AccountState* state; string owner; public: Account(string owner, double init); void setState(AccountState* state) { this->state = state; } AccountState* getState() { return this->state; } string getOwner() { return this->owner; } void deposit(double amount) { state->deposit(amount); } void withdraw(double amount) { state->withdraw(amount); } }; class RedState :public AccountState { public: RedState(AccountState* state) { this->balance = state->balance; this->acc = state->acc; } void stateCheck(); }; class YellowState :public AccountState { public: YellowState(AccountState* state) { this->balance = state->balance; this->acc = state->acc; } void stateCheck(); }; class GreenState :public AccountState { public: GreenState(double balance, Account* acc) { this->balance = balance; this->acc = acc; } GreenState(AccountState* state) { this->acc = state->acc; this->balance = state->balance; } void stateCheck() { if (balance >= -1000 && balance < 0) { acc->setState(new YellowState(this)); } else if (balance < -1000) { acc->setState(new RedState(this)); } else { acc->setState(new GreenState(this)); } } }; void RedState::stateCheck() { if (balance >= -1000 && balance < 0) { acc->setState(new YellowState(this)); } else if (balance < -1000) { acc->setState(new RedState(this)); } else { acc->setState(new GreenState(this)); } } void YellowState::stateCheck() { if (balance >= -1000 && balance < 0) { acc->setState(new YellowState(this)); } else if (balance < -1000) { acc->setState(new RedState(this)); } else { acc->setState(new GreenState(this)); } } void AccountState::deposit(double amount) { cout << "存款" << amount << endl; this->balance += amount; stateCheck(); cout << "账户余额:" << this->balance << endl; } void AccountState::withdraw(double amount) { cout << "取款" << amount << endl; if (amount > balance)cout << "您的账户余额不足,不能取款!" << endl; else this->balance -= amount; stateCheck(); cout << "账户余额:" << this->balance << endl; } Account::Account(string owner, double init) { this->owner = owner; this->state = new GreenState(init, this); } int main() { Account* account = new Account("用户", 0); account->deposit(1000); cout << "------------------------------" << endl; account->withdraw(5000); cout << "------------------------------" << endl; account->deposit(1000); cout << "------------------------------" << endl; account->withdraw(2000); account->withdraw(100); cout << "------------------------------" << endl; account->deposit(2000); cout << "------------------------------" << endl; return 0; }
实验23
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package shiyan23; public class AirplaneStrategy implements TravelStrategy{ public void travel() { System.out.println("乘坐飞机旅行"); } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package shiyan23; public class BicycleStrategy implements TravelStrategy{ public void travel() { System.out.println("自行车旅行"); } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package shiyan23; import java.util.Scanner; public class Client { public static void main(String[] args) { Person p = new Person(); System.out.println("请选择:1、飞机 2、火车 3、自行车"); Scanner input=new Scanner(System.in); int i=input.nextInt(); if(i==1) { p.setStrategy(new AirplaneStrategy()); }else if(i==2) { p.setStrategy(new TrainStrategy()); }else if(i==3) { p.setStrategy(new BicycleStrategy()); } p.travel(); } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package shiyan23; public class Person { //聚合策略类对象 private TravelStrategy strategy; public void setStrategy(TravelStrategy strategy) { this.strategy=strategy; } public void travel() { strategy.travel(); } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package shiyan23; public class TrainStrategy implements TravelStrategy{ public void travel() { System.out.println("乘火车旅行"); } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package shiyan23; public interface TravelStrategy { public void travel(); }