享元模式设计心得
来源:互联网 发布:ant打包java配置文件 编辑:程序博客网 时间:2024/06/16 09:35
![享元模式设计心得 - 断鸿零雁 - 断情阁 享元模式设计心得 - 断鸿零雁 - 断情阁](http://img1.ph.126.net/lpEfxCEIMIbqXt5ajc0V8w==/6630134280327423646.png)
IBizHandler 实现类:/*
* 版权信息:北京汉方三陆零科技有限公司</br>
* Copyright ?2014-2015. All rights reserved. 京ICP备15000045号
*/
package com.hf.util.flyweight;
/****************************************************************************
* com.hf.util.flyweight IBizHandler.java Created on 2015年5月20日
* @Author: linfenliang
* @Description:
* @Version: 1.0
***************************************************************************/
public interface IBizHandler {
/**
* 业务处理
*
* @param value,原始数据串
* @return
* boolean
* @Date 2015年5月20日
* @Version 1.0
* @Author linfenliang
*/
public boolean doBusiness(String value);
/**
* 下发数据处理结果
*
* @param result
* void
* @Date 2015年5月20日
* @Version 1.0
* @Author linfenliang
*/
public void downStreamHandler(boolean result);
}
IBizHandler 实现类:/*
* 版权信息:北京汉方三陆零科技有限公司</br>
* Copyright ?2014-2015. All rights reserved. 京ICP备15000045号
*/
package com.hf.util.flyweight;
/****************************************************************************
* com.hf.util.flyweight LoginBizHandler.java Created on 2015年5月20日
* @Author: linfenliang
* @Description:
* @Version: 1.0
***************************************************************************/
public class LoginBizHandler implements IBizHandler {
private String data;
public LoginBizHandler(String data) {
this.data = data;
}
@Override
public boolean doBusiness(String value) {
System.out.println("login bussiness handler:"+data);
return false;
}
@Override
public void downStreamHandler(boolean result) {
System.out.println("Login bussiness downstream:"+data);
}
}
工厂类:/*
* 版权信息:北京汉方三陆零科技有限公司</br>
* Copyright ?2014-2015. All rights reserved. 京ICP备15000045号
*/
package com.hf.util.flyweight;
/****************************************************************************
* com.hf.util.flyweight RegisterBizHandler.java Created on 2015年5月20日
* @Author: linfenliang
* @Description:
* @Version: 1.0
***************************************************************************/
public class RegisterBizHandler implements IBizHandler {
private String data;
public RegisterBizHandler(String data) {
this.data = data;
}
@Override
public boolean doBusiness(String value) {
System.out.println("register bussiness handler:"+data);
return false;
}
@Override
public void downStreamHandler(boolean result) {
System.out.println("register business downstream:"+data);
}
}
/*
* 版权信息:北京汉方三陆零科技有限公司</br>
* Copyright ?2014-2015. All rights reserved. 京ICP备15000045号
*/
package com.hf.util.flyweight;
import java.util.HashMap;
import java.util.Map;
/****************************************************************************
* com.hf.util.flyweight FlyWeightFactory.java Created on 2015年5月20日
* @Author: linfenliang
* @Description:享元模式设计实例,该模式即为一个可复用的缓存机制,与之前车联网项目中,
* 不同业务逻辑走不同的handler相同,如果该handler未初始化,则初始化后加载到Map中,
* 下次可直接调用,其中的factory可以看做是一个业务处理缓存器
* @Version: 1.0
***************************************************************************/
public class FlyWeightFactory {
private Map<String,IBizHandler> cacheMap = new HashMap<String, IBizHandler>();
public IBizHandler getBizHandler(String data){
IBizHandler handler = cacheMap.get(data);
if(handler==null){
if("login".equals(data)){
handler = new LoginBizHandler(data);
}else{
handler = new RegisterBizHandler(data);
}
cacheMap.put(data, handler);
}
return handler;
}
public static void main(String[] args) {
FlyWeightFactory factory = new FlyWeightFactory();
for(int i=0;i<1000;i++){
IBizHandler handler = factory.getBizHandler("");
boolean result = handler.doBusiness("{}");
handler.downStreamHandler(result);
}
}
}
- 享元模式设计心得
- 设计模式-享元
- 享元设计模式
- [设计]享元模式
- 设计模式-享元设计
- 设计模式---享元模式
- 设计模式 享元模式
- 设计模式 - 享元模式
- 设计模式:享元模式
- 设计模式 - 享元模式
- 设计模式-享元模式
- 设计模式---------享元模式
- 设计模式:享元模式
- 设计模式--享元模式
- 设计模式-享元模式
- 设计模式:享元模式
- 设计模式-享元模式
- 设计模式--享元模式
- maven在eclipse中自动化远程发布
- jQuery Mobile页面跳转后未加载JS解决办法
- 机器学习--Hoeffding Inequality--界定概率边界
- TextView加载html代码显示图片,并且支持图片放大功能
- 单例模式的另一种实现(内部类)
- 享元模式设计心得
- linux安装JDK1.8、tomcat8、Maven3.2、Scala2.10、Spark1.3.0
- org.apache.catalina.core.DefaultInstanceManager cannot be cast to org.apache.tomcat.InstanceManager
- 四天学会mongoDB (第四天 索引操作)
- 观察者模式(发布订阅模式)
- hive异常 java.lang.IllegalArgumentException: java.net.URISyntaxException: Relative path in absolute URI: ${system:java.io.tmpdir%7
- hive异常:创建MySQL时Specified key was too long; max key length is 1000 bytes
- 页脚永远至于页面底部的js
- hive的安装使用总结