Spring与策略模式
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 <tt>stg</tt>. * * @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<String, Strategy> stgMap = new HashMap<String, Strategy>();
/**
- Getter method for property stgMap.
- @return property value of stgMap
*/
public Map<String, Strategy> getStgMap() {
return stgMap;
}
/**
- Setter method for property stgMap.
- @param stgMap value to be assigned to property stgMap
*/
public void setStgMap(Map<String, Strategy> 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);
}
}
Spring与策略模式相关推荐
- 如何使用 Spring 实现策略模式+工厂模式
欢迎关注方志朋的博客,回复"666"获面试宝典 一.策略模式 策略模式定义了一组算法,将每个算法都封装起来,并且使它们之间可以互换 1.策略模式主要角色 主要角色如下: 封装角色( ...
- spring AOP策略模式使用
1.策略模式 The Strategy Pattern defines a family of algorithms,encapsulates each one,and makes them inte ...
- 原来使用 Spring 实现策略模式可以这么简单!
策略模式作为一种软件设计模式,指对象有某个行为,但是在不同的场景中,该行为有不同的实现算法,可以替代代码中大量的 if-else. 比如我们生活中的场景:买东西结账可以使用微信支付.支付宝支付或者银行 ...
- 策略模式及Spring整合策略模式
策略模式 抽象策略类 interface SortService{int[] sort(int arr[]);} 具体策略类 class InsertionSortServiceImpl implem ...
- Spring实现策略模式
通过Spring实现策略模式 当程序中使用太多的if/else/switch来处理不同类型的业务时,会变得极难维护,通过策略模式可以更容易的实现业务扩展和维护. 标准策略模式介绍 比如说对象的某个行为 ...
- Spring 中策略模式的 2 个经典应用
点击蓝色"程序猿DD"关注我哟 加个"星标",不忘签到哦 转自头条号程序汪汪 背景 程序员在项目实战中,策略模式用的非常多. 学习目标 会在Spring项目中运 ...
- Spring 中策略模式的 2 个经典应用,可以用来怼面试官了
一.背景 程序员在项目实战中,策略模式用的非常多. 二.学习目标 会在Spring项目中运用策略模式 三.代码例子 废话不多说,Java的软件开发们注意啦,开车啦! 下面是一个查询业务使用策略模式的案 ...
- Spring中策略模式实现方法
一.定义 在策略模式(Strategy Pattern)中,一个类的行为或其算法可以在运行时更改.这种类型的设计模式属于行为型模式.在策略模式中,我们创建表示各种策略的对象和一个行为随着策略对象改变而 ...
- java spring 实现策略,Spring 环境下实现策略模式的示例
背景 最近在忙一个需求,大致就是给满足特定条件的用户发营销邮件,但是用户的来源有很多方式:从 ES 查询的.从 csv 导入的.从 MongoDB 查询-.. 需求很简单,但是怎么写的优雅,方便后续扩 ...
- 策略模式、策略模式与Spring的碰撞
策略模式是GoF23种设计模式中比较简单的了,也是常用的设计模式之一,今天我们就来看看策略模式. 实际案例 我工作第三年的时候,重构旅游路线的机票查询模块,旅游路线分为四种情况: 如果A地-B地往返都 ...
最新文章
- Nginx 搭建负载均衡
- linux环境下安装gcc
- android在特定时间,如何在Android Oreo的特定时间在Android上发出通知?
- php生成文章页,php结合smarty生成静态页面php文章内分页代码
- 深度优先搜索——自然数的拆分问题(洛谷 P2404)
- web端项目管理/工程项目劳务资源管理系统/考勤审批/人员招聘/企业管理系统/工资管理/入职管理/组织结构/财务管理/大数据指挥中心/劳务系统/岗位工种/智慧工程监管/劳务app原型/axure原型
- 当IDENTITY_INSERT设置为OFF时不能向表插入显示值。(源:MSSQLServer,错误码:544)
- 设置win10有线网络连接
- 数字证书相关的知识点
- 题目:输入一个字符串,输出该字符串中字符的所有组合。举个例子,如果输入abc,它的组合有a、b、c、ab、ac、bc、abc。
- 什么是工业物联网网关?
- 基于 8051单片机的线跟随小车
- python+adb命令实现自动刷视频脚本
- 运营商大数据丨电销行业如何高效获得精准客户
- HADOOP 伪分布式集群搭建
- 微信小程序上下滑动卡顿 z-index设置不起作用
- 11、Microsoft Visual Studio 2022 Installer Projects踩坑一
- 安卓判断APP是在前台还是在后台
- 华硕K55VD安装ubuntu 18.04
- 第000篇 - 一步一步了解区块链技术