組合模式定義
- 將對(duì)象組合成樹形結(jié)構(gòu)以表示"部分-整體"的層次結(jié)構(gòu),使得用戶對(duì)單個(gè)對(duì)象和組合對(duì)象的使用具有一致性。
組合模式-場(chǎng)景?
場(chǎng)景一:表示對(duì)象的部分-整體結(jié)構(gòu)時(shí)
場(chǎng)景二:從一個(gè)整體中能夠獨(dú)立部分功能或模塊的場(chǎng)景
組合模式角色劃分
角色一:抽象根節(jié)點(diǎn)(compoment)
角色二:具體子節(jié)點(diǎn)(composite)
角色三:太監(jiān)節(jié)點(diǎn)(->沒有兒子)Leaf
在swift用final定義只允許使用不允許被繼承
提醒:在開發(fā)中?(菜單?)->UIView
iOS UI架構(gòu)設(shè)計(jì)->組合模式靈活運(yùn)用
抽象根節(jié)點(diǎn)->UIView
具體子節(jié)點(diǎn)->UITableView、UIImageView…
iOS開發(fā)太監(jiān)View(可能會(huì)有)
注意:容器存儲(chǔ)這些兒子,這是組合模式種核心特點(diǎn)
整體->UIView
部分->UIView子類
UIView有層次結(jié)構(gòu)
UIView和UITableView基本使用類似的
場(chǎng)景:只允許使用,不允許繼承
整體和部分是相對(duì)的(不是絕對(duì)的)->參照物
//使得用戶對(duì)單個(gè)對(duì)象和組合對(duì)象的使用具有一致性。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)
組合模式原理案例
案例一:原始案例?
**角色一:根節(jié)點(diǎn)->ComponentProtocol**
//
// ComponentProtocol.swift
// Dream_20180718_Component
//
// Created by Dream on 2018/7/18.
// Copyright ? 2018年 Tz. All rights reserved.
//
import Foundation
//根節(jié)點(diǎn)有幾個(gè)特點(diǎn)需要注意(父節(jié)點(diǎn)根據(jù)你的設(shè)計(jì)決定)
protocol ComponentProtocol {
//特點(diǎn)一:節(jié)點(diǎn)名字
var name:String{get}
//特點(diǎn)二:子節(jié)點(diǎn)(數(shù)組)
var components:Array<ComponentProtocol>{get}
//特點(diǎn)三:業(yè)務(wù)邏輯
func doSomthing()
}
**角色二:具體子節(jié)點(diǎn)->Composite**
//
// Composite.swift
// Dream_20180718_Component
//
// Created by Dream on 2018/7/18.
// Copyright ? 2018年 Tz. All rights reserved.
//
import UIKit
//具體子節(jié)點(diǎn)
class Composite: ComponentProtocol {
var name: String
var components:Array<ComponentProtocol>
init(name:String) {
self.name = name
self.components = Array<ComponentProtocol>()
}
func doSomthing() {
print("節(jié)點(diǎn)名稱:\(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()
}
}
角色三:葉子節(jié)點(diǎn)->太監(jiān)->Leaf
//
// Leaf.swift
// Dream_20180718_Component
//
// Created by Dream on 2018/7/18.
// Copyright ? 2018年 Tz. All rights reserved.
//
import UIKit
//角色三:葉子節(jié)點(diǎn)->太監(jiān)->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("節(jié)點(diǎn)名稱:\(self.name)")
}
}
案例二:改進(jìn)案例?
角色一:根節(jié)點(diǎn)->ComponentProtocol
//
// ComponentProtocol.swift
// Dream_20180718_Component
//
// Created by Dream on 2018/7/18.
// Copyright ? 2018年 Tz. All rights reserved.
//
import Foundation
//根節(jié)點(diǎn)有幾個(gè)特點(diǎn)需要注意(父節(jié)點(diǎn)根據(jù)你的設(shè)計(jì)決定)
protocol ComponentProtocol2 {
//特點(diǎn)一:節(jié)點(diǎn)名字
var name:String{get}
//特點(diǎn)二:子節(jié)點(diǎn)(數(shù)組)
var components:Array<ComponentProtocol2>{get}
//特點(diǎn)三:業(yè)務(wù)邏輯
func doSomthing()
func addChild(child:ComponentProtocol2)
func removeChild(child:ComponentProtocol2)
func getChild(index:Int) -> ComponentProtocol2
func clear()
}
角色二:具體子節(jié)點(diǎn)->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("節(jié)點(diǎn)名稱:\(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()
}
}
角色三:葉子節(jié)點(diǎn)->太監(jiān)->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("節(jié)點(diǎn)名稱:\(self.name)")
}
//添加
func addChild(child:ComponentProtocol2) {
print("發(fā)送錯(cuò)誤,葉子節(jié)點(diǎn)沒有子節(jié)點(diǎn)...")
}
//刪除
func removeChild(child:ComponentProtocol2){
print("發(fā)送錯(cuò)誤,葉子節(jié)點(diǎn)沒有子節(jié)點(diǎn)...")
}
//得到
func getChild(index:Int) -> ComponentProtocol2 {
print("發(fā)送錯(cuò)誤,葉子節(jié)點(diǎn)沒有子節(jié)點(diǎn)...")
//拋異常
return self.components[index]
}
//清空
func clear() {
print("發(fā)送錯(cuò)誤,葉子節(jié)點(diǎn)沒有子節(jié)點(diǎn)...")
}
}
組合模式UML類圖結(jié)構(gòu)
