9.CompositePattern - QDDCoder/LZLearniOS GitHub Wiki

组合模式

解释地址:https://www.runoob.com/design-pattern/composite-pattern.html

组合模式Composite Pattern),又叫部分整体模式是用于把一组相似的对象当作一个单一的对象组合模式依据树形结构来组合对象用来表示部分以及整体层次这种类型的设计模式属于结构型模式它创建了对象组的树形结构这种模式创建了一个包含自己对象组的类该类提供了修改相同对象组的方式我们通过下面的实例来演示组合模式的用法实例演示了一个组织中员工的层次结构意图将对象组合成树形结构以表示"部分-整体"的层次结构组合模式使得用户对单个对象和组合对象的使用具有一致性主要解决它在我们树型结构的问题中模糊了简单元素和复杂元素的概念客户程序可以像处理简单元素一样来处理复杂元素从而使得客户程序与复杂元素的内部结构解耦何时使用1您想表示对象的部分-整体层次结构树形结构)。 2您希望用户忽略组合对象与单个对象的不同用户将统一地使用组合结构中的所有对象如何解决树枝和叶子实现统一接口树枝内部组合该接口关键代码树枝内部组合该接口并且含有内部属性 List里面放 Component优点1高层模块调用简单2节点自由增加缺点在使用组合模式时其叶子和树枝的声明都是实现类而不是接口违反了依赖倒置原则使用场景部分整体场景如树形菜单文件文件夹的管理注意事项定义时为具体类

1.创建 Employee 类,该类带有 Employee 对象的列表。

class Employee: NSObject {
    private var name:String?
    private var dept:String?
    private var salary:Int?
    private var subordinates:[Employee]?
    init(with name:String,with dept:String,with salary:Int) {
        super.init()
        self.name = name
        self.dept = dept
        self.salary = salary
        self.subordinates = [Employee]()
    }
    
    func add(with e:Employee)  {
        subordinates?.append(e)
    }
    
    func remove(with e:Employee)  {
        subordinates = subordinates?.filter({$0 != e})
    }
    
    func getSubordinates() -> [Employee]? {
        return subordinates
    }
    
    func toString() -> String{
        return "Employ:[Name:\(name),dept:\(dept),salary:\(salary)]"
    }
    
}
  • 测试

let CEO = Employee(with: "John", with: "CEO", with: 30000)
let headSales = Employee(with: "Robert", with: "Head Sales", with: 20000)
let headMarketing = Employee(with: "Michel", with: "Head Marketing", with: 20000)

let clerk1 = Employee(with: "Laura", with: "Marketing", with: 10000)
let clerk2 = Employee(with: "Bob", with: "Marketing", with: 10000)

let salesExecutive1 = Employee(with: "Richard", with: "Sales", with: 10000)
let salesExecutive2 = Employee(with: "Rob", with: "Sales", with: 10000)

CEO.add(with: headSales)
CEO.add(with: headMarketing)

headSales.add(with: salesExecutive1)
headSales.add(with: salesExecutive2)

headMarketing.add(with: clerk1)
headMarketing.add(with: clerk2)

//打印该组织的所有员工
print(CEO.toString())
if let headList =  CEO.getSubordinates(){
    for headEmployee in headList {
        print(headEmployee.toString())
        if let employeeList = headEmployee.getSubordinates() {
            for employee in employeeList {
                print(employee.toString())
            }
        }
    }
}
  • 结果

Employ:[Name:Optional("John"),dept:Optional("CEO"),salary:Optional(30000)]
Employ:[Name:Optional("Robert"),dept:Optional("Head Sales"),salary:Optional(20000)]
Employ:[Name:Optional("Richard"),dept:Optional("Sales"),salary:Optional(10000)]
Employ:[Name:Optional("Rob"),dept:Optional("Sales"),salary:Optional(10000)]
Employ:[Name:Optional("Michel"),dept:Optional("Head Marketing"),salary:Optional(20000)]
Employ:[Name:Optional("Laura"),dept:Optional("Marketing"),salary:Optional(10000)]
Employ:[Name:Optional("Bob"),dept:Optional("Marketing"),salary:Optional(10000)]
⚠️ **GitHub.com Fallback** ⚠️