美文网首页
设计模式之策略模式

设计模式之策略模式

作者: 不怕天黑_0819 | 来源:发表于2020-06-21 15:58 被阅读0次

    什么是策略模式

    策略模式是指定义一系列算法,将每个算法都封装起来,并且使他们之间可以相互替换。

    优点:遵循了开闭原则,扩展性良好。
    缺点:随着策略的增加,对外暴露越来越多。

    以生活中的例子来说,比如我们要出去旅游,选择性很多,可以选择骑车、开车、坐飞机、坐火车等,就可以使用策略模式,把每种出行作为一种策略封装起来,后面增加了新的交通方式了,如超级高铁、火箭等,就可以不需要改动原有的类,新增交通方式即可,这样也符合软件开发的开闭原则。策略模式实现代码如下:

    
    // 声明旅行 
    interface ITrip {
        void going();
    }
    class Bike implements ITrip {
        @Override
        public void going() {
            System.out.println("骑自行车");
        }
    }
    class Drive implements ITrip {
        @Override
        public void going() {
            System.out.println("开车");
        }
    }
    //定义出行类 
    class Trip {
        private ITrip trip;
    
        public Trip(ITrip trip) {
            this.trip = trip;
        }
    
        public void doTrip() {
            this.trip.going();
        }
    }
    / /执行方法 
    public class StrategyTest {
        public static void main(String[] args) {
            Trip trip = new Trip(new Bike());
            trip.doTrip();
        }
    }
    

    策略模式定义了一系列算法,并将每个算法封装起来,使他们可以相互替换,且算法的变化不会影响到使用算法的客户。需要设计一个接口,为一系列实现类提供统一的方法,多个实现类实现该接口,设计一个抽象类(可有可无,属于辅助类),提供辅助函数,关系图如下:


    图中ICalculator提供统一的方法,
    AbstractCalculator是辅助类,提供辅助方法,jdk8中interface提供了default方法可以代替这个辅助类。接下来,依次实现下每个类:

    //统一的接口
    public interface ICalculator {  
        public int calculate(String exp);  
    }  
    
    //辅助类
    public abstract class AbstractCalculator {  
          
        public int[] split(String exp,String opt){  
            String array[] = exp.split(opt);  
            int arrayInt[] = new int[2];  
            arrayInt[0] = Integer.parseInt(array[0]);  
            arrayInt[1] = Integer.parseInt(array[1]);  
            return arrayInt;  
        }  
    }  
    
    
    //三个实现类
    public class Plus extends AbstractCalculator implements ICalculator { 
      
        @Override  
        public int calculate(String exp) {  
            int arrayInt[] = split(exp,"\\+");  
            return arrayInt[0]+arrayInt[1];  
        }  
    }  
    
    public class Minus extends AbstractCalculator implements ICalculator {  
      
        @Override  
        public int calculate(String exp) {  
            int arrayInt[] = split(exp,"-");  
            return arrayInt[0]-arrayInt[1];  
        }  
      
    }  
    
    public class Multiply extends AbstractCalculator implements ICalculator {  
      
        @Override  
        public int calculate(String exp) {  
            int arrayInt[] = split(exp,"\\*");  
            return arrayInt[0]*arrayInt[1];  
        }  
    
    
    
    //测试类
    public class StrategyTest {  
      
        public static void main(String[] args) {  
            String exp = "2+8";  
            ICalculator cal = new Plus();  
            int result = cal.calculate(exp);  
            System.out.println(result);  
        }  
    }  
    
    
    

    策略模式的决定权在用户,系统本身提供不同算法的实现,新增或者删除算法,对各种算法做封装。因此,策略模式多用在算法决策系统中,外部用户只需要决定用哪个算法即可。

    相关文章

      网友评论

          本文标题:设计模式之策略模式

          本文链接:https://www.haomeiwen.com/subject/cjsoxktx.html