Javabuild模式
当⼀个类的参数多的情况下,使⽤重叠构造器模式客户端代码会很难编写,并且可读性差;使⽤javabean模式,调⽤⼀个⽆参的构造器,然后调⽤setter⽅法来设置每个必要的参数。但是javabean⾃⾝有着严重的缺点,因为构造过程被分到⼏个调⽤中,在构造javabean可能处于不⼀致的状态,类⽆法仅仅通过检验构造器参数的有效性来保证⼀致性。另⼀点不⾜之处,javabean模式阻⽌了把类做成不可变的可能,这就需要程序员付出额外的努⼒来确保他的线程安全; build模式 既能保证像重叠构造器那样的安全,也能实现JavaBean模式那样的可读性。
使⽤build模式的步骤:
(1)不直接⽣成想要的对象,⽽是让客户端利⽤所有必要的参数调⽤构造器(或者静态⼯⼚),得到⼀个build对象。
(2)然后让客户端在build对象上调⽤类似的setter⽅法来设置每个相关的可选参数,
(3)最后,客户端调⽤⽆参的build⽅法来⽣成不可变的对象。这个builder是它构建的静态成员类。
实例:
package ity;
/**
设备实体类
* @author 崔耀强 20150507-20150822
* @version 1.1
* */
import java.util.ArrayList;
public class Equipment {
private int id; //设备id
private String name; //设备的名称
private String location; //位置信息
private double coordinateX; // 坐标x
private double coordinateY; // 坐标y
private EquipmentType equipmentType; //设备类型
private String IP; //IP
private String port; //端⼝号
private int module; //属于哪个模块(设备连接和位置区分)
private int subSysId; //属于哪个⼦系统
private String health; //设备健康值
private int netWorkState; //⽹络情况
private ArrayList<BusinessValue> value;
public ArrayList<BusinessValue> getValue() {
return value;
}
public void setValue(ArrayList<BusinessValue> value) {
this.value = value;
}
public int getNetWorkState() {
return netWorkState;
}
public void setNetWorkState(int netWorkState) {
thisWorkState = netWorkState;
}
public String getHealth() {
return health;
}
public void setHealth(String health) {
this.health = health;
}
public int getCoordinateInterfaceID() {
return module;
}
public void setCoordinateInterfaceID(int coordinateInterfaceID) { module = coordinateInterfaceID;
}
public String getIP() {
return IP;
}
public void setIP(String iP) {
IP = iP;
}
public String getPort() {
return port;
}
public void setPort(String port) {
this.port = port;
}
public EquipmentType getEquipmentType() {
return equipmentType;
}
public void setEquipmentType(EquipmentType equipmentType) { this.equipmentType = equipmentType;
}
public double getCoordinateX() {
return coordinateX;
}
public void setCoordinateX(double coordinateX) {
}
public double getCoordinateY() {
return coordinateY;
}
public void setCoordinateY(double coordinateY) {
}
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 String getLocation() {
return location;
return location;
}
public void setLocation(String location) {
this.location = location;
}
public Equipment(int id, String location ) {
this.id = id;
this.location = location;
}
public int getModleID() {
return subSysId;
}
public void setModleID(int modleID) {
this.subSysId = modleID;
}
public static class Builder {
private int id; //设备id
private String name; //shebei的名称
private String location; //位置信息
private double coordinateX; // 坐标x
private double coordinateY; // 坐标y
private EquipmentType equipmentType; //设备类型
private String IP; //IP
private String port; //端⼝号
private int module; //属于哪个模块(设备连接和位置区分)
private int subSysId; //属于哪个⼦系统
private String health; //设备健康值
private int netWorkState; //⽹络情况
private ArrayList<BusinessValue> value;
public Builder(int id, String name,EquipmentType equipmentType,String location) { super();
this.id = id;
this.name = name;
this.equipmentType = equipmentType;
this.location = location;
}
public Builder coordinate_X(double coordinate_X) {
return this;
}
public Builder coordinate_Y(double coordinate_Y) {
return this;
}
public Builder iP(String iP) {
IP = iP;
return this;
}
public Builder port(String port) {
this.port = port;
return this;
}
public Builder module(int module) {
return this;
}
public Builder subSysId(int subSysId) {
public Builder subSysId(int subSysId) {
this.subSysId = subSysId;
return this;
}
public Builder health(String health) {
java零基础该怎么学this.health = health;
return this;
}
public Builder netWorkState(int netWorkState) {
thisWorkState = netWorkState;
return this;
}
public Builder value(ArrayList<BusinessValue> value) {
this.value = value;
return this;
}
public Equipment build() {
return new Equipment(this);
}
}
private Equipment(Builder builder) {
id = builder.id;
name = builder.name;
location = builder.location;
coordinateX = dinateX;
coordinateY = dinateY;
equipmentType = builder.equipmentType;
IP = builder.IP;
port = builder.port;
module = dule;
subSysId = builder.subSysId;
health = builder.health;
netWorkState = builderWorkState;
value = builder.value;
}
public Equipment() {
super();
}
}
⽆意中发现了⼀个巨⽜的⼈⼯智能教程,忍不住分享⼀下给⼤家。教程不仅是零基础,通俗易懂,⽽且⾮常风趣幽默,像看⼩说⼀样!觉得太⽜了,所以分享给⼤家。可以跳转到教程。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论