做一个自定义的封装类,方便扩展

来源:互联网 发布:淘宝皇冠店年入多少 编辑:程序博客网 时间:2024/05/17 14:16

当程序书写到一定量的时候,毕竟要进行封装,重构。好处多多啊。

1.代码逻辑结构更加清晰。

2.冗余代码量减少,更加清爽。

3.维护量也较低,符合"对扩展开放,对修改关闭"的软件架构思想。

等等。


于是,思考自己写一些封装类,能够跟服务器端的数据能够更好的衔接。


一般情况下,自己定以的类结构,分为两部分。

1.Model类,跟服务器字段,和程序字段相统一。

2.Server类(也可以叫Helper类),该Model类的操作类,来满足响应的数据操作。


废话不多说了,直接上小的Demo。中间做了详细注释,也不用讲太多。


1.Model类,跟服务器字段,和程序字段相统一。

*.h文件

#import


@interface NewVideoModel :NSObject{

    int vID;

    NSString *vName;

    NSString *vImage;

}


@property int vID;

@property(nonatomic,copy) NSString *vName;

@property(nonatomic,copy) NSString *vImage;


@end

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

*.m 文件

#import "NewVideoModel.h"


@implementation NewVideoModel

@synthesize vID;

@synthesize vImage;

@synthesize vName;


@end

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

这没有什么好说的,实际上,就是声明数据库端响应的属性字段。唯一注意的是,字段用copy


2.Server类(也可以叫Helper类),该Model类的操作类,来满足相应的数据操作。

*.h 文件


#import

#import


@interface VideoServer : NSObject{

    NSMutableArray *arrVideo;//使用数组保存实例数据(数据来自互联网,或者其他途径)

}


  

-(NSArray *) allVideoData;

-(id) init;


@end

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

*.m 文件


#import "VideoServer.h"

#import "NewVideoModel.h"


@implementation VideoServer


 

+(void) addVideoWithProperty:(int) vId image:(NSString *) vImage name:(NSString *)vName toArray:(NSMutableArray *)arrModel{

    // 实例化Model

    NewVideoModel *model=[[NewVideoModel alloc]init];

    model.vID=vId;

    model.vImage=vImage;

    model.vName=vName;

    

    // 加入数据

    [arrModel addObject:model];

    

    [model release];


}


 

-(id) init{

    if (self=[superinit]) {

        if (!arrVideo) {

            arrVideo=[[NSMutableArrayalloc] init];

        }

        

        // 循环并且得到数据,加入到数组中,实际应用中,用来处理服务器端的数据逻辑

        [VideoServer addVideoWithProperty:1 image:@"image_1" name:@"图片1" toArray:arrVideo];

        [VideoServer addVideoWithProperty:1 image:@"image_2" name:@"图片2" toArray:arrVideo];

        [VideoServer addVideoWithProperty:1 image:@"image_3" name:@"图片3" toArray:arrVideo];

        [VideoServer addVideoWithProperty:1 image:@"image_4" name:@"图片4" toArray:arrVideo];

        [VideoServer addVideoWithProperty:1 image:@"image_5" name:@"图片5" toArray:arrVideo];


    }

    

    return self;

}


 

-(void) dealloc{

    [arrVideorelease];

    [superdealloc];

}


 

-(NSArray *) allVideoData{

    return [[arrVideo copy] autorelease];


}

@end

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


就这样,这只是一个这种方式的一个雏形,当然,可以扩展。


在使用时,只需要进行实例化和调用就OK了。真心方便啊!!!


VideoServer *videoServer=[[VideoServer alloc]init];

NSArray *arrTemp =[videoServerallVideoData];


当然,别忘记了,在使用的时候,引用相应的类哟(废话了)


简单吧。这已经是很老的一种简单架构了。但是,在代码中多用到,发现维护量,真的减少了太多!而且排错也方便了!呵呵。


希望对您有帮助!

0 0
原创粉丝点击