2016年12月14日 星期三

【Android】最簡潔的Model層 - AutoValue 使用介紹【三】

這篇要來說的是 AutoValue 如何搭配【Gson】一塊使用,

由於已經將 Model 內原先的field變更為 abstract method 了,

可想而知在 Gson de/serialize 的過程中會發生問題!

ps.如果不了解 Gson 可以先參考我的另一篇文章

不做任何處理的話會發現在 call Gson 的 fromJson() 時會噴出 Exception,

java.lang.RuntimeException: Failed to invoke public com.yourpackagename.Book() with no args
如果要讓 Gson 成功的序列化,就必須給它一個新的 parse 規則(預設規則不適用在我們的 AutoValue Model 身上),

這邊我們一樣會用到一個 library【auto-value-gson

接下來只需要幾個簡單步驟:
一、在 Model 類別內新增靜態的 typeAdapter() method
二、新增一個抽象類實作 TypeAdapterFactory,並添加一個可產生物件的靜態 method
三、使用 GsonBuilder 來 create Gson,並給予我們自訂義的 TypeAdapterFactory




Gradle:

//auto-value-gson
provided 'com.ryanharter.auto.value:auto-value-gson:0.4.6'
annotationProcessor 'com.ryanharter.auto.value:auto-value-gson:0.4.6'



After:

//步驟一

@AutoValue
public abstract class Book {
    public abstract String name();
    public abstract int price();
    public abstract String author();

    public static Book create(String name, int price, String author) {
        return new AutoValue_Book(name, price, author);
    }

    public static TypeAdapter<Book> typeAdapter(Gson gson) {
        return new AutoValue_Book.GsonTypeAdapter(gson);
    }
}
//步驟二

@GsonTypeAdapterFactory
public abstract class GsonAdapterFactory implements TypeAdapterFactory {
    public static TypeAdapterFactory create() {
        return new AutoValueGson_GsonAdapterFactory();
    }
}
//步驟三

String jsonString = "{\"author\":\"Anson\",\"name\":\"安森瓦舍\",\"price\":87}";
Gson gson = new GsonBuilder().registerTypeAdapterFactory(GsonAdapterFactory.create()).create();
//serialize
Book book = gson.fromJson(jsonString, Book.class);
//deserialize
System.out.println(gson.toJson(book));



這邊特別要注意的是【步驟二】的 return new AutoValueGson_GsonAdapterFactory() 的部分,
如果你在【步驟一】的 model 中沒有 typeAdapter 的靜態方法,那麼在 build project 時,AutoValueGson_GsonAdapterFactory 是不會被創建出來的。所以步驟可別亂掉!




最後,我在github上創建了一個Project來演示AotoValue與Rxjava2Retrofit2搭配使用的範例。




延伸閱讀:

● 最簡潔的Model層 - AutoValue 使用介紹【一】
● 最簡潔的Model層 - AutoValue 使用介紹【二】

2016年12月13日 星期二

【Android】最簡潔的Model層 - AutoValue 使用介紹【二】

一般來說,在Android若要採用較高效能的Parcelable來做資料傳遞,我們需要在Model內實作【writeToParcel、describeContents、CREATOR】,

這實在是一個很繁雜的功夫,也就是前一篇提過的duplicate code,

還好,在我們使用了AutoValue後,就能一併使用另一個plugin(auto-value-parcel),順便解決這個煩死人的實作。

ps.關於Parcelable與Serializable的效能差異可以參考我的另一篇文章



Gradle:

//auto-value-parcel
annotationProcessor 'com.ryanharter.auto.value:auto-value-parcel:0.2.5'



Before:

public class Book implements Parcelable {
    private String name;
    private int price;
    private String author;

    public Book(String name, int price, String author) {
        this.name = name;
        this.price = price;
        this.author = author;
    }

    @Override
    public String toString() {
        return "Book{"
                + "name=" + name
                + ", price=" + price
                + ", author=" + author
                + "}";
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }
    
    public static final Parcelable.Creator<Book> CREATOR = new Parcelable.Creator<Book>() {
        @Override
        public Book createFromParcel(Parcel in) {
            return new Book(
                    in.readString(),
                    in.readInt(),
                    in.readString()
            );
        }
        @Override
        public Book[] newArray(int size) {
            return new Book[size];
        }
    };

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(name);
        dest.writeInt(price);
        dest.writeString(author);
    }
}



After:

@AutoValue
public abstract class Book implements Parcelable {
    public abstract String name();
    public abstract int price();
    public abstract String author();

    public static Book create(String name, int price, String author) {
        return new AutoValue_Book(name, price, author);
    }
}


基本上在implement Parcelable 後,就不需要做事囉!

有沒有發現,我們的Model層越來越簡潔,越來越可愛了♡♡




延伸閱讀:

● 最簡潔的Model層 - AutoValue 使用介紹【一】
● 最簡潔的Model層 - AutoValue 使用介紹【三】

【Android】最簡潔的Model層 - AutoValue 使用介紹【一】

寫程式應該要盡可能的避免duplicate code(重複程式碼),一樣的、沒有價值的東西,一直寫幹嘛?對吧!

在Android開發上,不管你是使用哪一種Architecture(MVC、MVP、MVVM...等等),都一定有Model層,Model是我們在程式開發上最原始、最簡單的東西。

但打開你的Project看看你的Model,會發現重複的method一直在出現【getter、setter、equals、hashCode、toString....】等等等。

現在,我們透過【AutoValue】就可以大幅的簡化它了。

這個由google maintain的github開源項目,在本文章撰寫時最新版本是v1.4.1



Library projects:

Gradle:

//auto-value
provided 'com.google.auto.value:auto-value:1.4.1'
annotationProcessor 'com.google.auto.value:auto-value:1.4.1'



以下使用一個Book類別做示範



Before:

public class Book {
    private String name;
    private int price;
    private String author;

    public Book(String name, int price, String author) {
        this.name = name;
        this.price = price;
        this.author = author;
    }

    @Override
    public String toString() {
        return "Book{"
                + "name=" + name
                + ", price=" + price
                + ", author=" + author
                + "}";
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }
}
Book book = new Book("安森瓦舍", 87, "Anson");
System.out.println("書名:" + book.getName());
System.out.println("價格:" + book.getPrice());
System.out.println("作者:" + book.getAuthor());



After:

@AutoValue
public abstract class Book {
    public abstract String name();
    public abstract int price();
    public abstract String author();
    
    public static Book create(String name, int price, String author) {
        return new AutoValue_Book(name, price, author);
    }
}
Book book = Book.create("安森瓦舍", 87, "Anson");
System.out.println("書名:" + book.name());
System.out.println("價格:" + book.price());
System.out.println("作者:" + book.author());



如果你想使用Builder pattern的話

@AutoValue
public abstract class Book {
    public abstract String name();
    public abstract int price();
    public abstract String author();

    public static Book create(String name, int price, String author) {
        return builder()
                .name(name)
                .price(price)
                .author(author)
                .build();
    }

    public static Builder builder() {
        return new AutoValue_Book.Builder();
    }
    
    @AutoValue.Builder
    public abstract static class Builder {
        public abstract Builder name(String name);

        public abstract Builder price(int price);

        public abstract Builder author(String author);

        public abstract Book build();
    }
}
Book book = Book.create("安森瓦舍", 87, "Anson");
System.out.println("書名:" + book.name());
System.out.println("價格:" + book.price());
System.out.println("作者:" + book.author());



延伸閱讀:

● 最簡潔的Model層 - AutoValue 使用介紹【二】
● 最簡潔的Model層 - AutoValue 使用介紹【三】