替代多个构造函数

时间:2019-03-21 15:39:45

标签: java android firebase android-room

我有这个构造函数...

public ShiftLog(String companyName, boolean workedForAgent, String agentName,
                    Date shiftStart, Date shiftEnd,
                    boolean breakTaken, Date breakStart,
                    Date breakEnd, boolean isTransportJob,
                    String transportCompanyName, String vehicleRegistration) {
        this.userUid = FirebaseAuth.getInstance().getCurrentUser().getUid();
        this.companyName = companyName;
        this.workedForAgent = workedForAgent;
        this.agentName = agentName;
        this.shiftStart = shiftStart;
        this.shiftEnd = shiftEnd;
        this.breakTaken = breakTaken;
        this.breakStart = breakStart;
        this.breakEnd = breakEnd;
        this.isTransportJob = isTransportJob;
        this.transportCompanyName = transportCompanyName;
        this.vehicleRegistration = vehicleRegistration;
    }

现在,我想添加一个移位日志(为用户实例化一个移位日志对象)。问题是移位日志可以有多种组合。例如,workedForAgent为假,则无需传递agentName。在存在多个可能的组合的情况下,如何不创建多个构造函数而做到这一点?例如,用户可以为代理工作,但不能休息,这意味着不需要传递中断开始时间和结束时间。但是,对于所有可能的组合,都需要大量的构造函数。还有其他选择吗?

我也正在使用会议室数据库附加所有这些信息。因此,例如,如果workedForAgent为false,则自动将agentName设置为null。那怎么办呢?

3 个答案:

答案 0 :(得分:2)

看看Builder patterns

  

Builder模式是一种创新的设计模式,这意味着它可以解决与对象创建相关的问题。

     

它通常解决面向对象编程中的问题,即确定要使用的构造函数。

答案 1 :(得分:1)

添加到@Kodiak 您只需单击几下就可以用builder替换您的构造函数 如此处https://www.jetbrains.com/help/idea/replace-constructor-with-builder.html

另外,最好的部分是,它将使用builder自动重构所有构造函数的出现

答案 2 :(得分:0)

简短答案:使用Getters / Setters

长答案:这里的另一种方法是,您可以实例化确定必须在构造函数中存在的变量,然后可以使用setter方法定义其他条件变量,并且可以轻松获取与吸气剂。

public class ShiftLog {

    private Object userUid;
    private String companyName;
    private boolean workedForAgent;
    private String agentName;
    private Date shiftStart;
    private Date shiftEnd;
    private boolean breakTaken;
    private Date breakStart;
    private Date breakEnd;
    private boolean isTransportJob;
    private String transportCompanyName;
    private String vehicleRegistration;

    public ShiftLog(String companyName, Date shiftStart, Date shiftEnd) {

        this.userUid = FirebaseAuth.getInstance().getCurrentUser().getUid();
        this.companyName = companyName;
        this.shiftStart = shiftStart;
        this.shiftEnd = shiftEnd;

    }

    public boolean isWorkedForAgent() {
        return workedForAgent;
    }

    public void setWorkedForAgent(boolean workedForAgent) {
        this.workedForAgent = workedForAgent;
    }

    public String getAgentName() {
        return agentName;
    }

    public void setAgentName(String agentName) {
        this.agentName = agentName;
    }

    public boolean isBreakTaken() {
        return breakTaken;
    }

    public void setBreakTaken(boolean breakTaken) {
        this.breakTaken = breakTaken;
    }

    public Date getBreakStart() {
        return breakStart;
    }

    public void setBreakStart(Date breakStart) {
        this.breakStart = breakStart;
    }

    public Date getBreakEnd() {
        return breakEnd;
    }

    public void setBreakEnd(Date breakEnd) {
        this.breakEnd = breakEnd;
    }

    public boolean isTransportJob() {
        return isTransportJob;
    }

    public void setTransportJob(boolean isTransportJob) {
        this.isTransportJob = isTransportJob;
    }

    public String getTransportCompanyName() {
        return transportCompanyName;
    }

    public void setTransportCompanyName(String transportCompanyName) {
        this.transportCompanyName = transportCompanyName;
    }

    public String getVehicleRegistration() {
        return vehicleRegistration;
    }

    public void setVehicleRegistration(String vehicleRegistration) {
        this.vehicleRegistration = vehicleRegistration;
    }
}