此篇博客,更換格式爲MarkDown,請查看:http://www.javashuo.com/article/p-brqfiten-mr.htmlhtml
如今不少人都在用lombok,經過註解就能夠簡化代碼,省略咱們好多工做。可是其中有兩個註解對於咱們建立對象很是好用,一個是@Builder,一個是@Access;java
主要是學習一下@Builder這個註解所涉及到的設計模式;sql
SqlSession sqlSession = new SqlSessionFactoryBuilder() .build(new FileInputStream(new File("/mapper/test.xml"))) .openSession();
@Data public class ResultObj { private Object result; private String message; private Integer code; private ResultObj(Object result,String message,Integer code){ this.result = result; this.message = message; this.code = code; } public static ResultObj successObj(Object obj){ return successObj(obj,StatusEnum.SUCCESS.getMessage()); } public static ResultObj successObj(Object obj,String message){ return new ResultObj(obj,message, StatusEnum.SUCCESS.getCode()); } public static ResultObj failObj(){ return failObj(StatusEnum.FAIL.getMessage()); } public static ResultObj failObj(String message){ return failObj(null,message); } public static ResultObj failObj(Object result,String message){ return new ResultObj(result,message,StatusEnum.FAIL.getCode()); } }
將一個複雜對象的構建與它的表示分離,使得一樣的建造過程能夠建造不一樣的表示;(一句話總結,就是建對象唄,不過對象裏面的參數比較多複雜)設計模式
首先,咱們通常建立對象的方法有兩種:1.構造器建立;2.經過屬性set方法建立;session
構造器建立對象:mybatis
class A{ private String a; private String b; private String c ; A(){} A(String a){ this.a = a; } A(String a,String b){ this.a = a; this.b = b; } A(String a,String b,String c){ this.a = a; this.b = b; this.c = c ; } }
set方式建立對象app
class B{ private String a; B(){} public void setA(){ this.a = a } public String getA(){ return this.a ; } }
分析一下上面兩種方式存在的問題:框架
- 1. 構造器建立對象,多個屬性的話,必須嚴格按照順序注入;set方法不存在這種問題; - 2.構造器建立對象,可讀性差,若是對象中有多個屬性賦值 ,必須清楚每一個屬性參數位置;set方法不存在這種問題; - 3.set方法建立對象,對象在使用過程當中,容易被錯修改;
根據上面可知,當複雜對象建立時,構造器方法可讀性不好,且屬性傳參不方便,set方法建立對象在使用過程當中可能出現誤操做的缺點;學習
建造器模式完美的解決了上面的兩種問題;ui
經過建立builder接口,或者內置靜態成員類,來建立構造器;
public class NutritionFacts { private final int serversize; private final int servers; private final int calorizes; private final int fat; private final int sodium; private final int carbohydrate; static class Builder{ //必填項 private final int serversize; private final int servers; //選填項 private int calorizes; private int fat; private int sodium; private int carbohydrate; public Builder(int serversize,int servers){ this.serversize = serversize; this.servers = serversize; } public Builder calorizes(int calorizes){ this.calorizes = calorizes; return this; } public Builder fat(int fat){ this.fat = fat; return this; } public Builder sodium(int sodium){ this.sodium = sodium; return this; } public Builder carbohydrate(int carbohydrate){ this.carbohydrate = carbohydrate; return this; } //增長一個建立對象的build public NutritionFacts build(){ return new NutritionFacts(this); } } public NutritionFacts(Builder builder){ this.serversize = builder.serversize; this.servers = builder.servers; this.calorizes = builder.calorizes; this.fat = builder.fat; this.sodium = builder.sodium; this.carbohydrate = builder.carbohydrate; } }
上面說了lombok還提供了 @Access註解,這個註解的原理比較簡單,就是將傳統的set方法變成靜態,返回當前對象;
class A{ private String name; public static A setName(String name){ this.name = name; return this; } }
以上就是對構造器模式的簡單介紹。