组合模式定义
- 将对象组合成树形结构以表示"部分-整体"的层次结构,使得用户对单个对象和组合对象的使用具有一致性。
组合模式-场景?
场景一:表示对象的部分-整体结构时
场景二:从一个整体中能够独立部分功能或模块的场景
组合模式角色划分
角色一:抽象根节点(compoment)
角色二:具体子节点(composite)
角色三:太监节点(->没有儿子)Leaf
在swift用final定义只允许使用不允许被继承
提醒:在开发中?(菜单?)->UIView
iOS UI架构设计->组合模式灵活运用
抽象根节点->UIView
具体子节点->UITableView、UIImageView…
iOS开发太监View(可能会有)
注意:容器存储这些儿子,这是组合模式种核心特点
整体->UIView
部分->UIView子类
UIView有层次结构
UIView和UITableView基本使用类似的
场景:只允许使用,不允许继承
整体和部分是相对的(不是绝对的)->参照物
//使得用户对单个对象和组合对象的使用具有一致性。addSubview
let view = UIView(frame: self.view.frame)
view.addSubview(UIView(frame: self.view.frame))
let image = UIImageView(frame: self.view.frame)
view.addSubview(image)
组合模式原理案例
案例一:原始案例?
**角色一:根节点->ComponentProtocol**
//
// ComponentProtocol.swift
// Dream_20180718_Component
//
// Created by Dream on 2018/7/18.
// Copyright © 2018年 Tz. All rights reserved.
//
import Foundation
//根节点有几个特点需要注意(父节点根据你的设计决定)
protocol ComponentProtocol {
//特点一:节点名字
var name:String{get}
//特点二:子节点(数组)
var components:Array<ComponentProtocol>{get}
//特点三:业务逻辑
func doSomthing()
}
**角色二:具体子节点->Composite**
//
// Composite.swift
// Dream_20180718_Component
//
// Created by Dream on 2018/7/18.
// Copyright © 2018年 Tz. All rights reserved.
//
import UIKit
//具体子节点
class Composite: ComponentProtocol {
var name: String
var components:Array<ComponentProtocol>
init(name:String) {
self.name = name
self.components = Array<ComponentProtocol>()
}
func doSomthing() {
print("节点名称:\(self.name)")
}
//添加
func addChild(child:ComponentProtocol) {
self.components.append(child)
}
//删除
func removeChild(child:ComponentProtocol){
for index in 0...self.components.count - 1 {
if self.components[index].name == child.name {
self.components.remove(at: index)
break
}
}
}
//得到
func getChild(index:Int) -> ComponentProtocol {
return self.components[index]
}
//清空
func clear() {
self.components.removeAll()
}
}
角色三:叶子节点->太监->Leaf
//
// Leaf.swift
// Dream_20180718_Component
//
// Created by Dream on 2018/7/18.
// Copyright © 2018年 Tz. All rights reserved.
//
import UIKit
//角色三:叶子节点->太监->Leaf
//final表示不允许继承
final class Leaf: ComponentProtocol {
var name: String
var components:Array<ComponentProtocol>
init(name:String) {
self.name = name
self.components = Array<ComponentProtocol>()
}
func doSomthing() {
print("节点名称:\(self.name)")
}
}
案例二:改进案例?
角色一:根节点->ComponentProtocol
//
// ComponentProtocol.swift
// Dream_20180718_Component
//
// Created by Dream on 2018/7/18.
// Copyright © 2018年 Tz. All rights reserved.
//
import Foundation
//根节点有几个特点需要注意(父节点根据你的设计决定)
protocol ComponentProtocol2 {
//特点一:节点名字
var name:String{get}
//特点二:子节点(数组)
var components:Array<ComponentProtocol2>{get}
//特点三:业务逻辑
func doSomthing()
func addChild(child:ComponentProtocol2)
func removeChild(child:ComponentProtocol2)
func getChild(index:Int) -> ComponentProtocol2
func clear()
}
角色二:具体子节点->Composite
//
// Composite2.swift
// Dream_20180718_Component
//
// Created by Dream on 2018/7/18.
// Copyright © 2018年 Tz. All rights reserved.
//
import UIKit
class Composite2: ComponentProtocol2 {
var name: String
var components:Array<ComponentProtocol2>
init(name:String) {
self.name = name
self.components = Array<ComponentProtocol2>()
}
func doSomthing() {
print("节点名称:\(self.name)")
}
//添加
func addChild(child:ComponentProtocol2) {
self.components.append(child)
}
//删除
func removeChild(child:ComponentProtocol2){
for index in 0...self.components.count - 1 {
if self.components[index].name == child.name {
self.components.remove(at: index)
break
}
}
}
//得到
func getChild(index:Int) -> ComponentProtocol2 {
return self.components[index]
}
//清空
func clear() {
self.components.removeAll()
}
}
角色三:叶子节点->太监->Leaf
//
// Leaf2.swift
// Dream_20180718_Component
//
// Created by Dream on 2018/7/18.
// Copyright © 2018年 Tz. All rights reserved.
//
import UIKit
final class Leaf2: ComponentProtocol2 {
var name: String
var components:Array<ComponentProtocol2>
init(name:String) {
self.name = name
self.components = Array<ComponentProtocol2>()
}
//做异常处理
func doSomthing() {
print("节点名称:\(self.name)")
}
//添加
func addChild(child:ComponentProtocol2) {
print("发送错误,叶子节点没有子节点...")
}
//删除
func removeChild(child:ComponentProtocol2){
print("发送错误,叶子节点没有子节点...")
}
//得到
func getChild(index:Int) -> ComponentProtocol2 {
print("发送错误,叶子节点没有子节点...")
//抛异常
return self.components[index]
}
//清空
func clear() {
print("发送错误,叶子节点没有子节点...")
}
}
组合模式UML类图结构

网友评论