黑马程序员--Spring与策略模式

来源:互联网 发布:手机链接下载软件 编辑:程序博客网 时间:2024/05/29 15:21

---------------------- <a href="http://www.itheima.com"target="blank">ASP.Net+Unity开发</a><a href="http://www.itheima.com"target="blank">.Net培训</a>、期待与您交流! ----------------------

一:策略模式的定义

策略模式是对算法的包装,把使用算法的责任和算法本身分隔开,委派给不同的对象管理。策略模式通常把一系列的算法包装到一系列的策略类里面,作为一个抽象策略类的子类。

其类图如下:

 

 

 

如果是要用JAVA类来实现的策略模式,其源代码如下:

Java代码  收藏代码
  1. /** 
  2.  * 
  3.  * 策略执行 
  4.  * @author weique.lqf 
  5.  * @version $Id: Context.java, v 0.1 2014-2-9 下午2:32:56 weique.lqf Exp $ 
  6.  */  
  7. public class Context {  
  8.     private Strategy stg;  
  9.    
  10.     public Context(Strategy theStg) {  
  11.         this.stg = theStg;  
  12.     }  
  13.    
  14.     public void doAction() {  
  15.         this.stg.testStrategy();  
  16.     }  
  17. }  

 

 

策略接口:

Java代码  收藏代码
  1. /** 
  2.  * 
  3.  * 
  4.  * @author weique.lqf 
  5.  * @version $Id: Strategy.java, v 0.1 2014-2-9 下午2:32:17 weique.lqf Exp $ 
  6.  */  
  7. public interface Strategy {  
  8.    
  9.     void testStrategy();  
  10. }  

 

实现类一:

Java代码  收藏代码
  1. package com.proxy.strategy.impl;  
  2.    
  3. import com.proxy.strategy.Strategy;  
  4.    
  5. public class PrintStrategy implements Strategy {  
  6.    
  7.     public void testStrategy() {  
  8.         System.out.print("我要打印!!");  
  9.     }  
  10.    
  11. }  

 

实现类二:

Java代码  收藏代码
  1. package com.proxy.strategy.impl;  
  2.    
  3. import com.proxy.strategy.Strategy;  
  4.    
  5. public class WriteStrategy implements Strategy {  
  6.    
  7.     public void testStrategy() {  
  8.         System.out.println("我要写字!!!");  
  9.     }  
  10.    
  11. }  

 

执行代码:

Java代码  收藏代码
  1. package com.proxy.strategy;  
  2.    
  3. import com.proxy.strategy.impl.PrintStrategy;  
  4.    
  5. public class StrategyClient {  
  6.    
  7.     public static void main(String[] args) {  
  8.         Strategy stgA = new PrintStrategy();  
  9.         Context ct = new Context(stgA);  
  10.         ct.doAction();  
  11.     }  
  12. }  

 

二:spring实现策略模式

         现在使用spring的系统可以说是多如牛毛,那么如何在spring模式下实现策略呢?

其实只需要稍微改造下就可以了,因为spring的核心之一就是IOC。

首先修改Contex类:

Java代码  收藏代码
  1. package com.proxy.strategy;  
  2.    
  3. public class ContextSpring {  
  4.     private Strategy stg;  
  5.    
  6.     /** 
  7.      * Setter method for property <tt>stg</tt>. 
  8.      * 
  9.      * @param stg value to be assigned to property stg 
  10.      */  
  11.     public void setStg(Strategy stg) {  
  12.         this.stg = stg;  
  13.     }  
  14.    
  15.     public void doAction() {  
  16.         this.stg.testStrategy();  
  17.     }  
  18. }  

 

然后在spring配置文件里面配置,

   

Xml代码  收藏代码
  1. <bean id="ct" class = "com.proxy.strategy.ContextSpring">  
  2.       <property name="stg" ref="writeStg"/>  
  3.    </bean>  
  4.    <bean id="writeStg" class = "com.proxy.strategy.impl.WriteStrategy"/>  
  5.    <bean id="printStg" class = "com.proxy.strategy.impl.PrintStrategy"/>  

 

里面选择你将要注入的实现类,然后在执行的代码里面写这样:

Java代码  收藏代码
  1. package com.proxy.strategy;  
  2.    
  3. import org.springframework.context.ApplicationContext;  
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  5.    
  6. public class StrategySpringClient {  
  7.    
  8.     public static void main(String[] args) {  
  9.         ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");  
  10.         ContextSpring ct = (ContextSpring) context.getBean("ct");  
  11.         ct.doAction();  
  12.     }  
  13.    
  14. }  

 

看,这样就将spring引入了。

但是这样有好处也有坏处,如果我要根据不同的类型,比如说:合同是需要打印的,而情书是需要手写的。假设合同为类型2,情书为类型1,那我们怎么来自动适配?

三:高级版的spring策略模式

首先要修改Context类:

Java代码  收藏代码
  1. package com.proxy.strategy;  
  2.    
  3. import java.util.HashMap;  
  4. import java.util.Map;  
  5.    
  6. /** 
  7.  * 
  8.  * 
  9.  * @author weique.lqf 
  10.  * @version $Id: ContextSpringFactory.java, v 0.1 2014-2-9 下午3:46:09 weique.lqf Exp $ 
  11.  */  
  12. public class ContextSpringFactory {  
  13.    
  14.     private Map<String, Strategy> stgMap = new HashMap<String, Strategy>();  
  15.    
  16.     /** 
  17.      * Getter method for property <tt>stgMap</tt>. 
  18.      * 
  19.      * @return property value of stgMap 
  20.      */  
  21.     public Map<String, Strategy> getStgMap() {  
  22.         return stgMap;  
  23.     }  
  24.    
  25.     /** 
  26.      * Setter method for property <tt>stgMap</tt>. 
  27.      * 
  28.      * @param stgMap value to be assigned to property stgMap 
  29.      */  
  30.     public void setStgMap(Map<String, Strategy> stgMap) {  
  31.         this.stgMap = stgMap;  
  32.     }  
  33.    
  34.     public void doAction(String strType) {  
  35.         this.stgMap.get(strType).testStrategy();  
  36.     }  
  37. }  

 

然后修改spring的配置文件:

   

Xml代码  收藏代码
  1. <bean id="ctf" class = "com.proxy.strategy.ContextSpringFactory">  
  2.       <property name="stgMap">   
  3.          <map>   
  4.               <entry key="1" value-ref="writeStg"/>   
  5.               <entry key="2" value-ref="printStg"/>    
  6.          </map>   
  7.       </property>   
  8.    </bean>  

 

执行的入口类修改为:

Java代码  收藏代码
  1. package com.proxy.strategy;  
  2.    
  3. import org.springframework.context.ApplicationContext;  
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  5.    
  6. public class StrategySpringClientFactory {  
  7.     public static void main(String[] args) {  
  8.         //外部参数  
  9.         String type = "1";  
  10.         ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");  
  11.         ContextSpringFactory ctf = (ContextSpringFactory) context.getBean("ctf");  
  12.         ctf.doAction(type);  
  13.         //type 2  
  14.         type = "2";  
  15.         ctf.doAction(type);  
  16.     }  
  17. }  

  

然后运行下,看看会有什么结果?

---------------------- <a href="http://www.itheima.com"target="blank">ASP.Net+Unity开发</a><a href="http://www.itheima.com"target="blank">.Net培训</a>、期待与您交流! ----------------------


0 0