Android设计模式—Builder模式

什么是Builder模式?Builder模式又称构建者模式,通俗的来说就是你在创建一个对象时,可能会出现一些属性需要进行默认值设置,但又不一定要将全部属性全部赋值,这时就需要Builder模式来解决这个问题了。<–在android设计开源框架时这种Builder的模式非常常见–>


由于比较简单就直接贴代码了。

  • 测试代码

    1. 定义需要构建的类:
package cn.com.statedemo;

/**
 * Created by Administrator on 2017/10/18 0018.
 * 商品类
 */

public class Commodity {

    //属性
    private int id;
    private int number;
    private int money;
    private String name;
    private String content;


    //若想要在创建对象时添加属性值
    public Commodity(int id) {
        this.id = id;
    }

    public Commodity(int id, int number) {
        this.id = id;
        this.number = number;
    }

    public Commodity(int id, int number, int money) {
        this.id = id;
        this.number = number;
        this.money = money;
    }

    public Commodity(int id, int number, int money, String name) {
        this.id = id;
        this.number = number;
        this.money = money;
        this.name = name;
    }

    public Commodity(int id, int number, int money, String name, String content) {
        this.id = id;
        this.number = number;
        this.money = money;
        this.name = name;
        this.content = content;
    }
    //但是这样做并不能实现所有需求,因为可能出现我需要id + name + content ,这样的不同的组合

    /**
     * 这时就需要Builder模式
     */

    //私有无参构造,保证不被创建
    private Commodity() {

    }

    //提供一个私有以当前类对象为参数的构造方法
    private Commodity(Commodity commodity) {
        this.id = commodity.getId();
        this.name = commodity.getName();
        this.money = commodity.getMoney();
        this.number = commodity.getNumber();
        this.content = commodity.getContent();
    }


    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public int getMoney() {
        return money;
    }

    public void setMoney(int money) {
        this.money = money;
    }

    public String getName() {
        return name;
    }

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

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    //声明一个静态类
    public static class Builder {

        private Commodity commodity;

        public Builder() {
            commodity = new Commodity();
        }

        /**
         * 动态添加下列属性
         */

        public Builder id(int id) {
            commodity.setId(id);
            return this;
        }

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

        public Builder number(int number) {
            commodity.setNumber(number);
            return this;
        }

        public Builder money(int money) {
            commodity.setMoney(money);
            return this;
        }

        public Builder content(String content) {
            commodity.setContent(content);
            return this;
        }

        //最后返回一个Commodity对象。
        public Commodity build() {
            return new Commodity(commodity);
        }
    }
}

2.测试构建结果:

        //只控制构造,只能构造你所定义的参数列表
        Commodity commodity_1 = new Commodity(1);
        Commodity commodity_2 = new Commodity(1, 1);
        //使用了Builder模式,可动态添加你所需的属性
        Commodity commodity_3 = new Commodity.Builder().id(1).content("content").build();
        Commodity commodity_4 = new Commodity.Builder().id(1).name("name").build();

————————-
:尽力做好自己份内的事,剩下的就交给老天,其实人生如是而已。

你可能感兴趣的:(设计模式)