又叫生成器模式

public class MainActivity extends AppCompatActivity {

    TextView textView;
Button button;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.text);
button = findViewById(R.id.button); Student.Builder builder = new Student.Builder();
String address = builder.address("北京").name("四环").build().getAddress();
System.out.println(address);
}
}

public class Student {

    private int id;
private String name;
private String passwd;
private String sex;
private String address; // 构造器尽量缩小范围
private Student() {
} // 构造器尽量缩小范围
private Student(Student origin) {
// 拷贝一份
this.id = origin.id;
this.name = origin.name;
this.passwd = origin.passwd;
this.sex = origin.sex;
this.address = origin.address;
} public int getId() {
return id;
} public String getName() {
return name;
} public String getPasswd() {
return passwd;
} public String getSex() {
return sex;
} public String getAddress() {
return address;
} /**
* Student的创建完全依靠Student.Builder,使用一种方法链的方式来创建
*
*/
public static class Builder { private Student target; public Builder() {
target = new Student();
} public Builder address(int id) {
target.id = id;
return this;
} public Builder name(String name) {
target.name = name;
return this;
} public Builder password(String passwd) {
target.passwd = passwd;
return this;
} public Builder sex(String sex) {
target.sex = sex;
return this;
} public Builder address(String address) {
target.address = address;
return this;
} public Student build() {
return new Student(target);
} } }

build设计模式的更多相关文章

  1. Android使用RxJava+Retrofit2+Okhttp+MVP练习的APP

    Android使用RxJava+Retrofit2+Okhttp+MVP练习的APP 项目截图     这是我的目录结构 五步使用RxJava+Retrofit2+Okhttp+RxCache 第一步 ...

  2. Shiro-Subject 分析

    Subject反正就好像呈现的视图.所有Subject 都绑定到SecurityManager,与Subject的所有交互都会委托给SecurityManager:可以把Subject认为是一个门面: ...

  3. <Android 开源库> PhotoPicker 从头到脚

    1. 简介 PhotoPicker, 是一款开源的图片选择器.效果上和微信相似. 2. 使用方法 2.1 添加依赖 dependencies { compile 'me.iwf.photopicker ...

  4. Matisse,来自知乎的PhotoPicker

    简介 Matisse,是一款由知乎开源的媒体选择器. 在Activity和Fragment中使用 支持JPEG,PNG,GIF的图片选择和MPEG,MP4格式的视频选择.不能同时选择图片和视频 两种主 ...

  5. PhotoPicker 从头到脚

    1. 简介 PhotoPicker, 是一款开源的图片选择器.效果上和微信相似. 2. 使用方法 2.1 添加依赖 dependencies { compile 'me.iwf.photopicker ...

  6. 使用Java操作Elasticsearch(Elasticsearch的java api使用)

    1.Elasticsearch是基于Lucene开发的一个分布式全文检索框架,向Elasticsearch中存储和从Elasticsearch中查询,格式是json. 索引index,相当于数据库中的 ...

  7. Shiro-Subject 分析(转)

    Subject反正就好像呈现的视图.所有Subject 都绑定到SecurityManager,与Subject的所有交互都会委托给SecurityManager:可以把Subject认为是一个门面: ...

  8. 设计模式01 创建型模式 - 建造者模式(Build Pattern)

    参考 1. Builder Design Pattern | Youtube 2. 建造者模式(Builder和Director)| 博客园 3. 深入理解Builder模式 | 简书 建造者模式(B ...

  9. 设计模式之Build(生成者模式)

    一.生成器模式的定义: 生成器模式也称为建造者模式.生成器模式的意图在于将一个复杂的构建与其表示相分离,使得同样的构建过程可以创建不同的表示(GoF).在软件设计中,有时候面临着一个非常复杂的对象的创 ...

随机推荐

  1. 关于SpringBoot的一点笔记

    @SpringBootApplication /** * @SpringBootApplication 来标注一个主程序类,说明这是一个Spring Boot应用 */ @SpringBootAppl ...

  2. leetcode1558题解【贪心】

    leetcode1558.得到目标数组的最少函数调用次数 题目链接 算法 贪心 时间复杂度O(nlogN),N为数组中最大的那个数. 1.题意就是给定一个函数,该函数有两种功能,一种就是将数组中的所有 ...

  3. C++练习案例1.计算机类(利用多态实现)

    c++简单计算机类 简介 大家好,这里是天天like的博客,这是我发的第一篇随笔,用来记录我的学习日程,大家可以相互学习,多多交流,感谢 今天我要记录的随笔是在学习c++多态的知识点练习改进的一个案例 ...

  4. oracle数据库外部连接无法访问

    服务器出现的问题是运行的项目无法访问oracle数据库连接,用plsql输入用户名密码后卡死,无法连接.但是通过命令窗口对oracle数据库操作正常,对oracle服务进行查看并重启,并无异常,运行t ...

  5. 大话Python切片,助你彻底掌控切片小妖精

    切片语义 生活中切黄瓜.切萝卜.一本书的每一页等等都是符合切片的语义 切片的语义是从某个东西中通过某种手段拿到某个整体的一部分 切片是拿来主义,建立在已经有的序列上,有黄瓜才能切黄瓜  列表 -> ...

  6. Centos-强制将内存中数据写入磁盘-sync

    sync 强制将内存中数据写入磁盘,以免数据丢失.在linux系统中,修改过的操作并不会立即写入磁盘,而是先写到内存中,通过buffer队列当达到指定时间或者指定大小再一次性写入磁盘,提高IO效率,正 ...

  7. C++ 构造函数 隐式转换 深度探索,由‘类对象的赋值操作是否有可能调用到构造函数’该实验现象引发

    Test1 /** Ques: 类对象的赋值操作是否有可能调用到构造函数 ? **/ class mystring { char str[100]; public: mystring() //myst ...

  8. 下载 Oracle Database XE 11gR2

    操作系统:Windows 10 x64 第一节:下载 Oracle Database XE 11gR2 第二节:安装.验证安装 Oracle Database XE 11gR2 第三节:Oracle ...

  9. 【学习笔记/题解】虚树/[SDOI2011]消耗战

    题目戳我 \(\text{Solution:}\) 题目很显然可以设\(dp[i]\)表示\(i\)的子树内的关键点都不和\(i\)联通的最小待机,有如下\(dp\)方程: \(v\in son_u, ...

  10. ubuntu20 make redis6

    redis 官网:https://redis.io redis 下载和编译位置: cd /opt 下载 redis: wget http://download.redis.io/releases/re ...