一、单例模式
/**
* @author
* @version $1.0, 2011-1-30
* @since JDK6
*/
public class JackSonUtils
{
/**以 singleton的形式取出ObjectMapper.
* 供后面的程序重用。
* @return
*/
public static ObjectMapper getObjectMapper ( )
{
return Holer.mapper;
}
private static class Holer
{
private static ObjectMapper mapper = getInstance ( );
private static ObjectMapper getInstance ( )
{
ObjectMapper mapper = new ObjectMapper ( );
//初始化ObjectMapper......
mapper.setDateFormat ( new SimpleDateFormat ( "yyyy-MM-dd HH:mm:ss" ) );
mapper.configure ( JsonParser.Feature.ALLOW_SINGLE_QUOTES ,
true );
//设置是否格式化
mapper.configure ( SerializationFeature.INDENT_OUTPUT ,
true );
mapper.configure ( SerializationFeature.WRAP_ROOT_VALUE ,
false );
//数字加双引号
mapper.configure ( JsonGenerator.Feature.WRITE_NUMBERS_AS_STRINGS,
true );
mapper.getSerializerProvider ( ).setNullValueSerializer ( new JsonSerializer<Object>(){
@Override
public void serialize ( Object arg0 ,
JsonGenerator jgen ,
SerializerProvider arg2 ) throws IOException , JsonProcessingException
{
jgen.writeString ( "" );
}
} );
mapper.setVisibility ( PropertyAccessor.FIELD ,
JsonAutoDetect.Visibility.ANY ) // auto-detect all member fields
.setVisibility ( PropertyAccessor.GETTER ,
JsonAutoDetect.Visibility.NONE )
// but only public getters
.setVisibility ( PropertyAccessor.IS_GETTER ,
JsonAutoDetect.Visibility.NONE ); // and none of "is-setters"
return mapper;
}
}
}
二、观察者模式
Spring 的事件监听机制
三、适配器模式、桥梁模式
JDBC
持续更新