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();

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值