设计模式之静态工厂的使用
来源:互联网 发布:武汉工程大学 知乎 编辑:程序博客网 时间:2024/05/20 02:21
静态工厂类
package com.pbh.filter;import com.alibaba.fastjson.serializer.PropertyFilter;/** * * Description: 过滤序列化属性静态工厂类 * @author PanBaihui * @time 2017年9月19日 下午2:14:36 */public class PropertyFilterFactory { /** * * @Description:懒加载对象过滤 * @author PanBaihui * @time 2017年9月19日 下午5:47:44 * @return PropertyFilter */ public static PropertyFilter SimplePropertyFilter() { return new SimplePropertyFilter(); } /** * * @Description:代理对象过滤 * @author PanBaihui * @time 2017年9月19日 下午2:20:06 * @return PropertyFilter */ public static PropertyFilter CarSubBrandSimplePropertyFilter() { return new CarSubBrandSimplePropertyFilter(); } /** * * @Description:车型过滤 * @author PanBaihui * @time 2017年9月19日 下午2:19:50 * @return PropertyFilter */ public static PropertyFilter CarTypeSimplePropertyFilter() { return new CarTypeSimplePropertyFilter(); }}
接口实现类
package com.pbh.filter;import org.hibernate.collection.spi.PersistentCollection;import org.hibernate.proxy.HibernateProxy;import org.hibernate.proxy.LazyInitializer;import com.alibaba.fastjson.serializer.PropertyFilter;/** * * Description: 序列化时忽略代理的懒加载对象 * @author PanBaihui * @time 2017年9月15日 下午4:15:45 */public class SimplePropertyFilter implements PropertyFilter { @Override public boolean apply(Object object, String name, Object value) { if (value instanceof HibernateProxy) {//hibernate代理对象 LazyInitializer initializer = ((HibernateProxy) value).getHibernateLazyInitializer(); if (initializer.isUninitialized()) { return false; } } else if (value instanceof PersistentCollection) {//实体关联集合一对多等 PersistentCollection collection = (PersistentCollection) value; if (!collection.wasInitialized()) { return false; } Object val = collection.getValue(); if (val == null) { return false; } } return true; }}
package com.pbh.filter;import org.hibernate.collection.spi.PersistentCollection;import org.hibernate.proxy.HibernateProxy;import org.hibernate.proxy.LazyInitializer;import com.alibaba.fastjson.serializer.PropertyFilter;import com.lianshangche.model.car.CarSeries;import com.lianshangche.model.car.CarSubBrand;/** * * Description: 过滤Hibernate懒加载不能序列化对象 * @author PanBaihui * @time 2017年9月15日 下午4:15:45 */public class CarSubBrandSimplePropertyFilter implements PropertyFilter { private Class<CarSubBrand> className = CarSubBrand.class; private Class<CarSeries> carSeriesClassName = CarSeries.class; private String [] propertieNames = {"remark", "enable"}; @Override public boolean apply(Object object, String name, Object value) { if (value instanceof HibernateProxy) {//hibernate代理对象 LazyInitializer initializer = ((HibernateProxy) value).getHibernateLazyInitializer(); if (initializer.isUninitialized()) { return false; } } else if (value instanceof PersistentCollection) {//实体关联集合一对多等 PersistentCollection collection = (PersistentCollection) value; if (!collection.wasInitialized()) { return false; } Object val = collection.getValue(); if (val == null) { return false; } } if (object.getClass().equals(className)||object.getClass().equals(carSeriesClassName)) {//非序列化判断 for (String propertie : propertieNames) { if (name.equals(propertie)) { return false; } } } return true; }}
package com.pbh.filter;import org.hibernate.collection.spi.PersistentCollection;import org.hibernate.proxy.HibernateProxy;import org.hibernate.proxy.LazyInitializer;import com.alibaba.fastjson.serializer.PropertyFilter;import com.lianshangche.model.car.CarType;/** * * Description: 过滤Hibernate懒加载不能序列化对象 * @author PanBaihui * @time 2017年9月15日 下午4:15:45 */public class CarTypeSimplePropertyFilter implements PropertyFilter { private Class<CarType> className = CarType.class; private String [] propertieNames = {"yearType", "sizeType", "saleState", "enable", "price"}; @Override public boolean apply(Object object, String name, Object value) { if (value instanceof HibernateProxy) {//hibernate代理对象 LazyInitializer initializer = ((HibernateProxy) value).getHibernateLazyInitializer(); if (initializer.isUninitialized()) { return false; } } else if (value instanceof PersistentCollection) {//实体关联集合一对多等 PersistentCollection collection = (PersistentCollection) value; if (!collection.wasInitialized()) { return false; } Object val = collection.getValue(); if (val == null) { return false; } } if (object.getClass().equals(className)) {//非序列化判断 for (String propertie : propertieNames) { if (name.equals(propertie)) { return false; } } } return true; }}
阅读全文
0 0
- 设计模式之静态工厂的使用
- 设计模式之静态工厂模式
- 设计模式之静态工厂模式
- 设计模式初探之静态工厂模式
- JAVA设计模式之工厂模式(静态工厂模式)
- 设计模式之静态工厂、工厂方法和抽象工厂的联系与区别
- 设计模式--静态工厂
- 静态工厂设计模式
- 设计模式-静态工厂
- 设计模式之 使用静态工厂模式代替构造函数(Java)
- 设计模式之工厂模式使用
- 设计模式之工厂模式之静态工厂方法模式(三)
- Java 静态工厂模式的使用
- 设计模式(2) 静态工厂
- JAVA的设计模式之工厂模式
- 设计模式之简单的工厂模式
- 设计模式之简单的工厂模式
- 设计模式之工厂模式的学习心得
- java类中的变量
- jq文字无缝滚动代码(鼠标悬停停止滚动)
- c++ 记录程序运行时间
- 条码识别控件Dynamsoft Barcode Reader更新至v5.2丨附下载
- 代码实现九九乘法表
- 设计模式之静态工厂的使用
- phpStudy在linux下的使用说明
- Unity中鼠标的交互事件
- JQuery 常用的方法总结
- 配合长时间定时任务,立即执行该定时任务的思路
- Sublime模板插件sublimeTmpl使用
- 导入as项目提示安装,安装失败
- thinkphp多字段模糊查询
- 九九乘法表,九九除法表