1. 程式人生 > 其它 >軟體設計-策略模式

軟體設計-策略模式

旅行方式的選擇
旅遊的出行方式有乘坐飛機旅行、乘火車旅行和自行車遊,不同的旅遊方式有不同的實現過程,客戶可以根據自己的需要選擇一種合適的旅行方式。

類圖

Java

package rjsj.no23;

public class BicycleStrategy implements TravelStrategy{
    @Override
    public void travel() {
        System.out.println("騎自行車旅遊");
    }
}
package rjsj.no23;

public class Client {
    public
static void main(String[] args) { Traveler traveler = new Traveler(); System.out.println("旅遊開始!"); TravelStrategy travelStrategy = new TrainStrategy(); traveler.setTravelStrategy(travelStrategy); traveler.travel(); System.out.println("**************************************************"); System.out.println(
"旅遊開始!"); travelStrategy = new AirplaneStrategy(); traveler.setTravelStrategy(travelStrategy); traveler.travel(); System.out.println("**************************************************"); System.out.println("旅遊開始!"); travelStrategy = new BicycleStrategy(); traveler.setTravelStrategy(travelStrategy); traveler.travel(); System.out.println(
"**************************************************"); } }
package rjsj.no23;

public class TrainStrategy implements TravelStrategy{
    @Override
    public void travel() {
        System.out.println("坐火車旅遊");
    }
}
package rjsj.no23;

public class Traveler {
    private TravelStrategy travelStrategy;

    public void setTravelStrategy(TravelStrategy travelStrategy) {
        this.travelStrategy = travelStrategy;
    }

    public void travel(){
        travelStrategy.travel();
    }
}
package rjsj.no23;

public interface TravelStrategy {
    void travel();
}
package rjsj.no23;

public class AirplaneStrategy implements TravelStrategy{
    @Override
    public void travel() {
        System.out.println("乘坐飛機旅遊");
    }
}

C++

#include<iostream>
using namespace std;
class TravelStrategy {
public:
    virtual void travel()=0;
};
class Person {
private:
    TravelStrategy *strategy;
public:
    void setStrategy(TravelStrategy *strategy) {
        this->strategy=strategy;
    }
    void travel() {
        this->strategy->travel();
    }
};
class TrainStrategy : public TravelStrategy{
public:
    void travel() {
        cout<<"坐火車"<<endl;
    }
};
class BicycleStrategy : public TravelStrategy{
public:
    void travel() {
        cout<<"騎自行車"<<endl;
    }
};
class AirplaneStrategy: public TravelStrategy{
public:
    void travel() {
        cout<<"坐飛機"<<endl;
    }
};
int main(){
        Person *pr=new Person();

        cout<<"旅遊開始,請選擇交通工具:"<<endl;
        TravelStrategy *strategy=new TrainStrategy();
        pr->setStrategy(strategy);
        pr->travel();
        cout<<"****************************************"<<endl;

        cout<<"旅遊開始,請選擇交通工具:"<<endl;
        TravelStrategy *strategy2=new BicycleStrategy();
        pr->setStrategy(strategy2);
        pr->travel();
        cout<<"****************************************"<<endl;

        cout<<"旅遊開始,請選擇交通工具:"<<endl;
        TravelStrategy *strategy3=new AirplaneStrategy();
        pr->setStrategy(strategy3);
        pr->travel();
}

執行結果: