创建者模式(Builder)

创建者模式

建造者模式(Builder Pattern)使用多个简单的对象一步一步构建成一个复杂的对象。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。
一个 Builder 类会一步一步构造最终的对象。该 Builder 类是独立于其他对象的。

优点:

  1. 建造者独立,易扩展。
  2. 便于控制细节风险。

缺点:

  1. 产品必须有共同点,范围有限制。
  2. 如果内部变化复杂,会有很多的建造类。

使用场景:

  1. 通常用来将一个复杂的对象的构造过程分离, 让使用者可以==根据需要选择创建过程==.
  2. 当这个复杂的对象的构造包含很多可选参数时, 那Builder模式可以说是不二之选了.

类图

1762862-3acc88707702694e.png
5a4b6405e4b078cf1ed9caf3.png

实例代码

package com.jack.idea.test.designmode;

import java.util.Date;

/**
 * 创建者模式
 *
 * @author ljs.song
 * @date 2018-01-02 16:02
 */
public class BuilderPattern {
    public static void main(String[] args) {
        //大家的分享都有PPT文档,但是各自的格式都不一样,有的有署名,有的没有,哟肚饿有些时间,有的没有

        //小臣制作他的ppt
        SharePPT xiaoChen = new SharePPT.Builder()
                .userName("小臣")
                .date(new Date())
                .location("三楼接待室").build();
        System.out.println(xiaoChen);
        System.out.println("***********************");

        //小龙制作他的ppt
        SharePPT xiaolong = new SharePPT.Builder()
                .userName("小龙")
                .date(new Date())
                .shareContent("自动化测试").build();
        System.out.println(xiaolong);
        System.out.println("***********************");

        //小杰制作他的ppt
        SharePPT xiaojie = new SharePPT.Builder()
                .userName("小臣")
                .date(new Date())
                .location("火马会议室")
                .shareContent("设计模式")
                .comment("我是评论").build();
        System.out.println(xiaojie);
        System.out.println("***********************");
    }
}

/**
 * 分享PPT 组成
 */
class SharePPT{
    //分享人
    private String userName;
    //时间
    private Date date;
    //分享地点
    private String location;
    //分享主题
    private String shareContent;
    //分享备注讨论
    private String comment;

    public SharePPT(String userName, Date date, String location, String shareContent, String comment) {
        this.userName = userName;
        this.date = date;
        this.location = location;
        this.shareContent = shareContent;
        this.comment = comment;
    }

    public SharePPT(Builder builder) {
        this.userName = builder.userName;
        this.date = builder.date;
        this.location = builder.location;
        this.shareContent = builder.shareContent;
        this.comment = builder.comment;
    }

    public static class Builder{
        //分享人
        private String userName;
        //时间
        private Date date;
        //分享地点
        private String location;
        //分享主题
        private String shareContent;
        //分享备注讨论
        private String comment;

        //绑定分享人
        public Builder userName(String userName){
            this.userName = userName;
            return this;
        }

        public Builder date(Date date){
            this.date = date;
            return this;
        }

        public Builder location(String location){
            this.location = location;
            return this;
        }

        public Builder shareContent(String shareContent){
            this.shareContent = shareContent;
            return this;
        }

        public Builder comment(String comment){
            this.comment = comment;
            return this;
        }

        public SharePPT build(){
            return new SharePPT(this);
        }
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("SharePPT{");
        sb.append("userName='").append(userName).append('\'');
        if(date != null) {
            sb.append(", date=").append(date);
        }
        if(location != null) {
            sb.append(", location='").append(location).append('\'');
        }
        if(shareContent != null) {
            sb.append(", shareContent='").append(shareContent).append('\'');
        }
        if(comment != null) {
            sb.append(", comment='").append(comment).append('\'');
        }
        sb.append('}');
        return sb.toString();
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_34120274/article/details/87111185