• 构建者模式第二种方式


    /**
    * 建造者
    */
    public abstract class Builder {
    public abstract Builder buildA(String s);
    public abstract Builder buildB(String s);
    public abstract Builder buildC(String s);
    public abstract Product build();
    }

    public class ConcreteBuilder extends Builder {
    private Product product;
    public ConcreteBuilder() {
    product = new Product();
    }
    @Override
    public Builder buildA(String s) {
    product.setBuildA(s);
    return this;
    }
    @Override
    public Builder buildB(String s) {
    product.setBuildB(s);
    return this;
    }
    @Override
    public Builder buildC(String s) {
    product.setBuildC(s);
    return this;
    }
    @Override
    public Product build() {
    return product;
    }
    }


    public class Product {
    private String buildA="汉堡";
    private String buildB="饮料";
    private String buildC="薯条";
    public String getBuildA() {
    return buildA;
    }
    public void setBuildA(String buildA) {
    this.buildA = buildA;
    }
    public String getBuildB() {
    return buildB;
    }
    public void setBuildB(String buildB) {
    this.buildB = buildB;
    }
    public String getBuildC() {
    return buildC;
    }
    public void setBuildC(String buildC) {
    this.buildC = buildC;
    }
    @Override
    public String toString() {
    return "Product2{" +
    "buildA='" + buildA + '\'' +
    ", buildB='" + buildB + '\'' +
    ", buildC='" + buildC + '\'' +
    '}';
    }
    }

    public class TestProduct {
    public static void main(String[] args) {
    ConcreteBuilder concreteBuilder = new ConcreteBuilder();
    Product build = concreteBuilder.buildA("aa").buildB("bb").buildC("cc").build();
    // Product{buildA='aa', buildB='bb', buildC='cc'}
    System.out.println(build);
    }
    }

  • 相关阅读:
    「LibreOJ β Round #4」子集
    「LibreOJ β Round #4」框架
    「LibreOJ β Round #4」游戏
    [HNOI2008]GT考试
    [HNOI2008]水平可见直线
    UVA 1650 Number String
    [USACO14JAN]Recording the Moolympics
    UVA 1390 Interconnect
    UVA 12520 Square Garden
    [HNOI2008]神奇的国度
  • 原文地址:https://www.cnblogs.com/ladeng19/p/15839351.html
Copyright © 2020-2023  润新知