组合模式的解析-iOS

栏目: IOS · 发布时间: 7年前

内容简介:1、简单工厂模式、工厂模式、抽象工厂模式的解析-iOS2、建造者模式的解析-iOS3、单例模式的解析-iOS

组合模式的解析-iOS

其他 设计模式 的介绍

1、简单工厂模式、工厂模式、抽象工厂模式的解析-iOS 

2、建造者模式的解析-iOS 

3、单例模式的解析-iOS 

4、原型模式的解析-iOS 

5、代理模式的解析-iOS 

6、适配器模式的解析-iOS 

7、装饰器模式的解析-iOS 

8、外观模式的解析-iOS 

9、桥接模式的解析-iOS 

10、组合模式的解析-iOS

概率描述

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

实用场景

1.你想表示对象的部分-整体层次结构。 

2.你希望用户忽略组合对象与单个对象的不同,用户将统一地使用组合结构中的所有对象。百度百科 

如:文件夹的管理,树形结构的东西。

案例解析

我们数据结构里面学习的二叉树,就是部分-整体的模式,就是一个组合模式的结构。 

还有我们的平常工作的公司也是一个组合模式,比如,每个公司都是有不同的部门组合而成的,而每个部门是由不同的人组合而成的,这种结构也是“部分-整体”的层次结构。

公司的原型图如下: 


组合模式的解析-iOS

代码如下: 

先看我们的部分代码–就是我们的人员代码

//.h文件

#import

@interface Person : NSObject

@property(nonatomic,copy)NSString *name;

@property(nonatomic,copy)NSString *age;

@end



//.m文件

#import "Person.h"



@implementation Person



@end

部门的代码:

//.h文件

#import

#import "Person.h"



typedef enum {

recruitFunction,//招聘

exploitFunction,//开发

financeFunction,//财务

operationAndMaintenanceFunction,//运维



}functionType;



@interface Department : NSObject

@property(nonatomic,copy)NSString *departmentName;

@property(nonatomic,assign)functionType functions;



/*

*添加人员

*/


-(void)addPer:(Person *)person;

/*

*移除人员

*/


-(void)removePer:(Person *)person;



@end

//.m文件

#import "Department.h"

#import "Person.h"



@interface Department ()

@property (nonatomic,copy)NSMutableArray *perArray;

@end



@implementation Department

- (instancetype)init

{

self = [super init];

if (self) {

_perArray = [NSMutableArray arrayWithCapacity:10];

}

return self;

}

-(void)addPer:(Person *)person{

[_perArray addObject:person];

}

-(void)removePer:(Person *)person{

[_perArray removeObject:person];

}

-(NSString *)description{

for (Person*per in _perArray) {

NSLog(@"%@有 %@",self.departmentName,per.name);

}

return @"";

}

@end

公司的代码

//.h文件

#import



@interface Company : NSObject



@end

//.m文件

#import "Company.h"

#import "Person.h"

#import "Department.h"





@implementation Company

- (instancetype)init

{

self = [super init];

if (self) {

[self createCompany];

}

return self;

}

-(void)createCompany{

Department *departmentOne = [[Department alloc]init];

departmentOne.departmentName = @"招聘部门";

departmentOne.functions = recruitFunction;

for (int i = 0; i<2; i++) {

Person *per = [[Person alloc]init];

per.name = [NSString stringWithFormat:@"人员%d",i+1];

[departmentOne addPer:per];

}

[departmentOne description];



Department *departmentTwo = [[Department alloc]init];

departmentTwo.departmentName = @"研发部门";

departmentTwo.functions = recruitFunction;

for (int i = 2; i<5; i++) {

Person *per = [[Person alloc]init];

per.name = [NSString stringWithFormat:@"人员%d",i+1];

[departmentTwo addPer:per];

}

[departmentTwo description];



Department *departmentThree = [[Department alloc]init];

departmentThree.departmentName = @"财务部门";

departmentThree.functions = recruitFunction;

for (int i = 5; i<6; i++) {

Person *per = [[Person alloc]init];

per.name = [NSString stringWithFormat:@"人员%d",i+1];

[departmentThree addPer:per];

}

[departmentThree description];





Department *departmentFour = [[Department alloc]init];

departmentFour.departmentName = @"财务部门";

departmentFour.functions = recruitFunction;

for (int i = 6; i<8; i++) {

Person *per = [[Person alloc]init];

per.name = [NSString stringWithFormat:@"人员%d",i+1];

[departmentFour addPer:per];

}

[departmentFour description];



}

@end

调用公司的代码

    /************** 组合模式*************************/



    Company *company = [[Company alloc]init];

命令行的结果

2018-07-03 17:19:23.139459+0800 DesignDemo[6792:179813] 招聘部门有 人员1

2018-07-03 17:19:23.139646+0800 DesignDemo[6792:179813] 招聘部门有 人员2

2018-07-03 17:19:23.140479+0800 DesignDemo[6792:179813] 研发部门有 人员3

2018-07-03 17:19:23.140643+0800 DesignDemo[6792:179813] 研发部门有 人员4

2018-07-03 17:19:23.140780+0800 DesignDemo[6792:179813] 研发部门有 人员5

2018-07-03 17:19:23.141178+0800 DesignDemo[6792:179813] 财务部门有 人员6

2018-07-03 17:19:23.141612+0800 DesignDemo[6792:179813] 财务部门有 人员7

2018-07-03 17:19:23.141917+0800 DesignDemo[6792:179813] 财务部门有 人员8

优缺点

优点: 

1、组合模式解耦了程序和各元素内部结构,可以把复杂的程序像单个接口一样处理该程序 

2、高层模块调用简单 

3、节点自由增加 

缺点: 

1、违反了依赖倒置原则,而接口不应该依赖于具体的实现类。比如树形的结构的时候,其叶子和树叶的声明都是实现类。

总结

如果有写的不正确或者侵权的,希望大家给我提出来,我会及时修改。谢谢大家。

--------------------- 

作者:小雅_yyq 

原文:https://blog.csdn.net/u014644610/article/details/80902099 


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

部落:一呼百应的力量

部落:一呼百应的力量

高汀 (Godin.S.) / 刘晖 / 中信出版社 / 2009-7 / 26.00元

部落指的是任何一群人,规模可大可小,他们因追随领导、志同道合而相互联系在一起。人类其实数百万年前就有部落的出现,随之还形成了宗教、种族、政治或甚至音乐。 互联网消除了地理隔离,降低了沟通成本并缩短了时间。博客和社交网站都有益于现有的部落扩张,并促进了网络部落的诞生——这些部落的人数从10个到1000万个不等,他们所关注的也许是iPhone,或一场政治运动,或阻止全球变暖的新方法。 那么......一起来看看 《部落:一呼百应的力量》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具