最简单合理有效的建造者模式

建造者模式

最近在学习23中设计模式,当看到建造者模式的时候,感觉网上很多文章并不是很好。因为自己在项目中也常用这种模式(主要是用于建造dialog,不知道大家是否也常用)。所以这里想做一个分享。

通常,一个项目中的dialog风格都是一样的 只是展示的内容和回调不一样,这个时候用建造者模式是最省事的,但是这里我不准备用这个例子(因为太麻烦了),所以用一个简单的例子,建造一个流水账。

            (    人物  )   在      (  地点     )   (     事情      ),用了(   多少    )块钱。 

就构建一个流水账然后打印出来吧!
首先需要一个流水账类,也就是我们需要构建的东西。

public class Account {
    private String mPeople;  //人物
    private String mPlace;   //地点
    private String mThing;   //事情
    private double money;     //钱
    
    @Override
    public String toString() {
        return mPeople + "在" +
                        mPlace +
                       mThing+ ",花了" + money + "块钱";
    }

    }

然后直接在Account类内部新建一个静态的构建者类

   public static class Builder{
        private String mPeople;  //人物
        private String mPlace;   //地点
        private String mThing;   //事情
        private double money;     //钱

       public Builder() {
       }

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

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

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

        public Builder setMoney(double money) {
            this.money = money;
            return this;
        }

        public Account build(){
            return new Account(this);
        }

    }

上面每一个set方法就是我们根据需要构建不同的属性。
然后可以看到 Account需要一个以Build为参数的构建方法

   private Account(Builder build) {   //可以给成私有的,不允许外界调用
        if (0 != build.money){
            this.money = build.money;
        }
        if (null != build.mPeople){
            this.mPeople = build.mPeople;
        }else {
            this.mPeople = "小明";     //根据需要给默认值
        }
        if (null != build.mPlace){
            this.mPlace = build.mPlace;
        }
        if (null != build.mThing){
            this.mThing = build.mThing;
        }
    }

最简单的构建者就成了 可以试试

  System.out.println(
                new Account.Builder().setMoney(10).setmPeople("小红").setmPlace("超市").setmThing("买零食").build().toString()
        );
        System.out.println(
                new Account.Builder().setMoney(10).setmPlace("超市").setmThing("买零食").build().toString()
        );
        System.out.println(
                new Account.Builder().setMoney(100).setmPeople("小明和小红").setmPlace("教室").setmThing("学习").build().toString()
        );

打印的结果:

小红在超市买零食,花了10.0块钱
小明在超市买零食,花了10.0块钱
小明和小红在教室学习,花了100.0块钱

如果需要添加回调,也用同样的办法就行了,新建回调接口,然后在build定义接口set就行了,一样的道理。
可以看出,建造者模式其实适合和工厂模式一起使用会更优雅,耦合性更好(项目大一点的话)。
以上只是个人的见解。如有不对或者不太好的地方,还请多多指正。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值