博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java23种设计模式之一: 策略模式
阅读量:7023 次
发布时间:2019-06-28

本文共 3158 字,大约阅读时间需要 10 分钟。

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

一.应用背景

     在软件开发中常常遇到这种情况,实现某一个功能有多种算法或者策略,我们可以根据应用场景的不同选择不同的算法或者策略来完成该功能。把一个类(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     @Override19     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之类的应用场景,等等,举一反三,会发现生活中有很多的例子都是用到了策略模式。

 

转载于:https://www.cnblogs.com/MrRightZhao/p/8000421.html

你可能感兴趣的文章
mysql数据库中的存储引擎是什么意思呢
查看>>
mysql连接超时的问题处理
查看>>
原创《weex面向未来的架构》
查看>>
原创,自己做的一个简单实用的提示小插件,兼容性很好,基本上都兼容!
查看>>
webform 上传
查看>>
P3358 最长k可重区间集问题
查看>>
oracle数据库的随堂笔记(一)-常用操作
查看>>
中国合伙人
查看>>
实验三+138+牟平
查看>>
LinQ
查看>>
壳的装载过程
查看>>
树形结构
查看>>
在内存中是类似于这种形式存储
查看>>
i = NULL;
查看>>
α测试,β测试,冒烟测试,验收测试
查看>>
所不为人知的Python装饰器
查看>>
linux内核模块相关命令:lsmod,depmod,modprobe,modinfo,insmod,rmmod 使用说明
查看>>
xp 盗版变正版 vbs
查看>>
javaScript------------------基础
查看>>
时间复杂度一定的算法能处理的数据规模
查看>>