java23种设计模式之一: 策略模式

  由于最近在研究学习设计模式,我会用自己的理解方式来表述对设计模式的学习和认识,通过最常用、好记的案例来记住和使用设计模式,希望对设计代码方面有所提高和改进。

一.应用背景
     在软件开发中常常遇到这种情况,实现某一个功能有多种算法或者策略,我们可以根据应用场景的不同选择不同的算法或者策略来完成该功能。把一个类(A)中经常改变或者将来可能改变的部分提取出来,作为一个接口(B),然后在类(A)中包含这个接口(B),这样类(A)的实例在运行时就可以随意调用实现了这个接口的类(C)的行为。比如定义一系列的算法,把每一个算法封装起来, 并且使它们可相互替换,使得算法可独立于使用它的客户而变化。这就是策略模式。

二.优、缺点

  优点:
    1、可以动态的改变对象的行为
  缺点:
    1、客户端必须知道所有的策略类,并自行决定使用哪一个策略类
    2、策略模式将造成产生很多策略类

三.组成

  1.运行环境类:Strategy

    这个策略模式运行的环境,其实也就是在哪里使用

  2.应用场景类:Person

    这个就是客户端访问的类,也就是该类的对象所持有的策略

  3具体策略类:Car

    具体实现策略类

  4..抽象策略类:CarFunction

    根据不同的需求,产生不同的策略或算法的接口

四.代码实现

  1.抽象策略类:CarFunction

 1 package com.design.strategy;
 2 /**
 3 * @ClassName   : CarFunction 
 4 * @Description : 策略类 
 5 *
 6 */
 7 
 8 public interface CarFunction {
 9     void run();        //每辆车有不同的行驶方法
10 }

  2.具体策略父类

 1 package com.design.strategy;
 2 /**
 3  * 
 4  * @ClassName   : Car 
 5  * @Description : 每个车都具有的相同的属性和行为
 6  *
 7  */
 8 public class Car implements CarFunction {
 9     protected String name;            //车名字
10     protected String color;            //车颜色
11    
12     
13     public Car(String name, String color) {
14         this.name = name;
15         this.color = color;
16     }
17 
18     @Override
19     public void run() {
20         System.out.println(color +" " + name  +"在行驶。。。");
21     }
22     
23 }

  3.具体策略实现子类

 1 package com.design.strategy;
 2 /**
 3  * 
 4  * @ClassName   : SmallCar 
 5  * @Description : 具体策略实现子类
 6  *
 7  */
 8 public class SmallCar extends Car {
 9 
10     public SmallCar(String name, String color) {
11         super(name, color);
12     }
13     
14     public void run() {
15         System.out.println(color +" " + name  +"在高速的行驶。。。");
16     }
17     
18 }
 1 package com.design.strategy;
 2 
 3 public class BussCar extends Car{
 4 
 5     public BussCar(String name, String color) {
 6         super(name, color);
 7     }
 8     
 9     public void run() {
10         System.out.println(color +" " + name  +"在缓慢的行驶。。。");
11     }
12 }

 

 4.应用场景类

 1 package com.design.strategy;
 2 /**
 3  * 
 4  * @ClassName   : Person 
 5  * @Description : 应用场景类
 6  *
 7  */
 8 public class Person {
 9     private String name;    //姓名
10     private Integer age;    //年龄
11     private Car car;        //拥有车
12     
13     public void driver(Car car){
14         System.out.print(name +"  "+ age+" 岁 "+" 开着");
15         car.run();
16     }
17 
18     public Person(String name,Integer age) {
19         this.name=name;
20         this.age=age;
21     }
22 
23 }

  5.运行环境类:Strategy

 1 package com.design.strategy;
 2 /**
 3  * 
 4  * @ClassName   : Strategy 
 5  * @Description : 运行环境类:Strategy    
 6  * @date        : 2017年12月9日 上午11:43:58
 7  *
 8  */
 9 public class Strategy {
10     public static void main(String[] args) {
11         Car smallCar = new SmallCar("路虎","黑色");
12         Car bussCar = new BussCar("公交车","白色");
13         Person p1 = new Person("小明", 20);
14         p1.driver(smallCar);
15         p1.driver(bussCar);
16     }
17 }
18 
19 运行结果:
20 小明  20 岁  开着黑色 路虎在高速的行驶。。。
21 小明  20 岁  开着白色 公交车在缓慢的行驶。。。

 

五.总结

  策略模式可以理解为老司机开车,但是他今天想到路虎,明天想开奔驰。。。,针对他不同的需求,来产生不同的应对策略,好记一点就是:老司机开车!!! 哈哈。。。

六.延伸  

  同样也可以延伸到商家搞活动,消费多少元减50之类的应用场景,等等,举一反三,会发现生活中有很多的例子都是用到了策略模式。

 

posted @ 2017-12-09 11:54  硝烟漫过十八岁  阅读(25511)  评论(11编辑  收藏  举报