当前位置:网站首页>建造者模式(Swift 实现)
建造者模式(Swift 实现)
2022-07-30 05:43:00 【大头鑫】
Builder 建造者模式
Extracting the instance’s building code from the instance’s class, and put the code into a “Builder” object to do the build work. So we can build different complex instance by using different composition of the builder method. Besides, Add a “director” to manage the calling form client.
将对象的构造代码从对象的产品类代码中抽离出来,将这些代码放置在一个名为 “生成器” 的独立对象中。 所以,我们可以通过在建造方法中使用不同的组合,来创建不同的复杂实例。并且,增加一个 “指挥者” 来管理那些来自外界客户对调用。

// Director
class HouseDirector{
var builder: HouseBuilder
init (builder: HouseBuilder) {
self.builder = builder
}
func changeBuilder(bulider: HouseBuilder) {
self.builder = bulider
}
// The function "make" define the house must built by some componets.
func make(type: String) {
builder.reset()
if type == "simple" {
builder.buildA()
} else {
builder.buildB()
builder.buildC()
}
}
}
// Builder
protocol HouseBuilder {
func reset()
func buildA()
func buildB()
func buildC()
}
class Builder1: HouseBuilder {
private var result: Product1?
func reset() {
result = Product1()
}
func buildA() {
}
func buildB() {
}
func buildC() {
}
func getResult() -> Product1? {
return result
}
}
class Builder2: HouseBuilder {
private var result: Product2?
func reset() {
result = Product2()
}
func buildA() {
}
func buildB() {
}
func buildC() {
}
func getResult() -> Product2? {
return result
}
}
class Product1 {
}
class Product2 {
}
let b = Builder1()
let director = HouseDirector(builder: b)
director.make(type: "complex")
边栏推荐
- uni-app: about custom components, easycom specs, uni_modules, etc.
- MySQL index optimization and failure scenarios
- TDengineGUI cannot connect to TDengine
- [MATLAB] Image Processing - Recognition of Traffic Signs
- shardingsphere 分库分表及配置示例
- Awd summary
- Function functional interface and application
- SQL Server 数据库之生成与执行 SQL 脚本
- 二十二、Kotlin进阶学习:简单学习RecyclerView实现列表展示;
- C#预定义数据类型简介
猜你喜欢

2022CISCNmisc

十四、Kotlin进阶学习:一、内联函数 inline;二、泛型;三、泛型约束;四、子类与子类型;

C#下利用开源NPlot绘制股票十字交叉线

Online sql editing query tool sql-editor
![Monstache执行monstache -f config.toml出错No processor type exists with name [attachment] [type=parse_exc](/img/2d/50c9001125cd613087044d2b6c78b1.png)
Monstache执行monstache -f config.toml出错No processor type exists with name [attachment] [type=parse_exc

正则表达式语法详解及实用实例

【Spark】Spark 高频面试题英语版(1)

Use kotlin to extend plugins/dependencies to simplify code (after the latest version 4.0, this plugin has been deprecated, so please choose to learn, mainly to understand.)
Go简单实现协程池

Function 函数式接口及应用
随机推荐
十二、Kotlin进阶学习:一、Lambda 表达式;二、高阶函数;
Briefly describe SSRF
Trust anchor for certification path not found.异常解决方法。
Oracle数据库SQL优化详解
十五、Kotlin进阶学习:一、子类与子类型;二、协变;三、逆变;
Thread state of five
Volatility memory forensics - command shows
sqli-labs less3/4 Targeting Notes
Nacos配置中心用法详细介绍
MySQL special statement and optimizer
MySQL - 多表查询与案例详解
SQL Server database generation and execution of SQL scripts
FastAPI Quick Start
oracle row to column, column to row summary
The types of data structures and MySQL index
冒泡排序、选择排序、插入排序、快速排序
C#预定义数据类型简介
二十二、Kotlin进阶学习:简单学习RecyclerView实现列表展示;
批量自动归集
十三、Kotlin进阶学习:内联函数let、also、with、run、apply的用法。