OC 类簇与复合

时间:2023-03-10 02:41:24
OC 类簇与复合

OC 类簇与复合


类簇:

类簇是Foundation框架中广泛使用的设计模式。类簇将一些私有的、具体的子类组合在一个公共的、抽象的超类下面,以这种方法来组织类可以简化一个面向对象框架的公开架构,而又不减少功能的丰富性。

简单的来说,NSString是个“工厂类”,然后它在外层提供了很多方法接口,但是这些方法的实现是由具体 的内部类来实现的。当使用NSString生成一个对象时,初始化方法会判断哪个“自己内部的类”最适合生成这个对象,然后这个“工厂”就会生成这个具体的类对象返回给你。这种又外层类提供统一抽象的接口,然后具体实现让隐藏的,具体的内部类来实现,在设计模式中称为“抽象工厂”模式。

类簇实现的类不适宜继承。(有关博客点击下面的连接)

IOS 杂笔-1(为什么不继承类簇?)

我在这里实现了一个简单的类簇:

//
// CXString.m
// 类簇
//
// Created by ma c on 16/5/11.
// Copyright © 2016年 xubaoaichiyu. All rights reserved.
// #import "CXString.h" @interface cx1 : CXString @end
@implementation cx1 - (void)method{ NSLog(@"%s",__func__);
} @end @interface cx2 : CXString @end
@implementation cx2 - (void)method{ NSLog(@"%s",__func__);
} @end @implementation CXString + (CXString *)CXString1{ cx1 * cx = [[cx1 alloc]init]; return cx;
} + (CXString *)CXString2{ cx2 * cx = [[cx2 alloc]init]; return cx;
} @end

上面的代码并不是很难读懂,就不做过多的解释.

下面我调用一下我所设计的'类簇'

//
// ViewController.m
// 类簇
//
// Created by ma c on 16/5/11.
// Copyright © 2016年 xubaoaichiyu. All rights reserved.
// #import "ViewController.h"
#import "CXString.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; CXString * cx1 = [CXString CXString1];
CXString * cx2 = [CXString CXString2]; [cx1 method];
[cx2 method]; } @end

输出结果

2016-05-11 18:01:14.643 类簇[3161:1545420] -[cx1 method]

2016-05-11 18:01:14.644 类簇[3161:1545420] -[cx2 method]

复合:

在OC的复合模式就是把其他对象作为自身的一部分,以提升自身的功能。换句话说复合是某一个复杂的类,需要由多个不同的类组成。

比如我现在要制作一台电脑,电脑需要CPU,显示器,鼠标和键盘等。这些东西的研发都是很复杂的过程。如果现在有成型的CPU等组件,就可以直接用这些组件攒一台电脑。复合模式就是这样。

所有我们在制作电脑前要先找到这些组件。这些组件并不需要我们来制作,有专业的公司提供这些成型的组件。

这里我做一个实例:电脑使用键盘和鼠标

//
// ViewController.m
// 复合
//
// Created by ma c on 16/5/11.
// Copyright © 2016年 xubaoaichiyu. All rights reserved.
// #import "ViewController.h"
#import "Computer.h"
#import "Keyboard.h"
#import "mouse.h"
@interface ViewController () @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; Computer * com = [[Computer alloc]init]; Keyboard * key = [[Keyboard alloc]init]; mouse * mou = [[mouse alloc]init]; com.keyboard = key;
com.mouse = mou; [com useMouse];
[com useKeyboard]; } @end
//
// Computer.h
// 复合
//
// Created by ma c on 16/5/11.
// Copyright © 2016年 xubaoaichiyu. All rights reserved.
// #import <Foundation/Foundation.h>
#import "Keyboard.h"
#import "mouse.h" @interface Computer : NSObject @property (nonatomic, strong) Keyboard * keyboard;
@property (nonatomic, strong) mouse * mouse; - (void) useKeyboard; - (void) useMouse; @end
//
// Computer.m
// 复合
//
// Created by ma c on 16/5/11.
// Copyright © 2016年 xubaoaichiyu. All rights reserved.
// #import "Computer.h" @implementation Computer - (void)useKeyboard { [self.keyboard test];
} - (void)useMouse{ [self.mouse test];
}
@end
//
// Keyboard.m
// 复合
//
// Created by ma c on 16/5/11.
// Copyright © 2016年 xubaoaichiyu. All rights reserved.
// #import "Keyboard.h" @implementation Keyboard - (void) test{
NSLog(@"I am a keyboard");
} @end
//
// mouse.m
// 复合
//
// Created by ma c on 16/5/11.
// Copyright © 2016年 xubaoaichiyu. All rights reserved.
// #import "mouse.h" @implementation mouse - (void)test{ NSLog(@"I am a mouse");
} @end

测试结果:

2016-05-11 18:31:28.434 复合[3189:1554979] I am a mouse

2016-05-11 18:31:28.435 复合[3189:1554979] I am a keyboard