当前位置:网站首页>组合模式:Swift 实现
组合模式:Swift 实现
2022-08-10 10:12:00 【大头鑫】
组合模式 Composite
We want to make the client using the Container or Leafs in a same calling way, so we set the leaf and the container that implement the same interface (Component). And the Container contains some children. In this way we create a model like tree, the client can treat every node similarly.
为了使客户端能够使用相同的方式调用 “容器” 和 “叶子”,我们让容器和叶子都去实现同一个接口 “组件”。并且容器中包含有一些子的容器。通过这种方式,我们可以创建一个类似于一棵树的模型,客户端对每个结点使用相同的对待方式。

// interface
protocol Component {
func execute()
}
class Leaf: Component {
func execute() {
print("I'm leaf")
}
}
class SubLeaf: Leaf {
override func execute() {
print("I'm sub leaf")
}
}
// Composite
class Containner:Component {
private var children: [Component] = []
func add(c: Component) {
children.append(c)
}
func remove(c: Component) {
}
func execute() {
for item in children {
item.execute()
}
}
}
// We treat the leaf and the containner as the same type of node.
let leaf = Leaf()
let leafTwo = Leaf()
let subLeaf = SubLeaf()
let c = Containner()
c.add(c: leaf)
c.add(c: leafTwo)
let root = Containner()
root.add(c: c)
root.add(c: subLeaf)
c.execute()
root.execute()

如上图:
- root 持有 c 的引用,c 持有 leaf 和 leafTwo 的引用。root 和 c 是 Container 类型,leafTwo 是 Leaf 类型。
- 容器和叶子都遵守了 “组件” 协议,所以都应当有 execute 方法,只是实现方式不同(容器是访问所有子结点,叶子是只访问当前)。并且,容器和叶子可以在需要的时候,都视作 “组件” 来使用。
边栏推荐
- Which is the strongest workflow engine for "Technology Selection"?Chief Architecture Helps You Pick
- lua初学
- C#List的使用以及Linq的使用
- ESP8266 Tutorial 1 - Introduction to ESP8266 Hardware Platform
- 「数据战略」结果驱动的企业数据策略:组织和治理
- 面试官:项目中 Dao、Service、Controller、Util、Model 怎么划分的?
- 多租户技术
- Load balancing principle analysis and source code interpretation
- ZZULIOJ 1124: 两个有序数组合并
- ELK入门
猜你喜欢

跨公网环境,路由策略,进行设备的访问

Situation丨The intrusion of hackers intensifies, and the shooting range sets up a "defense shield" for network security

MySQL 执行查询流程

MongoDB数据库笔记

LCD模块如何建立联系分析

首次入选OSDI顶会!腾讯提出超大规模推荐系统的模型低延时更新方案

leetcode:334. 递增的三元子序列

【微服务架构】微服务与SOA架构(2)
![[Azure Cloud] What is the difference between a service endpoint and a private link?point of view (1)](/img/f6/561a5ff4e518d423cc13a58dcc4383.png)
[Azure Cloud] What is the difference between a service endpoint and a private link?point of view (1)

"Time Series Database" uses cassandra to scan time series data
随机推荐
讯飞翻译机抢镜背后,跨语种沟通迈入全新时代
Thrift -- 跨语言RPC 框架
3D旋转文本动画js特效
Redis6 (1) - Introduction to NoSQL Database and Installation of Redis
交换 生成树 知识总结
PPT | 「课件」企业中高层人员安全管理培训(118页)
VBA: Inputbox Function and Inputbox Method
CentOS和Ubantu的Mysql主从配置
LiveGBS操作日志页面快速的筛选上级平台的调用记录直播观看录像回看等操作
Text selection rounded style border-radius
裸辞→自我放松→闭关→复习→斩获Offer
LeetCode Algorithm 1721. 交换链表中的节点
「数据战略」结果驱动的企业数据策略:组织和治理
dedecms supports one-click upload of Word content
态势丨黑客侵扰加剧,靶场为网络安全架设“防御盾”
Dalian University of Technology & Pengcheng & UAE propose a mixed-scale triple network ZoomNet for camouflaged target detection, with SOTA performance!
Redis6(一)——NoSQL数据库简介与Redis的安装
VBA: 采用Combox控件实现二级下拉菜单功能
对话陈赐靓:哪吒要让高端产品大众化
what is bsp in rtems