android 設計模式之builder(一)

Android中的builder模式非常的常見,應用的也很廣泛,不okhttp,alertdialog等等都有使用這種模式,這種模式的好處就是講操作的細節隱藏了起來,只關注結果;今天我就簡單地分析下alertdialog的源碼,但是呢,先來寫一個關於builder的簡單demo,這樣理解alertdialog的源碼了;

package com.example.dialog;

import android.content.Context;

/**
 * Created by wsl on 2016/12/25.
 */

public class Man {

    public String name;
    public int age;
    public String sex;
    public boolean isIT;
    public Context context;

    public static class Builder {

        private String name;
        private int age;
        private String sex;
        private boolean isIT = true;//如果不傳入值就表示默認true

        private Context mContext;

        public Builder(Context context) {
            mContext = context;
        }

        public Builder name(String name) {
            this.name = name;
            return this;
        }

        public Builder age(int age) {
            this.age = age;
            return this;
        }

        public Builder sex(String sex) {
            this.sex = sex;
            return this;
        }

        public Builder isIT(boolean is) {
            this.isIT = is;
            return this;
        }

        public Context getContext() {
            return mContext;
        }


        public Man build() {
            Man man = new Man();
            applly(man);
            return man;
        }


        private void applly(Man man) {
            if (this.name != null) {
                man.name = this.name;
            }

            if (this.age != 0) {
                man.age = this.age;
            }
            if (this.sex != null) {
                man.sex = this.sex;
            }
            if (this.isIT == true) {
                man.isIT = this.isIT;
            }

            if (this.mContext != null) {
                man.context = this.mContext;
            }
        }
    }
}

activity是這樣寫的,

Man man = new Man.Builder(this)
                        .name("曲洋")
                        .age(20)
                        .sex("man")
                        .build();

                Toast.makeText(this, "man.isIT:" + man.isIT, Toast.LENGTH_SHORT).show();

然而並沒有結束,換藥進一步優化:


public class ManController {

    public String name;
    public int age;
    public String sex;
    public boolean isIT;
    public Context context;


    public static class ManParams {

        public String name;
        public int age;
        public String sex;
        public boolean isIT = false;//默認不是
        public Context context;
        public LayoutInflater inflater;


        public ManParams(Context context) {
            this.context = context;
            this.inflater = inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }


        public void apply(ManController controller) {
            if (this.name != null) {
                controller.name = controller.name;
            }

            if (this.age != 0) {
                controller.age = controller.age;
            }
            if (this.sex != null) {
                controller.sex = controller.sex;
            }
            if (this.isIT == true) {
                controller.isIT = controller.isIT;
            }

            if (this.context != null) {
                controller.context = controller.context;
            }

        }


    }

這樣就可以將man類中的變量封裝到一個對象中:


public class Man {


    public ManController controller;

    public static class Builder {

        public ManController.ManParams  p;


        private Context mContext;

        public Builder(Context context) {
            mContext = context;
        }

        public Builder name(String name) {
            p.name = name;
            return this;
        }

        public Builder age(int age) {
            p.age = age;
            return this;
        }

        public Builder sex(String sex) {
            p.sex = sex;
            return this;
        }

        public Builder isIT(boolean is) {
            p.isIT = is;
            return this;
        }

        public Context getContext() {
            return mContext;
        }


        public Man build() {
            Man man = new Man();
            p.apply(man.controller);
            return man;
        }

    }
}

好了,先寫這麼點。alertdialog的源碼模式就和這個demo基本一致,如果試圖將本博客的代碼粘過去運行,那你就錯了,這只是模仿源碼的一部分而已;

發佈了65 篇原創文章 · 獲贊 9 · 訪問量 19萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章