类簇的设计

这儿为了验证Xcode上类簇NSNumber实现过程设计了一个简化版类簇LONumber

#import <Foundation/Foundation.h>

@interface LONumber : NSObject

@property ( readonly ) int intValue; // 获取对象对应的 int 数据
@property ( readonly ) BOOL boolValue; // 获取对象对应的 bool 数据
// 初始化
- (
instancetype )initWithInt:( int )value;
- (
instancetype )initWithBool:( BOOL )value;
// 便利构造器
+ (
instancetype )numberWithInt:( int )value;
+ (
instancetype )numberWithBool:( BOOL )value;

@end
#import "LONumber.h"
// 私有子类 __LOCFNumber
@interface __LOCFNumber : LONumber
{
   
int _intValue; // 保存对象的基本数据类型的数据
}
@end
@implementation __LOCFNumber

// 初始化
- (
instancetype )initWithInt:( int )value {
   
self = [ super init ];
   
if ( self ) {
       
_intValue = value;
    }
   
return self ;
}
//intValue 方法
- (
int )intValue {
   
return _intValue ;
}

- (
NSString *)description {
   
return [ NSString stringWithFormat : @"%d" , _intValue ];
}
@end


// 私有子类 __LOCfBoolean
@interface __LOCfBoolean : LONumber
{
   
BOOL _boolValue; // 保存布尔值
}
@end
@implementation __LOCfBoolean
- (
instancetype )initWithBool:( BOOL )value {
   
self = [ super init ];
   
if ( self ) {
       
_boolValue = value;
    }
   
return self ;
}
- (
BOOL )boolValue {
   
return _boolValue ;
}

-(
NSString *)description {
   
return _boolValue ? @"YES" : @"NO" ;
}
@end


// 私有子类 LOPlaceholderNumber
@interface LOPlaceholderNumber : LONumber

@end
@implementation LOPlaceholderNumber
- (
instancetype )init {
   
return nil ;
}
- (
instancetype )initWithInt:( int )value {
   
return ( id )[[ __LOCFNumber alloc ] initWithInt :value];
}


- ( instancetype )initWithBool:( BOOL )value {
   
return ( id )[[ __LOCfBoolean alloc ] initWithBool :value];
}
@end


@implementation LONumber // 抽象类 : 不能创建的对象

// 通过重写 alloc 方法 , alloc 开辟空间时指向其私有子类 LOPlaceholderNumber 类型 , 通过此子类开辟空间
+ (
instancetype )alloc {
   
// 如果调用 alloc 方法的类型不是 LONuber, 则表示不执行类簇创建对象的流程 , 使用根类的 alloc 方法直接为调用 alloc 方法的类型开辟空间
   
if ( self != LONumber . class ) {
       
return super . alloc ;
    }
   
// 声明静态的 LOPlaceholderNumber 类型的指针 , 保证只做一次初始化
   
static LOPlaceholderNumber *number = nil ;
   
if (!number) {
        number =
LOPlaceholderNumber . alloc ;
    }
   
return number;
}

// 初始化
//- (instancetype)init {
//    return nil;
//}

- (
instancetype )initWithInt:( int )value {
   
return nil ;
}
- (
instancetype )initWithBool:( BOOL )value {
   
return nil ;
}
// 便利构造器
+ (
instancetype )numberWithInt:( int )value {
   
return [[ self alloc ] initWithInt :value];
}
+ (
instancetype )numberWithBool:( BOOL )value {
   
return [[ self alloc ] initWithBool :value];
}

@end

#import <Foundation/Foundation.h>
#import
"LONumber.h"
int main( int argc, const char * argv[]) {
   
LONumber *p = [[ LONumber alloc ] initWithInt : 2 ];
   
NSLog ( @"%@" ,p);
   
NSLog ( @"%@" , NSStringFromClass (p. class ));
   
LONumber *q = [[ LONumber alloc ] initWithBool : YES ];
   
NSLog ( @"%@" ,q);
   
NSLog ( @"%@" , NSStringFromClass (q. class ));
   
return 0 ;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值