您好,欢迎访问一九零五行业门户网

Spring与策略模式

一:策略模式的定义
策略模式是对算法的包装,把使用算法的责任和算法本身分隔开,委派给不同的对象管理。策略模式通常把一系列的算法包装到一系列的策略类里面,作为一个抽象策略类的子类。
其类图如下:
如果是要用java类来实现的策略模式,其源代码如下:
java代码  
/** * * 策略执行 * @author weique.lqf * @version $id: context.java, v 0.1 2014-2-9 下午2:32:56 weique.lqf exp $ */ public class context { private strategy stg; public context(strategy thestg) { this.stg = thestg; } public void doaction() { this.stg.teststrategy(); } }
策略接口:
java代码 /** * * * @author weique.lqf * @version $id: strategy.java, v 0.1 2014-2-9 下午2:32:17 weique.lqf exp $ */ public interface strategy { void teststrategy(); }
实现类一:
java代码
package com.proxy.strategy.impl; import com.proxy.strategy.strategy; public class printstrategy implements strategy { public void teststrategy() { system.out.print(我要打印!!); } }
实现类二:
java代码
package com.proxy.strategy.impl; import com.proxy.strategy.strategy; public class writestrategy implements strategy { public void teststrategy() { system.out.println(我要写字!!!); } }
执行代码:
java代码
package com.proxy.strategy; import com.proxy.strategy.impl.printstrategy; public class strategyclient { public static void main(string[] args) { strategy stga = new printstrategy(); context ct = new context(stga); ct.doaction(); } }
二:spring实现策略模式
现在使用spring的系统可以说是多如牛毛,那么如何在spring模式下实现策略呢?
其实只需要稍微改造下就可以了,因为spring的核心之一就是ioc。
首先修改contex类:
java代码
package com.proxy.strategy; public class contextspring { private strategy stg; /** * setter method for property stg. * * @param stg value to be assigned to property stg */ public void setstg(strategy stg) { this.stg = stg; } public void doaction() { this.stg.teststrategy(); } }
然后在spring配置文件里面配置,
xml代码

里面选择你将要注入的实现类,然后在执行的代码里面写这样:
java代码
package com.proxy.strategy; import org.springframework.context.applicationcontext; import org.springframework.context.support.classpathxmlapplicationcontext; public class strategyspringclient { public static void main(string[] args) { applicationcontext context = new classpathxmlapplicationcontext(beans.xml); contextspring ct = (contextspring) context.getbean(ct); ct.doaction(); } }
看,这样就将spring引入了。
但是这样有好处也有坏处,如果我要根据不同的类型,比如说:合同是需要打印的,而情书是需要手写的。假设合同为类型2,情书为类型1,那我们怎么来自动适配?
三:高级版的spring策略模式
首先要修改context类:
java代码
package com.proxy.strategy; import java.util.hashmap; import java.util.map; /** * * * @author weique.lqf * @version $id: contextspringfactory.java, v 0.1 2014-2-9 下午3:46:09 weique.lqf exp $ */ public class contextspringfactory { private map stgmap = new hashmap(); /** * getter method for property stgmap. * * @return property value of stgmap */ public map getstgmap() { return stgmap; } /** * setter method for property stgmap. * * @param stgmap value to be assigned to property stgmap */ public void setstgmap(map stgmap) { this.stgmap = stgmap; } public void doaction(string strtype) { this.stgmap.get(strtype).teststrategy(); } }
然后修改spring的配置文件:
xml代码

执行的入口类修改为:
java代码
package com.proxy.strategy; import org.springframework.context.applicationcontext; import org.springframework.context.support.classpathxmlapplicationcontext; public class strategyspringclientfactory { public static void main(string[] args) { //外部参数 string type = 1; applicationcontext context = new classpathxmlapplicationcontext(beans.xml); contextspringfactory ctf = (contextspringfactory) context.getbean(ctf); ctf.doaction(type); //type 2 type = 2; ctf.doaction(type); } }
然后运行下,看看会有什么结果?
其它类似信息

推荐信息