組合模式

組合模式定義

  • 將對(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)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1、組合模式的定義 將對(duì)象組合成樹形機(jī)構(gòu)以表示”部分-整體“的層次結(jié)構(gòu),使得用戶對(duì)單個(gè)對(duì)象和組合對(duì)象的使用具有一致...
    成績(jī)是汗閱讀 555評(píng)論 0 0
  • 何為組合模式? 組合模式讓我們可以把相同基類型的對(duì)象組合到樹狀結(jié)構(gòu)中,其中父節(jié)點(diǎn)包含同類型的子節(jié)點(diǎn)。換句話說,這種...
    泥孩兒0107閱讀 335評(píng)論 0 0
  • 組合模式定義 將對(duì)象組合成樹形結(jié)構(gòu)以表示“部分-整體”的層次結(jié)構(gòu),使得用戶對(duì)單個(gè)對(duì)象和組合對(duì)象的使用具有一致性 組...
    一毛錢閱讀 251評(píng)論 0 0
  • 【學(xué)習(xí)難度:★★★☆☆,使用頻率:★★★★☆】直接出處:組合模式梳理和學(xué)習(xí):https://github.com/...
    BruceOuyang閱讀 1,130評(píng)論 0 1
  • 簡(jiǎn)介 Compose objects into tree structures to represent part...
    Whyn閱讀 8,087評(píng)論 1 5

友情鏈接更多精彩內(nèi)容