组合模式

组合模式定义

  • 将对象组合成树形结构以表示"部分-整体"的层次结构,使得用户对单个对象和组合对象的使用具有一致性。

组合模式-场景?

场景一:表示对象的部分-整体结构时
场景二:从一个整体中能够独立部分功能或模块的场景

组合模式角色划分

角色一:抽象根节点(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类图结构

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 1、组合模式的定义 将对象组合成树形机构以表示”部分-整体“的层次结构,使得用户对单个对象和组合对象的使用具有一致...
    成绩是汗阅读 3,490评论 0 0
  • 何为组合模式? 组合模式让我们可以把相同基类型的对象组合到树状结构中,其中父节点包含同类型的子节点。换句话说,这种...
    泥孩儿0107阅读 2,166评论 0 0
  • 组合模式定义 将对象组合成树形结构以表示“部分-整体”的层次结构,使得用户对单个对象和组合对象的使用具有一致性 组...
    一毛钱阅读 1,657评论 0 0
  • 【学习难度:★★★☆☆,使用频率:★★★★☆】直接出处:组合模式梳理和学习:https://github.com/...
    BruceOuyang阅读 4,673评论 0 1
  • 简介 Compose objects into tree structures to represent part...
    Whyn阅读 12,426评论 1 5

友情链接更多精彩内容