当前位置:网站首页>建造者模式(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")
边栏推荐
- JVM Learning (2) Garbage Collector
- 十三、Kotlin进阶学习:内联函数let、also、with、run、apply的用法。
- 在不同的服务器上基于docker部署redis主从同步
- 【调优】一个 Spark 任务某天突然变慢怎么解决
- C#中使用OleDb操作access数据库
- MySQL window function
- nodejs PM2监控及报警邮件发送(二)
- Defense Ideas for a Type of SMS Vulnerability
- 十六、Kotlin进阶学习:协程详细学习。
- Function functional interface and application
猜你喜欢

Using PyQt5 to add an interface to YoloV5 (1)

十七、Kotlin进阶学习:1、守护线程;2、线程和协程之间的效率对比;3、取消协程;

二十二、Kotlin进阶学习:简单学习RecyclerView实现列表展示;

Monstache执行Monstache - f配置。toml出错不存在处理器类型和名称(附件)(= parse_exc类型
phpok website vulnerability exploitation analysis

C#下大批量一键空投实现

Twenty-two, Kotlin advanced learning: simply learn RecyclerView to achieve list display;
![[MATLAB]图像处理——交通标志的识别](/img/45/1a5797a17ebf6db965a64c85e0f037.png)
[MATLAB]图像处理——交通标志的识别

Flink-流/批/OLAP一体得到Flink引擎

Mycat2.0 build tutorial
随机推荐
awd --waf deployment
mysql删除表中重复数据,(只保留一行)
Nacos配置中心用法详细介绍
Briefly describe SSRF
sql concat()函数
C#下大批量一键空投实现
MySQL 5.7 installation tutorial (all steps, nanny tutorials)
冒泡排序、选择排序、插入排序、快速排序
Obtain geographic location and coordinates according to ip address (offline method)
FastAPI Quick Start
[MATLAB]图像处理——交通标志的识别
十八、Kotlin进阶学习:1、挂起函数执行的顺序;2、使用 async 和 await 异步执行挂起函数;3、协程的调度器;4、父子协程;
C#利用开源NPlot实现K线图(蜡烛图)
批量自动归集
The most powerful and most commonly used SQL statements in history
Arthas command parsing (watch/tt/sc)
Defense Ideas for a Type of SMS Vulnerability
GraphQL (1) Basic introduction and application examples
ClickHouse查询语句详解
【数仓】数据仓库高频面试题题英文版(1)