1. 程式人生 > >策略模式---------簡單分析

策略模式---------簡單分析

cls img 不同 簡單的 方法 @override cat raw ima

  繼續我的設計模式之旅,這次學習的是策略模式,自己感覺策略模式跟簡單工廠模式好像,引用大話設計模式裏面的定義,策略模式是一種定義一系列算法的方法,從概念上看,所有這些算法完成的都是相同的工作,只是實現不同,他可以以相同的方法調用所有的算法,減少各種算法類與使用類之間的耦合度。

  例子中融合了簡單工廠模式以及反射的知識點

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)); 但是想實現更大的耦合,擅自使用了反射,對反射不是很熟悉,也是在摸索階段,繼續加油

策略模式---------簡單分析