“This is the 14th day of my participation in the November Gwen Challenge.The final text challenge in 2021”
Abstract
The generator pattern can be used in objects that have multiple parameters to configure. Initialization and configuration parameters can be separated to create objects step by step. You can also combine different configuration parameters to encapsulate a specific object initialization method.
Generators are a creative design pattern that allows you to create complex objects step by step. How do you understand complex objects?
For example, an object that needs to pass arguments when it is created can be said to be complex if it passes too many arguments (four or five or more arguments). How do you create it step by step?
Step by step, we can start from the following two aspects:
- The parameters that need to be transferred to create an object are divided into non-interfering functions, and the function is used to achieve the purpose of transferring parameters, which is temporarily called parameter function here.
- By combination, the parameter functions are combined into specific objects with different functions.
In Swift, to implement the generator pattern, you need to use the protocol approach. We can first declare parameter functions using protocol, which is equivalent to an interface class. Now create an interface class with three parameter functions:
protocol Builder{
func attributesMethod1(_ attr1: String)
func attributesMethod2(_ attr2: Bool)
func attributesMethod3(_ attr3: String)
}
Copy the code
Now create a concrete build class that complies with the Builder protocol and implements each of these three parameter functions.
class ContereBuilder: Builder { private var pro = Product() func reset() { pro = Product() } func attributesMethod1(_ attr1: String) { pro.attri1 = attr1 print("set attri1 complete") } func attributesMethod2(_ attr2: Bool) { pro.attri2 = attr2 print("set attri2 complete") } func attributesMethod3(_ attr3: String) { pro.attri3 = attr3 print("set attri3 complete") } func retrieveProduct() -> Product { let result = pro reset() return result } }Copy the code
The ContereBuilder class defines a private Product object. These three parameter functions are used to set parameters for the Product object. A reset() function is also implemented to recreate the object. You also create a function retrieveProduct that retrieveProduct the object, which can be handled according to its own needs without emulating this process. Here we also define the Product class and declare three variables corresponding to the parameter functions above.
class Product {
var attri1: String = ""
var attri2: Bool = false
var attri3: String = ""
}
Copy the code
BuildProA (_:) and buildProB(_:isTrue:) correspond to different concrete objects and configure the different parameters of the objects in the function. You can then generate different concrete objects with the absolute concrete business.
class Director { private var builder: Builder? func update(builder: Builder) { self.builder = builder } func buildProA(_ str: String) { builder? .attributesMethod1(str) } func buildProB(_ str: String, isTrue: Bool) { builder? .attributesMethod2(isTrue) builder? .attributesMethod3(str) } }Copy the code
At this point, all processing for the generator mode is done. The following code creates a concrete object for ProA, using the buildProA function. If you want to create other concrete objects, just use another function that builds them, such as the buildProB function.
class Client { static func someMethod(director: Director) { let builder = ContereBuilder() director.update(builder: Builder) // Specific configuration director.buildproa ("pro") // Return object let proA = Builder.retrieveProduct () print(proA)}}Copy the code
In addition to self-implemented functions that generate concrete objects, you can also use custom combinations of classes that comply with the Builder protocol.
/ / create a new concrete object builder. AttributesMethod1 (" 123 ") builder. AttributesMethod2 (true)Copy the code
The generator pattern can create objects step by step, delay the creation step, or run the creation step recursively (similar to a chained implementation). And you can reuse the same creation code to generate different types of concrete objects. This allows complex creation code to be handled separately from the business logic. But this pattern requires many more classes, so the overall complexity of the code increases.
digression
Design pattern is abstract, need more review, more practice, can be well absorbed. Therefore, this article will be updated from time to time, and strive to make the abstract more concrete. If you haven’t considered it, please leave a comment with me in the comments section.