组合模式的解析-iOS

组合模式的解析-iOS

其他设计模式的介绍

1、简单工厂模式、工厂模式、抽象工厂模式的解析-iOS
2、建造者模式的解析-iOS
3、单例模式的解析-iOS
4、原型模式的解析-iOS
5、代理模式的解析-iOS
6、适配器模式的解析-iOS
7、装饰器模式的解析-iOS
8、外观模式的解析-iOS
9、桥接模式的解析-iOS
10、组合模式的解析-iOS

概率描述

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

实用场景

1.你想表示对象的部分-整体层次结构。
2.你希望用户忽略组合对象与单个对象的不同,用户将统一地使用组合结构中的所有对象。百度百科
如:文件夹的管理,树形结构的东西。

案例解析

我们数据结构里面学习的二叉树,就是部分-整体的模式,就是一个组合模式的结构。
还有我们的平常工作的公司也是一个组合模式,比如,每个公司都是有不同的部门组合而成的,而每个部门是由不同的人组合而成的,这种结构也是“部分-整体”的层次结构。
公司的原型图如下:
这里写图片描述
代码如下:
先看我们的部分代码–就是我们的人员代码

//.h文件
#import <Foundation/Foundation.h>
@interface Person : NSObject
@property(nonatomic,copy)NSString *name;
@property(nonatomic,copy)NSString *age;
@end

//.m文件
#import "Person.h"

@implementation Person

@end

部门的代码:

//.h文件
#import <Foundation/Foundation.h>
#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 <Foundation/Foundation.h>

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

总结

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值