返回顶部

一缕半夏微光

温柔半两,从容一生

导航

软件设计23|策略模式

一、Java版本

(1)效果如下:

(2)类图如下:

(3)工程目录如下:

(4)代码如下:

AirplaneStrategy.java

 1 package strategy;
 2 
 3 public class AirplaneStrategy implements TravelStrategy {
 4 
 5     @Override
 6     public void travelMethod() {
 7         // TODO Auto-generated method stub
 8         System.out.println("乘坐飞机旅行");
 9     }
10 
11 }

BicycleTravelStrategy.java

 1 package strategy;
 2 
 3 public class BicycleTravelStrategy implements TravelStrategy {
 4 
 5     @Override
 6     public void travelMethod() {
 7         // TODO Auto-generated method stub
 8         System.out.println("自行车游");
 9     }
10 
11 }

Client.java

 1 package strategy;
 2 
 3 public class Client {
 4 
 5     public static void main(String[] args) {
 6         // TODO Auto-generated method stub
 7         System.out.println("************");
 8         Person person=new Person(new AirplaneStrategy());
 9         System.out.print("person:");
10         person.travelMethod();
11         System.out.println("************");
12         Person person1=new Person(new TrainStrategy());
13         person1.travelMethod();
14         System.out.print("person1:");
15         System.out.println("************");
16         Person person2=new Person(new BicycleTravelStrategy());
17         person2.travelMethod();
18         System.out.print("person2:");
19     }
20 
21 }

Person.java

 1 package strategy;
 2 
 3 public class Person {
 4     private TravelStrategy travelstrategy;
 5 
 6     public Person(TravelStrategy travelstrategy) {
 7         this.travelstrategy = travelstrategy;
 8     }
 9 
10     public void travelMethod() {
11         travelstrategy.travelMethod();
12     }
13 }

TrainStrategy.java

 1 package strategy;
 2 
 3 public class TrainStrategy implements TravelStrategy {
 4 
 5     @Override
 6     public void travelMethod() {
 7         // TODO Auto-generated method stub
 8         System.out.println("乘火车旅行");
 9     }
10 
11 }

TravelStrategy.java

1 package strategy;
2 
3 public interface TravelStrategy {
4     public void travelMethod();
5 }

(5)参考链接:Java设计模式(四)策略模式

二、C++版本

(1)效果如下:

(2)类图如下:

(3)代码如下:

 1 #include<iostream>
 2 using namespace std;
 3 
 4 class TravelStrategy
 5 {
 6 public:
 7     virtual void travelMethod() = 0;
 8 };
 9 
10 class Person
11 {
12 public:
13     Person(TravelStrategy* t) {
14         trainstrategy = t;
15     }
16 
17     void travelMethod() {
18         if (nullptr != trainstrategy)
19             trainstrategy->travelMethod();
20     }
21 
22 private:
23     TravelStrategy* trainstrategy = nullptr;
24 };
25 
26 // 飞机
27 class AirplaneStrategy : public TravelStrategy
28 {
29 public:
30     void travelMethod() {
31         std::cout << "乘坐飞机旅行" << std::endl;
32     }
33 };
34 
35 // 火车
36 class TrainStrategy : public TravelStrategy
37 {
38 public:
39     void travelMethod() {
40         std::cout << "乘火车旅行" << std::endl;
41     }
42 };
43 
44 // 自行车
45 class BicycleTravelStrategy : public TravelStrategy
46 {
47 public:
48     void travelMethod() {
49         std::cout << "自行车游" << std::endl;
50     }
51 };
52 
53 int main()
54 {
55     TravelStrategy* air = new AirplaneStrategy();
56     TravelStrategy* train = new TrainStrategy();
57     TravelStrategy* bicycle = new BicycleTravelStrategy();
58 
59     Person* person = new Person(air);
60     Person* person1 = new Person(train);
61     Person* person2 = new Person(bicycle);
62 
63     cout << "************" << endl;
64     cout << "person:";
65     person->travelMethod();
66     cout << "************" << endl;
67     cout << "person1:";
68     person1->travelMethod();
69     cout << "************" << endl;
70     cout << "person2:";
71     person2->travelMethod();
72 
73     return 0;
74 }

(4)参考链接:C++ 策略模式 - 来一场说走就走的旅行

posted on 2021-11-28 16:40  一缕半夏微光  阅读(28)  评论(0编辑  收藏  举报