策略模式
定义:
定义了算法家族,分别封装起来,让他们之间可以互相替换,此模式让算法的变化不会影响到使用算法的用户。
可以消除if else 和case
类型:
行为型
适应场景:
系统有很多类,而他们的区别仅仅在于他们的行为不同。
一个系统需要动态地在几种算法中选择一种
优点:
开闭原则,在不修改原有系统地基础上修改行为,并且行为可以扩展。
避免使用多重条件转移语句也会降低耦合
提高算法的保密性和安全性
缺点:
客户端必须知道所有的策略类,并自行决定使用哪个策略类
产生很多策略模式
策略模式相关的设计模式
策略模式和工厂模式:
类型不同:策略模式是行为型的,工厂模式是创建型的
工厂模式创建符合要求的对象,策略模式接受创建好的对象从而实现不同的行为。
策略模式和状态模式:
策略模式的客户端需要知道选择哪个策略,状态模式客户端不关心具体状态,状态会自动转换。
不同状态下行为有差异,而且状态发生转换,使用状态模式。若某一类的某一行为有多种实现方式,使用策略模式。
策略模式的低级别实现:
//策略接口
public interface PaymentClassification {
public double caclulatePayment();
}
//实现类1
public class monthClassification implements PaymentClassification {
int monthRate;
int month;
public monthClassification(int rate,int h){
monthRate=rate;
month=h;
}
@Override
public double caclulatePayment() {
return month+monthRate;
}
}
//实现类2
public class HourlyClassification implements PaymentClassification{
int hourlyRate;
int hours;
public HourlyClassification(int rate,int h){
hourlyRate=rate;
hours=h;
}
@Override
public double caclulatePayment() {
return hourlyRate*hours;
}
}
public class Employee {
String name;
PaymentClassification pc;
public Employee(String s){
name=s;
}
//设置策略的方法
public void setPaymentClassfication(PaymentClassification paymentClassfication){
pc=paymentClassfication;
}
public void getPayment(){
System.out.println(name+": "+pc.caclulatePayment()+"工资");
}
}
public class testStraget {
public static void main(String[] args) {
Employee employee=new Employee("asd");
//设置具体的策略数值
HourlyClassification hc=new HourlyClassification(10,40);
monthClassification mc=new monthClassification(10,200);
employee.setPaymentClassfication(hc);
employee.getPayment();
}
}
类图:
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-9PxLo22X-1606316096931)(G:\研究生课程\第一学期\高级软件设计\photo\02 策略模式\image-20201125224745351.png)]](https://img-blog.csdnimg.cn/20201125225516268.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2pqeTE5OTcxMDIz,size_16,color_FFFFFF,t_70#pic_center)
策略模式的高级实现,配合工厂模式
//策略接口
public interface PaymentClassification {
public double caclulatePayment();
public String classificationName();
}
//实现类1
public class HourlyClassification implements PaymentClassification {
int hourlyRate;
int hours;
public HourlyClassification(int rate,int h){
hourlyRate=rate;
hours=h;
}
@Override
public double caclulatePayment() {
return hourlyRate*hours;
}
@Override
public String classificationName() {
return "小时工";
}
}
//实现类2
public class MonthClassification implements PaymentClassification {
int monthRate;
int month;
public MonthClassification(int rate, int h){
monthRate=rate;
month=h;
}
@Override
public double caclulatePayment() {
return month+monthRate;
}
@Override
public String classificationName() {
return "月收入人员";
}
}
//实现类3
public class NullClassification implements PaymentClassification {
@Override
public double caclulatePayment() {
return 0;
}
@Override
public String classificationName() {
return "非员工,输入策略错误";
}
}
public class Employee {
String name;
PaymentClassification pc;
public Employee(String s){
name=s;
}
//设置策略的方法
public void setPaymentClassfication(PaymentClassification paymentClassfication){
pc=paymentClassfication;
}
public void getPayment(){
System.out.println(name+": 级别为"+pc.classificationName()+" 获得工资为"+pc.caclulatePayment()+"工资");
}
}
//工厂模式
import java.util.HashMap;
public class ClassificationStrategyFactory {
private static HashMap<String,PaymentClassification> hm=new HashMap<>();
static{
hm.put("hour",new HourlyClassification(1,10));
hm.put("month",new MonthClassification(12,1));
}
private static PaymentClassification NULL_EMPLOYEE=new NullClassification();
//把它构造函数变私有不让外人访问
private ClassificationStrategyFactory(){
}
public static PaymentClassification getClassificationStrategy(String type){
PaymentClassification paymentClassification=hm.get(type);
return paymentClassification==null?NULL_EMPLOYEE:paymentClassification;
}
}
public class testStraget {
public static void main(String[] args) {
Employee employee = new Employee("帅哥");
String type = "hour";
employee.setPaymentClassfication(ClassificationStrategyFactory.getClassificationStrategy(type));
employee.getPayment();
}
}
类图
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-23GaBXue-1606316096934)(G:\研究生课程\第一学期\高级软件设计\photo\02 策略模式\image-20201125225243378.png)]](https://img-blog.csdnimg.cn/20201125225505146.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2pqeTE5OTcxMDIz,size_16,color_FFFFFF,t_70#pic_center)
版权声明:本文为jjy19971023原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。