当前位置:网站首页>建造者模式(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")
边栏推荐
- mysql is not an internal or external command, nor is it a runnable program or batch file to resolve
- Calendar类的习题
- 【OS】操作系统高频面试题英文版(1)
- Mysql 客户端常见异常分析
- Using custom annotations, statistical method execution time
- sql中 exists的用法
- Monstache执行Monstache - f配置。toml出错不存在处理器类型和名称(附件)(= parse_exc类型
- 冒泡排序、选择排序、插入排序、快速排序
- Volatility memory forensics - command shows
- Offensive and defensive world easy_web
猜你喜欢

2022CISCNmisc

sqli-labs shooting range SQL injection learning Less-1
Misc of CTF-Memory Analysis (Volatility)

oracle row to column, column to row summary

Jackson 序列化失败问题-oracle数据返回类型找不到对应的Serializer

Flink CDC implements Postgres to MySQL streaming processing transmission case

线程的5种状态

Mycat2.0搭建教程

【OS】操作系统高频面试题英文版(1)

C#预定义数据类型简介
随机推荐
The number of warehouse 】 data quality
The most powerful and most commonly used SQL statements in history
【OS】操作系统高频面试题英文版(1)
mysql不是内部或外部命令,也不是可运行的程序或批处理文件解决
Arthas command parsing (jvm/thread/stack/heapdump)
Flink-流/批/OLAP一体得到Flink引擎
十四、Kotlin进阶学习:一、内联函数 inline;二、泛型;三、泛型约束;四、子类与子类型;
misc-log analysis of CTF
正则表达式语法详解及实用实例
SQL Server database generation and execution of SQL scripts
Student management system
【零基础搞定C语言——导航汇总篇】
uni-app: about custom components, easycom specs, uni_modules, etc.
Volatility memory forensics - command shows
国内数字藏品交易平台开发市场会开放二级市场吗
Awd summary
史上超强最常用SQL语句大全
Detailed explanation of regular expression syntax and practical examples
MySQL - Multi-table query and case detailed explanation
C#中对委托的理解和使用