ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Builder模式

2019-07-09 15:01:09  阅读:116  来源: 互联网

标签:return int Builder 模式 age BuilderTest speed public


还没有看过Builder模式的作用,看过一篇介绍Builder模式的文章,这里是关于Builder模式的思考:思考是否有比新建一个内部类更好的方法,首先想到的是

package xyz.n490808114.test;
public class  BuilderTest{
    String name;
    int age;
    int high;
    int weight;
    int speed;

    public BuilderTest name(String name){
        this.name = name;
        return this;
    }
    public BuilderTest age(int age){
        this.age = age;
        return this;
    }
    public BuilderTest high(int high){
        this.high = high;
        return this;
    }
    public BuilderTest weight(int weight){
        this.weight = weight;
        return this;
    }
    public BuilderTest speed(int speed){
        this.speed = speed;
        return this;
    }
    public void setName(String name){this.name = name;}
    public void setAge(int age){this.age = age;}
    public void setHigh(int high){this.high = high;}
    public void setWeight(int weight){this.weight = weight;}
    public void setSpeed(int speed){this.speed = speed;}
    public String getName(){return this.name;}
    public int getAge(){return this.age;}
    public int getHigh(){return this.high;}
    public int getWeight(){return this.weight;}
    public int getSpeed(){return this.speed;}
    public static void main(String[] args) {
        BuilderTest t = new BuilderTest().age(20).speed(50);
        System.out.println(t.name);
        System.out.println(t.age);
        System.out.println(t.high);
        System.out.println(t.weight);
        System.out.println(t.speed);
    }
}

输入为

null
20
0
0
50

这样感觉跟set方法重复太多,整合一下,如下

package xyz.n490808114.test;
public class  BuilderTest{
    String name;
    int age;
    int high;
    int weight;
    int speed;
    public BuilderTest setName(String name){this.name = name;return this;}
    public BuilderTest setAge(int age){this.age = age;return this;}
    public BuilderTest setHigh(int high){this.high = high;return this;}
    public BuilderTest setWeight(int weight){this.weight = weight;return this;}
    public BuilderTest setSpeed(int speed){this.speed = speed;return this;}
    public String getName(){return this.name;}
    public int getAge(){return this.age;}
    public int getHigh(){return this.high;}
    public int getWeight(){return this.weight;}
    public int getSpeed(){return this.speed;}
    public static void main(String[] args) {
        BuilderTest t = new BuilderTest().setAge(20).setSpeed(50);
        System.out.println(t.name);
        System.out.println(t.age);
        System.out.println(t.high);
        System.out.println(t.weight);
        System.out.println(t.speed);
    }
}

这样只是精简代码,只是不知道这样的set方法能不能被架构认可,待测试

能够想到的是:

1,Builder模式用内部类的方法,可以用来修改现有代码,不改动,不新增类的方法,仅在创建时使用内部类;

2,猜测:如果不是频繁创建的话,对性能的影响不大;

3,如果不采用模式,仅使用set方法的话,代码量会多,但是代码会更清晰;

4,Builder模式使代码更易阅读。

标签:return,int,Builder,模式,age,BuilderTest,speed,public
来源: https://www.cnblogs.com/xiangzhahao/p/11157397.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有