由于最近在研究学习设计模式,我会用自己的理解方式来表述对设计模式的学习和认识,通过最常用、好记的案例来记住和使用设计模式,希望对设计代码方面有所提高和改进。
一.应用背景
在软件开发中常常遇到这种情况,实现某一个功能有多种算法或者策略,我们可以根据应用场景的不同选择不同的算法或者策略来完成该功能。把一个类(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.具体策略父类
package com.design.strategy;
/**
*
* @ClassName : Car
* @Description : 每个车都具有的相同的属性和行为
*
*/
public class Car implements CarFunction {
protected String name; //车名字
protected String color; //车颜色 public Car(String name, String color) {
this.name = name;
this.color = color;
} @Override
public void run() {
System.out.println(color +" " + name +"在行驶。。。");
} }
3.具体策略实现子类
package com.design.strategy;
/**
*
* @ClassName : SmallCar
* @Description : 具体策略实现子类
*
*/
public class SmallCar extends Car { public SmallCar(String name, String color) {
super(name, color);
} public void run() {
System.out.println(color +" " + name +"在高速的行驶。。。");
} }
package com.design.strategy; public class BussCar extends Car{ public BussCar(String name, String color) {
super(name, color);
} public void run() {
System.out.println(color +" " + name +"在缓慢的行驶。。。");
}
}
4.应用场景类
package com.design.strategy;
/**
*
* @ClassName : Person
* @Description : 应用场景类
*
*/
public class Person {
private String name; //姓名
private Integer age; //年龄
private Car car; //拥有车 public void driver(Car car){
System.out.print(name +" "+ age+" 岁 "+" 开着");
car.run();
} public Person(String name,Integer age) {
this.name=name;
this.age=age;
} }
5.运行环境类:Strategy
package com.design.strategy;
/**
*
* @ClassName : Strategy
* @Description : 运行环境类:Strategy
* @date : 2017年12月9日 上午11:43:58
*
*/
public class Strategy {
public static void main(String[] args) {
Car smallCar = new SmallCar("路虎","黑色");
Car bussCar = new BussCar("公交车","白色");
Person p1 = new Person("小明", 20);
p1.driver(smallCar);
p1.driver(bussCar);
}
} 运行结果:
小明 20 岁 开着黑色 路虎在高速的行驶。。。
小明 20 岁 开着白色 公交车在缓慢的行驶。。。
五.总结
策略模式可以理解为老司机开车,但是他今天想到路虎,明天想开奔驰。。。,针对他不同的需求,来产生不同的应对策略,好记一点就是:老司机开车!!! 哈哈。。。
六.延伸
同样也可以延伸到商家搞活动,消费多少元减50之类的应用场景,等等,举一反三,会发现生活中有很多的例子都是用到了策略模式。