定义:一个类的行为或方法可以在运行时改变。
优点: 1、算法可以自由切换。 2、避免使用多重条件判断。 3、扩展性良好。
缺点: 1、策略类会增多。 2、所有策略类都需要对外暴露
方法一:
方法一:反射实现
package strategydesignpattern.strategydesginpatternmyself.pay; public interface Pay { void pay(String account,String payPassword); } package strategydesignpattern.strategydesginpatternmyself.payImpl; import strategydesignpattern.strategydesginpatternmyself.pay.Pay; public class WeChatPay implements Pay { @Override public void pay(String account, String payPassword) { System.out.println("微信支付成功!"); } } package strategydesignpattern.strategydesginpatternmyself.payImpl; import strategydesignpattern.strategydesginpatternmyself.pay.Pay; public class ZhifubaoPay implements Pay { @Override public void pay(String account, String payPassword) { System.out.println("支付宝支付成功!"); } } package strategydesignpattern.strategydesginpatternmyself.payEnum; public enum PayEnum { WECHATPAY("strategydesignpattern.strategydesginpatternmyself.payimpl.WeChatPay"), ZHIFUBAOPAY("strategydesignpattern.strategydesginpatternmyself.payimpl.ZhifubaoPay"); private String className; PayEnum(String className) { this.className = className; } public String getClassName() { return className; } } package strategydesignpattern.strategydesginpatternmyself.factory; import strategydesignpattern.strategydesginpatternmyself.pay.Pay; public class StrategyFactory { /** * 动态获取支付方式 * @param className * @return */ public static Pay getPayMethodByClassName(String className){ try { return (Pay) Class.forName(className).newInstance(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } return null; } } package strategydesignpattern.strategydesginpatternmyself.context; import strategydesignpattern.strategydesginpatternmyself.factory.StrategyFactory; import strategydesignpattern.strategydesginpatternmyself.pay.Pay; import strategydesignpattern.strategydesginpatternmyself.payEnum.PayEnum; public class PayContext { private Pay payMethod; public PayContext(PayEnum payMethod) { this.payMethod = StrategyFactory.getPayMethodByClassName(payMethod.getClassName()); } /** * 进行支付 * @param accout * @param payPassword */ public void pay(String accout,String payPassword){ payMethod.pay(accout,payPassword); } } package strategydesignpattern.strategydesginpatternmyself; import strategydesignpattern.strategydesginpatternmyself.context.PayContext; import strategydesignpattern.strategydesginpatternmyself.payEnum.PayEnum; public class StrategyPayTest { public static void main(String[] args) { //微信支付测试 PayContext contextWeChat = new PayContext(PayEnum.WECHATPAY); contextWeChat.pay("flyFish","123456"); //支付宝支付 PayContext contextZFB = new PayContext(PayEnum.ZHIFUBAOPAY); contextZFB.pay("guessMe","111111"); } }
方法二:使用spring通过上下文动态获取
package strategydesignpattern.strategydesginpatternspringboot.pay; public interface Pay { void pay(String account,String payPassword); } package strategydesignpattern.strategydesginpatternspringboot.payImpl; import org.springframework.stereotype.Component; import strategydesignpattern.strategydesginpatternspringboot.pay.Pay; @Component public class WeChatPay implements Pay { @Override public void pay(String account, String payPassword) { System.out.println("微信支付成功!"); } } package strategydesignpattern.strategydesginpatternspringboot.payImpl; import org.springframework.stereotype.Component; import strategydesignpattern.strategydesginpatternspringboot.pay.Pay; @Component public class ZhifubaoPay implements Pay { @Override public void pay(String account, String payPassword) { System.out.println("支付宝支付成功!"); } } package strategydesignpattern.strategydesginpatternspringboot.payenum; public enum PayEnum { WECHATPAY("weChatPay"),ZHIFUBAOPAY("zhifubaoPay"); private String className; PayEnum(String className) { this.className = className; } public String getClassName() { return className; } } package strategydesignpattern.strategydesginpatternspringboot.context; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.stereotype.Component; import strategydesignpattern.strategydesginpatternspringboot.payenum.PayEnum; import strategydesignpattern.strategydesginpatternspringboot.pay.Pay; @Component public class Context implements ApplicationContextAware { private ApplicationContext applicationContext; @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.applicationContext = applicationContext; } /** * 获取applicationContext * @return */ public ApplicationContext getApplicationContext() { return applicationContext; } /** * 进行支付 * */ public Pay pay(PayEnum payEnum){ return (Pay) getBean(payEnum.getClassName()); } /** * 通过name获取 Bean. * @param name * @return */ public Object getBean(String name){ return getApplicationContext().getBean(name); } /** * 通过class获取Bean. * @param clazz * @param <T> * @return */ public <T> T getBean(Class<T> clazz){ return getApplicationContext().getBean(clazz); } /** * 通过name,以及Clazz返回指定的Bean * @param name * @param clazz * @param <T> * @return */ public <T> T getBean(String name,Class<T> clazz){ return getApplicationContext().getBean(name, clazz); } } package strategydesignpattern.strategydesginpatternspringboot.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import strategydesignpattern.strategydesginpatternspringboot.context.Context; import strategydesignpattern.strategydesginpatternspringboot.payenum.PayEnum; @RestController public class StrategyPayController { @Autowired private Context context; @RequestMapping("/pay") @ResponseBody public String pay(){ //微信支付 context.pay(PayEnum.WECHATPAY).pay("flyFish","123456"); //支付宝支付 context.pay(PayEnum.ZHIFUBAOPAY).pay("guessMe","111111"); return "success"; } }