<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">//默认创建</span>Gson gson= new Gson();
仅供查考:
package com.zghw.gson; public final class GsonBuilder { private Object config1 = new Object(); private Object config2 = new Object(); private Object confign = new Object(); /** * 创建者设置配置 * * @param config1 * @return */ public GsonBuilder setConfig1(String config1) { this.config1=config1; System.out.println("设置配置1后返回,配置后的创建者"); return new GsonBuilder(); } /** * 创建者设置配置2 * * @param config2 * @return */ public GsonBuilder setConfig2(String config2) { this.config2=config2; System.out.println("设置配置2后返回,配置后的创建者"); return new GsonBuilder(); } /** * 设置配置N * * @return */ public GsonBuilder setConfign() { this.confign=config1; System.out.println("设置配置n后返回配置后的创建者"); return new GsonBuilder(); } public Gson create() { System.out.println("组合各种配置进行产品的创建"); return new Gson(config1,config2,confign); } }
package com.zghw.gson; public final class Gson { public Gson() { this(new Object(),new Object(),new Object()); } Gson(Object config1, Object config2, Object config3) { System.out.println("初始化所有配置!"); } }