使用责任链模式进行类型转换
public class Test {
public static void main(String[] args) {
Date d = new Date();
Double d2 = new Double("99.145456");
String str = null;
FormaterChain fc = FormaterChain.getInstance();
fc.add(new DateFormater());
fc.add(new DecimalFormater());
//转换日期
str = fc.doFormat(d);
System.out.println(str);
//保留小数2位
str = fc.doFormat(d2);
System.out.println(str);
}
}
//对象转字符串
public interface Formater {
String format(Object o, FormaterChain chain);
}
//单例、责任链
public class FormaterChain {
private List<Formater> formaters = new LinkedList<Formater>();
private Iterator<Formater> chain = null;
private FormaterChain(){}
private static class Inner{
private static FormaterChain formaterChain = new FormaterChain();
}
public static FormaterChain getInstance(){
return Inner.formaterChain;
}
public String doFormat(Object o){
if(o == null){
return null;
}
if(chain.hasNext()){
Formater f = chain.next();
return f.format(o, this);
}
return o.toString();
}
//增加类型转换器
public void add(Formater f){
formaters.add(f);
chain = formaters.iterator();
}
}
//日期转换器-转换为字符串
public class DateFormater implements Formater {
public String format(Object o, FormaterChain chain) {
if(o.getClass().equals(java.util.Date.class)){
return date2str((java.util.Date)o);
}
return chain.doFormat(o);
}
private String date2str(Date d){
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
return df.format(d);
}
}
public class DecimalFormater implements Formater {
public String format(Object o, FormaterChain chain) {
if(o.getClass().equals(java.lang.Double.class)){
return double2str((Double)o);
}
return chain.doFormat(o);
}
private String double2str(double d){
DecimalFormat df = new DecimalFormat("0.00");
df.setRoundingMode(RoundingMode.HALF_UP);
return df.format(d);
}
}
相关推荐
设计模式之责任链模式源码 设计模式之责任链模式源码 设计模式之责任链模式源码
设计模式之责任链模式Java版本实现和UML类图设计
java设计模式之责任链模式,从实际生活入手说明责任链模式的原理。
【设计模式】之责任链模式责任链模式概念责任链,顾名思义,就是用来处理相关事务责任的一条执行链,执行链上有多个节点,每个节点都有机会(条件匹配)处理请求事务,如果
一、责任链模式 现有 “战士”、“班长”、“排长”、“连长”,“营长”五种角色,当有人要请假时要进行以下处理 1.只能是下级象上级请假(如“排长”只能向“连长请假”) 2.班长可以批准1天内的假期,排长批5天,...
JAVA 设计模式: 工厂模式 代理模式 迭代模式 责任链模式 源码
这个资源就是让你掌握责任链模式的原理和运用,你值得拥有,好好学习,天天向上。
ios平台中通过最简单的代码讲解责任链模式,可在博客http://blog.sina.com.cn/s/blog_161d504630102wxis.html中查看简单解释
责任链模式(Chain) 用意:避免请求发送者与接受者耦合在一起,让多个对象连接成一条链,并且沿关这条链传递请求,直至有对象处理他为止。
23种设计模式之责任链模式对责任链模式进行了简单的介绍,并附带了例子程序,和大家分享
设计模式C++学习之责任链模式(Chain of Responsibility)
设计模式的责任链模式的例子,希望对大家有用~~~~~~~~
java常用设计模式-责任链模式
开始击鼓时,花束就开始依次传递,鼓声一落,如果花束在某人手中,则该人就得饮酒,这是很典型的行为责任链模式。 State模式主要解决的是在开发中时常遇到的根据不同的状态需要进行不同的处理操作的问题,而这样的...
设计模式(Design pattern)代表了最佳的实践,通常被有经验的面向对象的软件开发人员所采用。设计模式是软件开发人员在软件开发过程中面临的一般问题的解决方案。这些解决方案是众多软件开发人员经过相当长的一段...
2 责任链模式 3 策略模式 4 模板方法模式 5 工厂方法模式 6 抽象工厂模式 7 建造者模式 8 代理模式 9 装饰模式 10 原型模式 11 委派模式 12 适配器模式 设计模式综合运用 1 门面+模版方法+责任链+策略 2 门面+模版...
c++设计模式-行为型模式-责任链模式;qt工程;c++简单源码; 责任链(Chain of Responsibility)模式的定义:为了避免请求发送者与多个请求处理者耦合在一起,于是将所有请求的处理者通过前一对象记住其下一个对象的...
设计模式之责任链模式,使用形象化例子来解释说明。
压缩包里是我自己写的管道模式、单例模式的代码;你需要copy到你自己eclipse建的工程文件中。另外,我把我的代码说明,写在了readme.txt文件里,偷懒以及求快的人看看...如果有问题,可留言。另外我分数高,但绝对有...