参考Shiro的Builder设计DO2VO

DO的设计

package com.wjz.demo;

import java.util.Date;

public class BuilderDO {

    private Integer id;
    private String name;
    private Date addTime;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Date getAddTime() {
        return addTime;
    }

    public void setAddTime(Date addTime) {
        this.addTime = addTime;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((addTime == null) ? 0 : addTime.hashCode());
        result = prime * result + ((id == null) ? 0 : id.hashCode());
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        BuilderDO other = (BuilderDO) obj;
        if (addTime == null) {
            if (other.addTime != null) {
                return false;
            }
        } else if (!addTime.equals(other.addTime)) {
            return false;
        }
        if (id == null) {
            if (other.id != null) {
                return false;
            }
        } else if (!id.equals(other.id)) {
            return false;
        }
        if (name == null) {
            if (other.name != null) {
                return false;
            }
        } else if (!name.equals(other.name)) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "BuilderDO [id=" + id + ", name=" + name + ", addTime=" + addTime + "]";
    }

}

VO的设计

鲜明的区别就是日期不再是java.util.Date类型而是java.lang.String类型

其内部定义了静态内部类,作用就是根据注入的DO转换为VO

package com.wjz.demo;

import java.text.SimpleDateFormat;
import java.util.Date;

public class BuilderVO {

    private Integer id;
    private String name;
    private String addTime;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddTime() {
        return addTime;
    }

    public void setAddTime(String addTime) {
        this.addTime = addTime;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((addTime == null) ? 0 : addTime.hashCode());
        result = prime * result + ((id == null) ? 0 : id.hashCode());
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        BuilderVO other = (BuilderVO) obj;
        if (addTime == null) {
            if (other.addTime != null) {
                return false;
            }
        } else if (!addTime.equals(other.addTime)) {
            return false;
        }
        if (id == null) {
            if (other.id != null) {
                return false;
            }
        } else if (!id.equals(other.id)) {
            return false;
        }
        if (name == null) {
            if (other.name != null) {
                return false;
            }
        } else if (!name.equals(other.name)) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "BuilderVO [id=" + id + ", name=" + name + ", addTime=" + addTime + "]";
    }
    
    public static class Builder {

        private BuilderDO builderDO;

        public Builder() {
            this(new BuilderDO());
        }

        public Builder(BuilderDO builderDO) {
            super();
            if (builderDO == null) {
                throw new NullPointerException("The DO instance is necessary.");
            }
            this.builderDO = builderDO;
        }

        public BuilderDO getBuilderDO() {
            return builderDO;
        }

        public void setBuilderDO(BuilderDO builderDO) {
            this.builderDO = builderDO;
        }

        private BuilderVO transferDO2VO() {
            final BuilderVO builderVO = new BuilderVO();
            builderVO.setId(getBuilderDO().getId());
            builderVO.setName(getBuilderDO().getName());
            builderVO.setAddTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(getBuilderDO().getAddTime()));
            return builderVO;
        }

        public Builder id(Integer id) {
            builderDO.setId(id);
            return this;
        }

        public Builder name(String name) {
            builderDO.setName(name);
            return this;
        }

        public Builder addTime(Date addTime) {
            builderDO.setAddTime(addTime);
            return this;
        }

        public BuilderVO buildBuilderVO() {
            return transferDO2VO();
        }
    }
    
}

Builder的设计

其具备了DO的实例,默认构造器创建了DO,并且支持DO的各种属性的设置赋值,buildBuilderVO是组建VO,transferDO2VO则是将DO转换为VO

Demo

package com.wjz.demo;

import java.util.Date;

public class BuilderDemo {

    public static void main(String[] args) {
        BuilderDO builderDO = new BuilderDO();
        builderDO.setId(2);
        builderDO.setName("iss000");
        builderDO.setAddTime(new Date());
        
        BuilderVO builderVO = new BuilderVO.Builder()
                .id(1)
                .name("iss002")
                .addTime(new Date())
                .buildBuilderVO();
        
        BuilderVO builderVO2 = new BuilderVO.Builder(builderDO).buildBuilderVO();
        
        System.out.println(builderVO);
        System.out.println(builderVO2);
    }
    
}

但是这种设计并不推荐,因为这样一来每个DO类中都会有一个Builder类,类的数量翻了一倍。 

posted @ 2018-07-17 10:25  BINGJJFLY  阅读(309)  评论(0编辑  收藏  举报