策略模式---------简单分析

  继续我的设计模式之旅,这次学习的是策略模式,自己感觉策略模式跟简单工厂模式好像,引用大话设计模式里面的定义,策略模式是一种定义一系列算法的方法,从概念上看,所有这些算法完成的都是相同的工作,只是实现不同,他可以以相同的方法调用所有的算法,减少各种算法类与使用类之间的耦合度。

  例子中融合了简单工厂模式以及反射的知识点

Strategy,公共的接口,定义算法统一的方法

package com.wqq.design.strategy;
/** 
 * @author wangqq 
 * @version 创建时间:2018年8月6日 下午2:28:01 
 * 策略模式,定义所有支持算法的公共接口 
 * 
 * 工厂模式是返回子类,策略模式返回子类的方法
 */
public interface Strategy {
    
    void AlgorithmInterface();

}

AddStrategy,一种算法实现

package com.wqq.design.strategy;
/** 
 * @author wangqq 
 * @version 创建时间:2018年8月6日 下午2:36:22 
 * 类说明 
 */
public class AddStrategy implements Strategy {
    
    private Double math ;
    
    public AddStrategy() {
        // TODO Auto-generated constructor stub
    }
    
    public AddStrategy(Double math) {
        this.math = math;
    }
    
    @Override
    public void AlgorithmInterface() {
        // TODO Auto-generated method stub
        System.out.println(math);
    }

}

SubstractStrategy 另一种算法实现

package com.wqq.design.strategy;
/** 
 * @author wangqq 
 * @version 创建时间:2018年8月6日 下午2:37:08 
 * 类说明 
 */
public class SubstractStrategy implements Strategy {
    
    private String args ;
    private Double b;
    
    public SubstractStrategy() {
        // TODO Auto-generated constructor stub
    }
    
    public SubstractStrategy(String args,Double b) {
        this.args = args ;
        this.b = b;
    }
    
    @Override
    public void AlgorithmInterface() {
        // TODO Auto-generated method stub
        System.out.println(args+b);
    }

}

Context 外界能用使用的类

package com.wqq.design.strategy;

import java.lang.reflect.Constructor;

/** 
 * @author wangqq 
 * @version 创建时间:2018年8月6日 下午2:37:50 
 * 类说明 
 * @param <T>
 */
public class Context<T> {
    private Strategy strategy ;
    
//当前包名
private String path = Context.class.getPackage().getName(); public Context (Strategy strategy){ this.strategy = strategy ; } public Context (String className) throws Exception{ className = path+"."+className; this.strategy = (Strategy) Class.forName(className).newInstance() ; } @SuppressWarnings({ "unchecked", "rawtypes" }) public Context(String className , Object[] paramValue,Class[] paramType) throws Exception{ className = path+"."+className; if (null == paramValue){ throw new Exception("参数值异常"); } if (paramValue.length != paramType.length){ throw new Exception("参数值与类型数量不符"); } Class<T> cls = (Class<T>) Class.forName(className); Constructor<T> constructor = cls.getConstructor(paramType); this.strategy = (Strategy) constructor.newInstance(paramValue); } public void ContextInterfacr (){ strategy.AlgorithmInterface(); } }

测试类

package com.wqq.design.strategy;

/** 
 * @author wangqq 
 * @version 创建时间:2018年8月6日 下午3:09:15 
 * 类说明 
 */
public class TestStrategy {
    
    @SuppressWarnings({ "rawtypes" })
    public static void  main(String[] args) {
        try {
            
            String str = "SubstractStrategy";
            Context context = new Context(str,new Object[]{
                    "测试呀"    ,12.0
                }, new Class[]{
                    String.class,Double.class
                });
            
            context.ContextInterfacr();
            
            
            context = new Context("AddStrategy");
            context.ContextInterfacr();
        } catch (Exception e) {
            // TODO: handle exception
        }
        
    }
    
}

结果:

测试类其实可以使用简单的 Context context = new Context (new SubstractStrategy("测试呀",12.0)); 但是想实现更大的耦合,擅自使用了反射,对反射不是很熟悉,也是在摸索阶段,继续加油

猜你喜欢

转载自www.cnblogs.com/Cassie-wang/p/9448619.html