美文网首页
组合模式

组合模式

作者: 架构师的一小步 | 来源:发表于2019-03-11 18:57 被阅读0次

组合模式定义

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

组合模式-场景?

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

组合模式角色划分

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

相关文章

  • 设计模式:组合模式 职责链模式

    组合模式 职责链模式 组合模式 组合模式将对象组合成树形结构,以表示“部分-整体”的层次结构。 在组合模式的树形结...

  • 第4章 结构型模式-组合模式

    一、组合模式简介 二、组合模式的优缺点 三、组合模式的使用场景 、组合模式的实例

  • 组合模式(统一叶子与组合对象)

    目录 从生活场景出发,映射组合模式 组合模式的理论概念 组合模式的实现 组合模式在源码中的应用 组合 “优于” 继...

  • 组合模式

    1. 组合模式 1.1 组合模式的定义 组合模式(Composite): 又称部分-整体模式, 将对象组合成树形结...

  • 组合模式

    设计模式系列7--组合模式 《Objective-c 编程之道 iOS 设计模式解析》 - 组合模式 常见组合模式...

  • 设计模式 | 组合模式及典型应用

    本文的主要内容: 介绍组合模式 示例 组合模式总结 源码分析组合模式的典型应用java.awt中的组合模式Java...

  • 组合模式

    一、组合模式介绍 二、组合模式代码实例

  • 组合模式

    设计模式之组合模式 什么是组合模式? 组合模式允许你将对象组合成树形结构来表现”部分-整体“的层次结构,使得客户以...

  • 15、组合模式(Composite Pattern)

    1. 组合模式 1.1 简介   Composite模式,即组合模式,又叫部分整体模式。Composite模式将对...

  • 组合模式原型解析

    组合模式定义: 组合模式,将对象组合成树形结构以表示“部分-整体”的层次结构,组合模式使得用户对单个对象和组合对象...

网友评论

      本文标题:组合模式

      本文链接:https://www.haomeiwen.com/subject/zgibpqtx.html