本文共 6729 字,大约阅读时间需要 22 分钟。
java 生成器 设计模式
Today we will look into Builder pattern in java. Builder design pattern is a creational design pattern like and .
今天,我们将研究Java中的Builder模式。 生成器设计模式是一种创新设计模式,如和 。
Builder pattern was introduced to solve some of the problems with Factory and Abstract Factory design patterns when the Object contains a lot of attributes.
当对象包含很多属性时,引入了构建器模式来解决Factory和Abstract Factory设计模式的一些问题。
There are three major issues with Factory and Abstract Factory design patterns when the Object contains a lot of attributes.
当对象包含很多属性时,工厂设计模式和抽象工厂设计模式存在三个主要问题。
We can solve the issues with large number of parameters by providing a constructor with required parameters and then different setter methods to set the optional parameters. The problem with this approach is that the Object state will be inconsistent until unless all the attributes are set explicitly.
通过为构造函数提供必需的参数,然后提供不同的setter方法来设置可选参数,我们可以解决大量参数的问题。 这种方法的问题在于,除非明确设置所有属性,否则对象状态将一直不一致 。
Builder pattern solves the issue with large number of optional parameters and inconsistent state by providing a way to build the object step-by-step and provide a method that will actually return the final Object.
Builder模式通过提供一种逐步构建对象并提供一种将实际返回最终Object的方法的方式,解决了具有大量可选参数和状态不一致的问题。
Let’s see how we can implement builder design pattern in java.
让我们看看如何在Java中实现构建器设计模式。
Computer
then builder class should be named as ComputerBuilder
. 首先,您需要创建一个 ,然后将所有参数从外部类复制到Builder类。 我们应该遵循命名约定,如果类名称为Computer
则构建器类应命名为ComputerBuilder
。 build()
method in the builder class that will return the Object needed by client program. For this we need to have a private constructor in the Class with Builder class as argument. 最后一步是在构建器类中提供build()
方法,该方法将返回客户端程序所需的Object。 为此,我们需要在Class中有一个带有Builder类作为参数的私有构造函数。 Here is the sample builder pattern example code where we have a Computer class and ComputerBuilder class to build it.
这是示例构建器模式示例代码,其中有一个Computer类和ComputerBuilder类来构建它。
package com.journaldev.design.builder;public class Computer { //required parameters private String HDD; private String RAM; //optional parameters private boolean isGraphicsCardEnabled; private boolean isBluetoothEnabled; public String getHDD() { return HDD; } public String getRAM() { return RAM; } public boolean isGraphicsCardEnabled() { return isGraphicsCardEnabled; } public boolean isBluetoothEnabled() { return isBluetoothEnabled; } private Computer(ComputerBuilder builder) { this.HDD=builder.HDD; this.RAM=builder.RAM; this.isGraphicsCardEnabled=builder.isGraphicsCardEnabled; this.isBluetoothEnabled=builder.isBluetoothEnabled; } //Builder Class public static class ComputerBuilder{ // required parameters private String HDD; private String RAM; // optional parameters private boolean isGraphicsCardEnabled; private boolean isBluetoothEnabled; public ComputerBuilder(String hdd, String ram){ this.HDD=hdd; this.RAM=ram; } public ComputerBuilder setGraphicsCardEnabled(boolean isGraphicsCardEnabled) { this.isGraphicsCardEnabled = isGraphicsCardEnabled; return this; } public ComputerBuilder setBluetoothEnabled(boolean isBluetoothEnabled) { this.isBluetoothEnabled = isBluetoothEnabled; return this; } public Computer build(){ return new Computer(this); } }}
Notice that Computer class has only getter methods and no public constructor. So the only way to get a Computer object is through the ComputerBuilder class.
请注意,Computer类仅具有getter方法,而没有公共构造函数。 因此,获取Computer对象的唯一方法是通过ComputerBuilder类。
Here is a builder pattern example test program showing how to use Builder class to get the object.
这是一个构建器模式示例测试程序,显示了如何使用Builder类来获取对象。
package com.journaldev.design.test;import com.journaldev.design.builder.Computer;public class TestBuilderPattern { public static void main(String[] args) { //Using builder to get the object in a single line of code and //without any inconsistent state or arguments management issues Computer comp = new Computer.ComputerBuilder( "500 GB", "2 GB").setBluetoothEnabled(true) .setGraphicsCardEnabled(true).build(); }}
Recently I uploaded a YouTube video for Builder Design Pattern. I have also explained why I think the builder pattern defined on is not a very good Object Oriented approach, and how we can achieve the same level of abstraction using different approach and with one class.
最近,我上传了一个关于Builder设计模式的YouTube视频。 我还解释了为什么我认为在定义的构建器模式不是一种非常好的面向对象的方法,以及我们如何使用不同的方法和一个类来实现相同的抽象级别。
Note that this is my point of view, I feel design patterns are to guide us, but ultimately we have to decide if it’s really beneficial to implement it in our project or not. I am a firm believer of .
请注意,这是我的观点,我认为设计模式可以指导我们,但最终我们必须决定在项目中实施它是否真的有益。 我坚信 。
If you like the video, please do share it, like it and subscribe to my channel. If you think I am mistaken or you have any comments or feedback so that I can improve my videos in future, please let me know through comments here or on YouTube video page.
如果您喜欢该视频,请共享它并订阅我的频道。 如果您认为我有误,或者有任何意见或反馈,以便将来可以改进自己的视频,请通过此处或YouTube视频页面上的评论让我知道。
Some of the builder pattern example in Java classes are;
Java类中的一些构建器模式示例为:
That’s all for builder design pattern in java.
这就是Java中的构建器设计模式。
翻译自:
java 生成器 设计模式
转载地址:http://wtlzd.baihongyu.com/