Java设计模式之builder模式
Java设计模式之builder模式
今天学mybatis的时候,知道了SQLSessionFactory使用的是builder模式来生成的。再次整理一下什么是builder模式以及应用场景。
1. builder简介
builder模式也叫建造者模式,builder模式的作用将一个复杂对象的构建与他的表示分离,使用者可以一步一步的构建一个比较复杂的对象。
2. 代码实例
我们通常构造一个有很多参数的对象时有三种方式:构造器重载,JavaBeans模式和builder模式。通过一个小例子我们来看一下builder模式的优势。
2.1 构造器重载方式
package com.wangjun.designPattern.builder;
public class Product {
private int id;
private String name;
private int type;
private float price;
public Product() {
super();
}
public Product(int id) {
super();
this.id = id;
}
public Product(int id, String name) {
super();
this.id = id;
this.name = name;
}
public Product(int id, String name, int type) {
super();
this.id = id;
this.name = name;
this.type = type;
}
public Product(int id, String name, int type, float price) {
super();
this.id = id;
this.name = name;
this.type = type;
this.price = price;
}
}
使用构造器重载我们需要定义很多构造器,为了应对使用者不同的需求(有些可能只需要id,有些需要id和name,有些只需要name,......),理论上我们需要定义2^4 = 16个构造器,这只是4个参数,如果参数更多的话,那将是指数级增长,肯定是不合理的。要么你定义一个全部参数的构造器,使用者只能多传入一些不需要的属性值来匹配你的构造器。很明显这种构造器重载的方式对于多属性的情况是不完美的。
2.2 JavaBeans方式
package com.wangjun.designPattern.builder;
public class Product2 {
private int id;
private String name;
private int type;
private float price;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
}
JavaBeans方式就是提供setter方法,在使用的时候根据需求先调用无参构造器再调用setter方法填充属性值。
Product2 p2 = new Product2();
p2.setId(10);
p2.setName("phone");
p2.setPrice(100);
p2.setType(1);
这种方式弥补了构造器重载的不足,创建实例很容易,代码读起来也很容易。但是,因为构造过程被分到了几个调用中,在构造过程中JavaBeans可能处于不一致的状态,类无法仅仅通过检验构造器参数的有效性来保证一致性。
2.3 builder模式
package com.wangjun.designPattern.builder;
public class Product3 {
private final int id;
private final String name;
private final int type;
private final float price;
private Product3(Builder builder) {
this.id = builder.id;
this.name = builder.name;
this.type = builder.type;
this.price = builder.price;
}
public static class Builder {
private int id;
private String name;
private int type;
private float price;
public Builder id(int id) {
this.id = id;
return this;
}
public Builder name(String name) {
this.name = name;
return this;
}
public Builder type(int type) {
this.type = type;
return this;
}
public Builder price(float price) {
this.price = price;
return this;
}
public Product3 build() {
return new Product3(this);
}
}
}
可以看到builder模式将属性定义为不可变的,然后定义一个内部静态类Builder来构建属性,再通过一个只有Builder参数的构造器来生成Product对象。Builder的setter方法返回builder本身,以便可以将属性连接起来。我们就可以像下面这样使用了。
Product3 p3 = new Product3.Builder()
.id(10)
.name("phone")
.price(100)
.type(1)
.build();
当然具体使用builder的情况肯定没有这么简单,但是思路大致一样:先通过某种方式取得构造对象需要的所有参数,再通过这些参数一次性构建这个对象。比如MyBatis中SqlSessionFactoryBuilder就是通过读取MyBatis的xml配置文件来获取构造SqlSessionFactory所需要的参数的。
Java设计模式之builder模式的更多相关文章
- Java设计模式-建造者(Builder)模式
目录 由来 使用 1. 定义抽象 Builder 2. 定义具体 Builder类 3. 定义具体 Director类 4. 测试 定义 文字定义 结构图 优点 举例 @ 最近在看Mybatis的源码 ...
- 折腾Java设计模式之建造者模式
博文原址:折腾Java设计模式之建造者模式 建造者模式 Separate the construction of a complex object from its representation, a ...
- 折腾Java设计模式之模板方法模式
博客原文地址:折腾Java设计模式之模板方法模式 模板方法模式 Define the skeleton of an algorithm in an operation, deferring some ...
- java设计模式6--适配器模式(Adapter )
本文地址:http://www.cnblogs.com/archimedes/p/java-adapter-pattern.html,转载请注明源地址. 适配器模式(别名:包装器) 将一个类的接口转换 ...
- java设计模式5--原型模式(Prototype)
本文地址:http://www.cnblogs.com/archimedes/p/java-prototype-pattern.html,转载请注明源地址. 原型模式 用原型实例指定创建对象的种类,并 ...
- Java 设计模式之建造者模式(四)
原文地址:Java 设计模式之建造者模式(四) 博客地址:http://www.extlight.com 一.前言 今天继续介绍 Java 设计模式中的创建型模式--建造者模式.上篇设计模式的主题为 ...
- 设计模式:Builder模式
设计模式:Builder模式 一.前言 今天我们讨论一下Builder建造者模式,这个Builder,其实和模板模式非常的像,但是也有区别,那就是在模板模式中父类对子类中的实现进行操作,在父类之 ...
- java设计模式3——建造者模式
java设计模式3--建造者模式 1.建造者模式介绍: 建造者模式属于创建型模式,他提供了一种创建对象得最佳方式 定义: 将一个复杂对象的构建和与它的表示分离,使得同样的构建过程可以创建不同的表示 主 ...
- Java设计模式——装饰者模式
JAVA 设计模式 装饰者模式 用途 装饰者模式 (Decorator) 动态地给一个对象添加一些额外的职责.就增加功能来说,Decorator 模式相比生成子类更为灵活. 装饰者模式是一种结构式模式 ...
随机推荐
- TCP协议的常见面试题
1. 为什么连接的时候是三次握手,关闭的时候却是四次握手? 因为当Server端收到Client端的SYN连接请求报文后,可以直接发送SYN+ACK报文.其中ACK报文是用来应答的,SYN报文是用来同 ...
- 前端有用的CSS属性和JS方法
1.CSS属性: 透明属性(值越大越不透明): IE:filter:alpha(opacity:30) Google:opacity:0.3 层次属性(值大的会在上面): z-index:100 2. ...
- xen虚拟化环境安装
1. 操作系统安装 OEL下载地址大全: http://koumm.blog.51cto.com/703525/1283801 # uname -a Linux localhost 2.6.39-40 ...
- python绘图 转
Python有很多可视化工具,本篇只介绍Matplotlib. Matplotlib是一种2D的绘图库,它可以支持硬拷贝和跨系统的交互,它可以在Python脚本.IPython的交互环境下.Web应用 ...
- Django 之restfromwork 序列化组件实现数据增删查改
rest-framework序列化之Serializer models.py from django.db import models # Create your models here. class ...
- linux 命令输出保存为文件的三种方式
一.ls >2.txt 将ls命令直接保存到home文件夹下的2.txt,命令窗口无显示 二.ls | tee 2.txt 也是直接保存在了home文件夹下的2.txt,命令 ...
- X509Store 类
标题:X509Store 类 地址:https://docs.microsoft.com/zh-cn/dotnet/api/system.security.cryptography.x509certi ...
- 2016年的EK工具
什么是Exploit Kit? 预打包了安装程序.控制面板.恶意代码以及相当数量的攻击工具.通常基于PHP的一个软件. Exploit Kit经济体制 价格在成百上千美元: 可以按日/周/月来付租金: ...
- Android Studio在代码重构中的妙用
代码重构几乎是每个程序员在软件开发中必须要不断去做的事情,以此来不断提高代码的质量.Android Stido(以下简称AS)以其强大的功能,成为当下Android开发工程师最受欢迎的开发工具,也是A ...
- flux沉思录:面向store和通信机制的前端框架
一.综述 Flux 被用来描述“单向”的数据流,且包含某些特殊的事件和监听器. 响应式编程是一种面向数据流和变化传播的编程范式 flux是响应式编程的一种? Flux 在本质上采用了模型-视图-控制器 ...