Builder模式的演示

package com.mozq.mb.mb01.pojo;

/*
类的某些参数给出了默认值,但是一旦设置了就不可变。使用Builder模式来创建。
*/
public class ImageConfig {
private final int x;
private final int y;
private final int width;
private final int height;
private final String fileDiskPath; public ImageConfig(Builder builder){
this.x = builder.x;
this.y = builder.y;
this.width = builder.width;
this.height = builder.height;
this.fileDiskPath = builder.fileDiskPath;
} public static class Builder {
private int x = 0;
private int y = 0;
private int width = 0;
private int height = 0;
private String fileDiskPath = "";
public Builder(){}
public Builder(int x, int y, int width, int height, String fileDiskPath){
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.fileDiskPath = fileDiskPath;
}
public Builder x(int x){
this.x = x;
return this;
}
public Builder y(int y){
this.y = y;
return this;
}
public Builder width(int width){
this.width = width;
return this;
}
public Builder height(int height){
this.height = height;
return this;
}
public Builder fileDiskPath(String fileDiskPath){
this.fileDiskPath = fileDiskPath;
return this;
}
public ImageConfig build(){
return new ImageConfig(this);
}
} @Override
public String toString() {
return "ImageConfig{" +
"x=" + x +
", y=" + y +
", width=" + width +
", height=" + height +
", fileDiskPath='" + fileDiskPath + '\'' +
'}';
} public static void main(String[] args) {
ImageConfig imageConfig = new ImageConfig.Builder().x(200).build();
System.out.println(imageConfig);
}
}

Builder模式的演示的更多相关文章

  1. Android设计模式源码解析之Builder模式

    https://github.com/simple-android-framework/android_design_patterns_analysis/tree/master/builder/mr. ...

  2. Android 建造者(Builder)模式

    关于 Builder 模式 详述:http://blog.csdn.net/jjwwmlp456/article/details/39890699 先来张图 看到 Android  中 使用了 Bui ...

  3. Effective Java 第三版——2. 当构造方法参数过多时使用builder模式

    Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...

  4. Creational模式之Builder模式

    1.意图 将一个复杂对象的构建与它表示分离,使得相同的构建过程能够创建不同的表示. 查看很多其它请点击 2.别名 无 3.动机 一个RTF(Rich Text Format)文档交换格式的阅读器应能将 ...

  5. 当构造方法参数过多时使用builder模式

    静态工厂和构造方法都有一个限制:它们不能很好地扩展到很多可选参数的情景.请考虑一个代表包装食品上的营养成分标签的例子.这些标签有几个必需的属性——每次建议的摄入量,每罐的份量和每份卡路里 ,以及超过 ...

  6. 同事写了一个疯狂的类构造器,我要疯了,Builder 模式都不会么?!

    疯狂的类构造器 最近栈长在做 Code Review 时,发现一段创建对象的方法: Task task = new Task(112, "紧急任务", "处理一下这个任务 ...

  7. 疯狂的类构造器Builder模式,链式调用

    疯狂的类构造器 最近栈长在做 Code Review 时,发现一段创建对象的方法: Task task = new Task(112, "紧急任务", "处理一下这个任务 ...

  8. Joshua Bloch错了? ——适当改变你的Builder模式实现

    注:这一系列都是小品文.它们偏重的并不是如何实现模式,而是一系列在模式实现,使用等众多方面绝对值得思考的问题.如果您仅仅希望知道一个模式该如何实现,那么整个系列都会让您失望.如果您希望更深入地了解各个 ...

  9. Builder模式在Java中的应用

    在设计模式中对Builder模式的定义是用于构建复杂对象的一种模式,所构建的对象往往需要多步初始化或赋值才能完成.那么,在实际的开发过程中,我们哪些地方适合用到Builder模式呢?其中使用Build ...

随机推荐

  1. Mysql悲观锁乐观锁区别与使用场景

    本人免费整理了Java高级资料,涵盖了Java.Redis.MongoDB.MySQL.Zookeeper.Spring Cloud.Dubbo高并发分布式等教程,一共30G,需要自己领取.传送门:h ...

  2. Flask 教程 第十四章:Ajax

    本文翻译自The Flask Mega-Tutorial Part XIV: Ajax 这是Flask Mega-Tutorial系列的第十四部分,我将使用Microsoft翻译服务和少许JavaSc ...

  3. mysql5.5下载安装教程

    下载地址:https://dev.mysql.com/downloads/mysql/ 这里选择的是5.5的版本: 步骤1: 步骤2: 步骤三: 步骤四: 步骤5: 步骤6: 步骤7: 步骤8: 步骤 ...

  4. vue应用调试工具 vue-devtools安装

    方法一:chrome直接访问下面地址下载安装:(需要翻墙) https://chrome.google.com/webstore/detail/vuejs-devtools/nhdogjmejigli ...

  5. report for PA2

    目录 说明 Report for PA 2(writed with vim) Part i - pa2.1 Steps: instr(seperately) Part ii - 2.2 Part ii ...

  6. English: Class GXX

    ######################################## GGGGG GG GG GG GG GGGG GG GGG GGGGGG author:enomothem date: ...

  7. RU/RUR的安装

    RU/RUR的安装方法是仍然使用现有的Opatch技术来安装RU/RUR. 更多常见问题,请参考文档: Release Update and Release Update Revisions for ...

  8. mongodb基本安装

    这次搞搞NOSQL, 但最简单的MONGODB安装,还是要作点配置的. 一,安装网址: https://www.mongodb.com/download-center/community?jmp=na ...

  9. java.lang.IllegalStateException: Cannot call sendError() after the response has been committe

    1.问题描述 严重: Servlet.service() for servlet [default] in contextwith path [/OxygenCloud] threw exceptio ...

  10. 1+x 证书 Web 前端开发中级理论考试(试卷 8 )含答案

    1+x 证书 Web 前端开发中级理论考试(试卷 8 ) 官方QQ群 转载请注明来源:妙笔生花个人博客http://blog.zh66.club/index.php/archives/438/ 一.单 ...