组合模式:将对象组合成树形结构以表示“部分-整体”的层次结构。组合模式使得用户对单个对象和组合对象的使用具有一致性。
优点:
1.能够将简单的具有相同行为的对象组合在一起,实现复杂的行为。
2.添加叶节点行为简单,扩张性好。
3.组合模式可以一致的使用组合结构和单个对象。
缺点:
1.如果叶节点数据太大会造成维护成本太高。
总结:需求中说体现部分与整体层次的结构时,以及你希望用户可以忽略组合对象与单个对象的不同,统一地使用组合结构中的所有对象时,就应该考虑用组合模式,组合模式适合小面积使用,如果叶节点过多的话,就要考虑其他模式,否则维护起来会越来越复杂。
项目地址:https://github.com/GameRisker/Study-Design-Patterns.git
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
// // main.m // Composite // // Created by GameRisker on 16/10/16. // Copyright (c) 2016年 GameRisker. All rights reserved. // #import "Company.h" #import "ConcreteCompany.h" #import "FinanceDepartment.h" #import "HRDepartment.h" #import <Foundation/Foundation.h> int main(int argc, const char *argv[]) { @autoreleasepool { // insert code here... ConcreteCompany *concrete = [[ConcreteCompany alloc] init]; id hr = [[HRDepartment alloc] initWithName:@"北京总公司"]; id finance = [[FinanceDepartment alloc] initWithName:@"北京总公司"]; [concrete Add:hr]; [concrete Add:finance]; hr = [[HRDepartment alloc] initWithName:@"深圳子公司"]; finance = [[FinanceDepartment alloc] initWithName:@"深圳子公司"]; [concrete Add:hr]; [concrete Add:finance]; hr = [[HRDepartment alloc] initWithName:@"上海子公司"]; finance = [[FinanceDepartment alloc] initWithName:@"上海子公司"]; [concrete Add:hr]; [concrete Add:finance]; hr = [[HRDepartment alloc] initWithName:@"成都子公司"]; finance = [[FinanceDepartment alloc] initWithName:@"成都子公司"]; [concrete Add:hr]; [concrete Add:finance]; [concrete Display]; } return 0; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
// // ICompany.h // // // Created by GameRisker on 16/10/16. // // #import <Foundation/Foundation.h> @protocol ICompany <NSObject> - (instancetype)initWithName:(NSString *)name; - (void)Display; @end |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
// // Company.h // Composite // // Created by GameRisker on 16/10/16. // Copyright (c) 2016年 GameRisker. All rights reserved. // #import "ICompany.h" @interface Company : NSObject <ICompany> @property(copy, nonatomic) NSString *name; - (void)Add:(Company *)c; - (void)Remove:(Company *)c; @end |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
// // Company.m // // // Created by GameRisker on 16/10/16. // // #import "Company.h" #import "ICompany.h" #import <Foundation/Foundation.h> @implementation Company - (instancetype)initWithName:(NSString *)name { if (self = [super init]) { self.name = name; } return self; } - (void)Display { } - (void)Add:(Company *)c { } - (void)Remove:(Company *)c { } @end |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
// // ConcreteCompany.h // Composite // // Created by GameRisker on 16/10/16. // Copyright (c) 2016年 GameRisker. All rights reserved. // #import "ICompany.h" #import <Foundation/Foundation.h> @interface ConcreteCompany : NSObject <ICompany> @property(nonatomic, strong) NSMutableArray<ICompany> *children; - (void)Add:(Company *)c; - (void)Remove:(Company *)c; - (void)Display; @end |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
// // ConcreteCompany.m // // // Created by GameRisker on 16/10/16. // // #import "Company.h" #import "ConcreteCompany.h" #import "ICompany.h" @implementation ConcreteCompany - (NSMutableArray *)children { if (!_children) { _children = [NSMutableArray arrayWithCapacity:6]; } return _children; } - (void)Add:(Company *)c { [self.children addObject:c]; } - (void)Remove:(Company *)c { [self.children removeObject:c]; } - (void)Display { for (Company *company in _children) { [company Display]; } } @end |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
// // HRDepartment.h // Composite // // Created by GameRisker on 16/10/16. // Copyright (c) 2016年 GameRisker. All rights reserved. // #import "Company.h" #import <Foundation/Foundation.h> @interface HRDepartment : Company - (void)Display; @end |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
// // HRDepartment.m // // // Created by GameRisker on 16/10/16. // // #import "Company.h" #import "HRDepartment.h" #import <Foundation/Foundation.h> @implementation HRDepartment - (void)Display { NSLog(@"i am HR , i am in %@", self.name); } @end |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
// // FinanceDepartment.h // Composite // // Created by GameRisker on 16/10/16. // Copyright (c) 2016年 GameRisker. All rights reserved. // #import "Company.h" #import <Foundation/Foundation.h> @interface FinanceDepartment : Company - (void)Display; @end |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
// // FinanceDepartment.m // // // Created by GameRisker on 16/10/16. // // #import "Company.h" #import "FinanceDepartment.h" #import <Foundation/Foundation.h> @implementation FinanceDepartment - (void)Display { NSLog(@"i am Finance , i am in %@", self.name); } @end |