Building Complex Objects with Ease: An Introduction to the Builder Pattern in Go
Contact Author@:Medium,LinkedIn,Twitter
Hello, here is Wesley, Today’s article is about builder pattern in Go. Without further ado, let’s get started.💪
In golang,design patterns are a way to implement software engineering best practices. Among them, Builder Pattern is a creational design pattern that creates complex objects step by step, while avoiding situations where constructors have too many parameters.
Core Concepts of the Builder Pattern
- Intent: Separate the construction process of complex objects from their representation, allowing for different representations to be created using the same construction process.
- Applicability:
- When creating complex objects requires a step-by-step approach.
- When constructors have too many parameters or parameter combinations make object creation complicated.
- Advantages:
- Separate object creation and representation, conforming to the single responsibility principle.
- More flexible and easier to extend.
- Disadvantages:
- Increase class complexity.
- May be slightly more cumbersome for creating simple objects.
Structure of the Builder Pattern
The Builder pattern primarily consists of the following components:
- Product: The complex object to be created.
- Builder: Defines the interface for creating objects, including all construction steps.
- ConcreteBuilder: Concretely implements the Builder interface, completing each part of the construction process.
- Director: Guides the construction process, defining the order in which the object is created.
Implementation in Go
Here’s an example of implementing the Builder pattern in Go, using a computer assembly as an example:
1. Define the product structure
1 | // Computer structure, describing the important components of a computer |
2. Define the Builder interface
1 | // Builder interface, defining methods for generating instances |
3. Implementing Concrete Builders
Office Model Builder
1 | type OfficeModelBuilder struct { |
Game Model Builder
1 | type GameModelBuilder struct { |
4. Director Implementation
1 | type Director struct{} |
5. Test Code
1 | func main() { |
6. Thoughts on implementing Go’s builder pattern
The implementation above references Java’s implementation method, but Go should have its own characteristics. Go Is Not Java
- Removing
Director
:
In Go’s implementation,Director
is not necessary. Placing the construction logic directly in the builder function (OfficeComputerBuilder
andGameComputerBuilder
) is more in line with Go’s simple and direct style. - Method chaining:
TheGenericBuilder
uses method chaining (methods return themselves), making it easy to build objects step by step. - Clearer responsibility division:
GenericBuilder
is a general builder, while specific builders likeOfficeComputerBuilder
andGameComputerBuilder
are responsible for generating specific computer configurations. - Avoiding over-design:
Go advocates simplicity, so there’s no need to force the use of complex design patterns like Java.
You can also refer to the article: Design patterns in Golang — The Builder | by Surya Reddy | Medium
Summary
- The Builder pattern is typically used in scenarios where there are multiple constructor parameters or a large number of build steps involved. Using the Builder pattern can simplify the number of constructor parameters, making the building process more organized.
- It is possible to provide multiple different implementations for the same product. For example, in the above code, two different implementations were created for the
Computer
type: an office computer built throughNewOfficeModelBuilder
, and a game computer built throughNewGameModelBuilder
. - It can be applied to scenarios where the building process is not allowed to be interrupted. Still referring to the previous code, the
Computer
type object either completes or does not exist at all, without intermediate states, because the structdirector
encapsulates the corresponding process, and intermediate states only exist in ConcreteBuilder.
More Series Articles about You Should Know In Golang:
https://wesley-wei.medium.com/list/you-should-know-in-golang-e9491363cd9a
And I’m Wesley, delighted to share knowledge from the world of programming.
Don’t forget to follow me for more informative content, or feel free to share this with others who may also find it beneficial. it would be a great help to me.
Give me some free applauds, highlights, or replies, and I’ll pay attention to those reactions, which will determine whether or not I continue to post this type of article.
See you in the next article. 👋
中文文章: https://programmerscareer.com/zh-cn/golang-builder-pattern/
Author: Medium,LinkedIn,Twitter
Note: Originally written at https://programmerscareer.com/golang-builder-pattern/ at 2024-11-19 00:35.
Copyright: BY-NC-ND 3.0
Comments