隐藏实现细节

类比UIButton的创建,创建按钮时:

UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
复制代码

按钮类型枚举:

typedef NS_ENUM(NSInteger, UIButtonType) {
     UIButtonTypeCustom = 0,                         // no button type
     UIButtonTypeSystem NS_ENUM_AVAILABLE_IOS(7_0),  // standard system button
     
     UIButtonTypeDetailDisclosure,
     UIButtonTypeInfoLight,
     UIButtonTypeInfoDark,
     UIButtonTypeContactAdd,
     
     UIButtonTypeRoundedRect = UIButtonTypeSystem,   // Deprecated, use UIButtonTypeSystem instead
     };
复制代码

我们来类比UIButton

Test:

#import <Foundation/Foundation.h>

typedef NS_ENUM(NSUInteger, CaiTestType) {
    TestTypeOne,
    TestTypeTwo,
    TestTypeThree
};

@interface Test : NSObject

+ (Test *)createTest:(CaiTestType)type;

- (void)doSomething;

@end



#import "Test.h"
#import "TestOne.h"
#import "TestTwo.h"
#import "TestThree.h"

@implementation Test

+ (Test *)createTest:(CaiTestType)type
{
    switch (type) {
        case TestTypeOne:
            return [TestOne new];
            break;
        case TestTypeTwo:
            return [TestTwo new];
            break;
        case TestTypeThree:
            return [TestThree new];
            break;
        default:
            break;
    }
}

- (void)doSomething
{
    //子类实现
}

@end
复制代码

TestOne TestTwo TestThree

#import "TestOne.h"

@implementation TestOne

- (void)doSomething
{
    NSLog(@"---TestOne doSomething");
}

@end


#import "TestTwo.h"

@implementation TestTwo

- (void)doSomething
{
    NSLog(@"---TestTwo doSomething");
}

@end


#import "TestThree.h"

@implementation TestThree

- (void)doSomething
{
    NSLog(@"---TestThree doSomething");
}

@end

复制代码

ViewController

#import "ViewController.h"
#import "Test.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    self.view.backgroundColor = [UIColor orangeColor];
    
    Test *testOne = [Test createTest:TestTypeOne];
    [testOne doSomething];
    
    Test *testTwo = [Test createTest:TestTypeTwo];
    [testTwo doSomething];
    
    Test *testThree = [Test createTest:TestTypeThree];
    [testThree doSomething];
    
    Test *test = [Test createTest:TestTypeThree];
    [test doSomething];
}

@end
复制代码

打印结果:

2017-04-07 15:01:39.293 Test[58960:2605033] ---TestOne doSomething
2017-04-07 15:01:39.293 Test[58960:2605033] ---TestTwo doSomething
2017-04-07 15:01:39.293 Test[58960:2605033] ---TestThree doSomething
2017-04-07 15:01:39.294 Test[58960:2605033] ---TestThree doSomething
复制代码
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值