当前位置: 首页 > 面试题库 >

生成器模式:首选哪个变体?

东方华晖
2023-03-14
问题内容

我正在阅读《有效的Java》一书,并创建了一些笔记供以后参考,我遇到了Builder Pattern。

我很清楚它是什么以及应该如何使用它。在此过程中,我创建了构建器模式的两个示例变体。

在列出差异和每个优势时,我需要帮助吗?好吧,我当然注意到,Example 1通过允许更灵活地使用它,公开了更少的方法,限制更少,通用性更强。

请指出我错过的其他事情?

例子1

package item2;

/**
 * @author Sudhakar Duraiswamy
 *
 */
public  class Vehicle {

    private String type;
    private int wheels;

    interface Builder<T>{
        public  T build();
    }

    public static class CarBuilder implements Builder<Vehicle>{
        private String type;
        private int wheels;

        CarBuilder createVehicle(){
            this.type= "Car";
            return this;
        }

        CarBuilder addWheels(int wheels){
            this.wheels = wheels;
            return this;
        }

        public Vehicle build(){
            Vehicle v = new Vehicle();
            v.type = type;
            v.wheels = wheels;
            return v;
        }               
    }

    public static class TruckBuilder implements Builder<Vehicle>{       
        private String type;
        private int wheels;

        TruckBuilder createVehicle(){           
            this.type= "Truck";
            return this;
        }

        TruckBuilder addWheels(int wheels){
            this.wheels = wheels;
            return this;
        }

        public Vehicle build(){
            Vehicle v = new Vehicle();
            v.type = type;
            v.wheels = wheels;
            return v;
        }
    }

    public Vehicle(){

    }

    public static void main(String[] args) {
        //This builds a car with 4 wheels
        Vehicle car = new Vehicle.CarBuilder().createVehicle().addWheels(4).build();

        //THis builds a Truck with 10 wheels
        Vehicle truck = new Vehicle.TruckBuilder().createVehicle().addWheels(10).build();

    }
}

例子2

package item2;
/**
 * @author Sudhakar Duraiswamy
 *
 */
public  class Vehicle2 {

    private String type;
    private int wheels;

    interface Builder<T>{
        public  T build();      
        public String getType();
        public int getWheels() ;
    }

    public static class CarBuilder implements Builder<Vehicle2>{
        private String type;
        private int wheels;

        public String getType() {
            return type;
        }
        public int getWheels() {
            return wheels;
        }

        CarBuilder createVehicle(){
            this.type= "Car";
            return this;
        }

        CarBuilder addWheels(int wheels){
            this.wheels = wheels;
            return this;
        }

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

    public static class TruckBuilder implements Builder<Vehicle2>{      
        private String type;
        private int wheels;

        public String getType() {
            return type;
        }

        public int getWheels() {
            return wheels;
        }

        TruckBuilder createVehicle(){           
            this.type= "Truck";
            return this;
        }

        TruckBuilder addWheels(int wheels){
            this.wheels = wheels;
            return this;
        }

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


public Vehicle2(Builder<? extends Vehicle2> builder){
    Vehicle2 v = new Vehicle2();
    v.type = builder.getType();
    v.wheels = builder.getWheels();
}

    public Vehicle2(){
    }

    public static void main(String[] args) {            
        //This builds a car with 4 wheels
        Vehicle2 car = new Vehicle2.CarBuilder().createVehicle().addWheels(4).build();

        //THis builds a Truck with 10 wheels
        Vehicle2 truck = new Vehicle2.TruckBuilder().createVehicle().addWheels(10).build();
    }
}

问题答案:

以上都不是。

第一个不允许构建不可变的Vehicle,这通常就是为什么使用Builder模式的原因。

第二个示例是第一个示例的变体,它允许使用其他getter方法从构建器获取信息。但是这些方法没有在任何地方使用,除了Vehicle构造函数外,该构造函数可以直接访问构建器字段。我看不出添加它们的意义。

我认为还有两点需要改进:

  1. 两种构建器类型执行的操作完全相同。不需要两种类型。一个就足够了。
  2. 什么createVehicle()方法不应该由制造商构造函数来完成。如果您构建CarBuilder,显然是要构建汽车,因此应在构建构建器后立即设置车辆类型。这是我的写法:

public final class Vehicle {

    private final String type;
    private final int wheels;

    private Vehicle(Builder builder) {
        this.type = builder.type;
        this.wheels = builder.wheels;
    }

    public static Builder carBuilder() {
        return new Builder("car");
    }

    public static Builder truckBuilder() {
        return new Builder("truck");
    }

    public static class Builder {
        private final String type;
        private int wheels;

        private Builder(String type) {
            this.type = type;
        }

        public Builder addWheels(int wheels){
            this.wheels = wheels;
            return this;
        }

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

    public static void main(String[] args) {
        Vehicle car = Vehicle.carBuilder().addWheels(4).build();
        Vehicle truck = Vehicle.truckBuilder().addWheels(10).build();
    }
}


 类似资料:
  • 问题 你需要准备一个复杂的、多部分的对象,你希望操作不止一次或有不同的配置。 解决方案 创建一个生成器封装对象的产生过程。 Todo.txt 格式提供了一个先进的但还是纯文本的方法来维护待办事项列表。手工输入每个项目有损耗且容易出错,然而 TodoTxtBuilder 类可以解决我们的麻烦: class TodoTxtBuilder constructor: (defaultParamet

  • 亦称: 建造者模式、Builder 意图 生成器模式是一种创建型设计模式, 使你能够分步骤创建复杂对象。 该模式允许你使用相同的创建代码生成不同类型和形式的对象。 问题 假设有这样一个复杂对象, 在对其进行构造时需要对诸多成员变量和嵌套对象进行繁复的初始化工作。 这些初始化代码通常深藏于一个包含众多参数且让人基本看不懂的构造函数中; 甚至还有更糟糕的情况, 那就是这些代码散落在客户端代码的多个位置

  • 5. 生成器(Builder) Intent 封装一个对象的构造过程,并允许按步骤构造。 Class Diagram Implementation 以下是一个简易的 StringBuilder 实现,参考了 JDK 1.8 源码。 // java public class AbstractStringBuilder { protected char[] value; protec

  • 构建器模式使用简单对象并使用逐步方法构建复杂对象。 这种类型的设计模式属于创建模式,因为此模式提供了创建对象的最佳方法之一。 Builder类逐步构建最终对象。 此构建器独立于其他对象。 实现 (Implementation) 我们考虑过快餐店的商业案例,其中典型的餐点可以是汉堡包和冷饮。 汉堡可以是蔬菜汉堡或鸡肉汉堡,将由包装纸包装。 冷饮可以是可乐或百事可乐,也可以装在一个瓶子里。 我们将创建

  • 问题内容: 我的对象层次结构随着继承树的加深而增加了复杂性。这些都不是抽象的,因此,它们的所有实例都或多或少地达到了目的。 由于参数数量很多,我想使用“构建器模式”来设置属性,而不是对多个构造函数进行编码。由于我需要适应所有排列,因此继承树中的叶类将具有伸缩构造函数。 当我在设计过程中遇到一些问题时,我已经浏览了这里的答案。首先,让我给您一个简单的,简短的示例来说明问题。 现在我们有一些代码可以继

  • 一、简介  用于生成站点首页。 二、功能演示   如下图所示: 点击“生成首页”。 如提示成功则可完成修改操作,否则则提示错误消息。 成功信息:           同时你也可以通过:当前位置菜单栏——>生成首页;进行生成首页。