java使用建筑者模式按固定顺序构造对象(设计模式)

人生之路坎坎坷坷,跌跌撞撞在所难免。但是,不论跌了多少次,你都必须坚强勇敢地站起来。任何时候,无论你面临着生命的何等困惑抑或经受着多少挫折,无论道路多艰难,希望变得如何渺茫,请你不要绝望,再试一次,坚持到底,成功终将属于勇不言败的你。

导读:本篇文章讲解 java使用建筑者模式按固定顺序构造对象(设计模式),希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

模拟场景

把大象放进冰箱需要几步?
把大象放进冰箱共需要3步。

  1. 第一步:打开冰箱门;
  2. 第二步:把大象装进去;
  3. 第三步:关好冰箱门。

把长颈鹿放进冰箱需要几步?
把长颈鹿放进冰箱共需要4步。

  1. 第一步:打开冰箱门;
  2. 第二步:把大象取出来;
  3. 第三步:把长颈鹿装进去;
  4. 第四步:关好冰箱门。

代码实现

/**
 * 冰箱
 */
public class IceBox {

    /**
     * 里面装的
     */
    private String inside;

    public String getInside() {
        return inside;
    }

    public void setInside(String inside) {
        this.inside = inside;
    }
}
/**
 * 放冰箱
 */
public interface PutIceBoxBuilder {

    /**
     * 第一步:打开冰箱门
     */
    One open();

}
public interface One {

    /**
     * 第二步:把东西取出;
     */
    Two takeOut();

}
public interface Two {

    /**
     * 第三步:把东西装进去;
     */
    Three put(String inside);

}
public interface Three {

    /**
     * 第四步:关好冰箱门。
     */
    void close();

}
package com.zm.from;

import org.apache.commons.lang3.StringUtils;

/**
 * 进冰箱的方式
 * @author zhou
 */
public class PutIceBoxBuilderImpl implements PutIceBoxBuilder, One, Two,Three {

    private IceBox iceBox;

    public PutIceBoxBuilderImpl(IceBox iceBox) {
        this.iceBox = iceBox;
    }

    @Override
    public One open() {
        System.out.println("打开冰箱");
        return this;
    }

    @Override
    public Two takeOut() {
        if(StringUtils.isNotBlank(iceBox.getInside())){
            System.out.println("取出:"+ iceBox.getInside());
        }
        iceBox.setInside(null);
        return this;
    }

    @Override
    public Three put(String inside) {
        System.out.println("把"+inside+"塞进去");
        iceBox.setInside(inside);
        return this;
    }

    @Override
    public void close() {
        System.out.println("关冰箱");
    }


}

/**
 * 冰箱(建筑类)
 */
public interface IceBoxBuilder {

    /**
     * 放冰箱的步骤
     */
    PutIceBoxBuilder putIceBoxBuilder();

    /**
     * 构建出装有物品的冰箱
     */
    IceBox build();

}
package com.zm.from;

/**
 * @author zhou
 */
public class IceBoxBuilderImpl implements IceBoxBuilder {

    private IceBox iceBox = new IceBox();

    public IceBoxBuilderImpl() {

    }

    public IceBoxBuilderImpl(IceBox iceBox) {
        this.iceBox = iceBox;
    }

    @Override
    public PutIceBoxBuilder putIceBoxBuilder() {
        return new PutIceBoxBuilderImpl(iceBox);
    }

    @Override
    public IceBox build() {
        return iceBox;
    }
}

public class ExternalBuilderTest {

    public static void main(String[] args) {

        IceBoxBuilderImpl iceBoxBuilderImpl = new IceBoxBuilderImpl();
        iceBoxBuilderImpl
                .putIceBoxBuilder()
                .open()
                .takeOut()
                .put("大象")
                .close();
        IceBox iceBox = iceBoxBuilderImpl.build();
        System.out.println("冰箱里的东西是:"+iceBox.getInside());

        System.out.println("==========================");

        IceBoxBuilderImpl iceBoxBuilderImpl2 = new IceBoxBuilderImpl(iceBox);
        iceBoxBuilderImpl2
                .putIceBoxBuilder()
                .open()
                .takeOut()
                .put("长颈鹿")
                .close();
        iceBox = iceBoxBuilderImpl2.build();
        System.out.println("冰箱里的东西是:"+iceBox.getInside());


    }

}

输出结果
在这里插入图片描述

不按顺序构造是不能编译成功的
在这里插入图片描述
在这里插入图片描述

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/133882.html

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!