Wind hai: Causeway man, I recently bought a nice computer, do you want to see the configuration?
Gong: Come on, I’ll take a look.
Windy: Look at that.
type | configuration |
---|---|
The main body | Intel winner of 510 s |
memory | DDR4 |
Video interface | VGA/HDMI |
CPU | 4 core i3 |
The graphics card | integration |
The hard disk | Western Digital 7200rpm |
Input devices | Logitech mouse + hard drive |
display | Samsung XX model |
Gong: It feels good. Bring it back later.
Fenghai: Actually, THERE are some parameters that I haven’t put up yet. I wonder if one day I am responsible for writing code to deal with this input, wouldn’t it be necessary for me to build a Computer class that contains such a big chunk of data?
Gong: Aha, if your class really contains such a large chunk of data, it’s a classic complex build problem.
Windy Sea: Oh? So is there a good solution for building complex objects?
Gong: There is a design Pattern that is designed to solve this problem. It is called Builder Pattern, or generator Pattern, and there are some other translations, but the English name is the same, called Builder Pattern.
Windy sea: Speaking of creating objects, we talked about factory mode, the builder mode is also responsible for creating objects, what’s the difference between the two?
Causeway: There are differences, of course. The factory model addresses type diversity, while the Builder model addresses structural complexity within types.
Fenghai: Well, take the code as an example. If I write this Computer as a Computer class, and make personalized configuration according to the information provided by customers, how should I write this class?
Gong: I’ll start with the basics, like this:
class Computer {
var body: String = ""
var memory: String = ""
var description: String {
"body = \(body) memory = \(memory)"}}Copy the code
Windhai: Yes, you wrote a Computer class that provides two properties, and then you didn’t see a Builder.
Fenghai: Don’t worry. This is just the beginning. I’ll write down.
class Computer {
var body: String = ""
var memory: String = ""
var description: String {
"body = \(body) memory = \(memory)"
}
static func builder(a) -> Builder {
return Builder()}class Builder {
let computer = Computer(a)@discardableResult
func set(body: String) -> Builder {
computer.body = body
return self
}
@discardableResult
func set(memory: String) -> Builder {
computer.memory = memory
return self
}
func build(a) -> Computer {
return computer
}
}
}
Copy the code
Now we have defined a Computer class, which is created not by ourselves, but by a Builder class inside the Computer.
Windy: I see, so Builder mode requires complex classes to have a Builder inside them?
Gong: Oh, no, no, the design pattern doesn’t dictate where the Builder is. The design pattern just lays out the general idea, and the implementation is up to the developer based on the actual situation.
In this example, when we are creating the Computer object, we can write this
let builder = Computer.builder()
builder.set(body: "Intel winner of 510 s")
builder.set(memory: "DDR4")
let computer = builder.build()
print("\(computer.description)")
Copy the code
Fenghai: I see. At this time, the creation of Computer was delayed, and I left it to Builder to gradually build the attributes, and then directly create the Computer after it was built, which is good, but it is quite a lot of code.
Gong: Yes, so have you seen that the set code from Builder will return to Builder, just for convenience, in fact, Computer creation can also be written this way.
let computer = Computer.builder()
.set(body: "Intel winner of 510 s")
.set(memory: "DDR4")
.build()
print("\(computer.description)")
Copy the code
Windy: I see. It’s really much easier. So that @discardableresult is also set up for pure attribute Settings without getting a compiler warning if you don’t intend to receive a return value.
Gong: Yes, that’s right. We can extend it further, for example by building a Computer from Builder, and as builders grow, we can also make Builder templates, such as the following code:
class HPComputerBuilder: Computer.Builder {
override init(a) {
super.init(a)set(body: "HP Super Chip").set(memory: "DDR4")}}Copy the code
Fenghai: I see. This makes it very easy to directly build a custom Computer with a separate defined Builder class.
Gong: Yes. When needed, we write:
let hpComputer = HPComputerBuilder().build()
print("\(hpComputer.description)")
Copy the code
Windy: Well, we’ve talked so much that we don’t even have a definition of builder mode.
Gong: Yeah, leave the definition to last.
Builder Pattern (English: Builder Pattern) is a design Pattern, also known as construction Pattern, is an object construction Pattern. It can abstract the construction process of complex objects (abstract classes), so that different implementation methods of this abstract process can construct objects with different manifestations (attributes).
Fenghai: Ok, it’s three o ‘clock. Tea.
Gong: Oh yeah.