用Objective-C实现抽象类

栏目: Objective-C · 发布时间: 5年前

内容简介:插图的花是我上个月发工资当天晚上加班回家的路上从一个老人家那里买的,一枝独秀!上周在微信公众号发布的一篇名为反向代理) 的文章,居然真被微信给封掉了,当时在写的时候我还在想是不是会被微信给封杀掉,真是怕什么来什么,完全符合墨菲定律!那篇文章我就是想给大家分享一下如何设置

用Objective-C实现抽象类

插图的花是我上个月发工资当天晚上加班回家的路上从一个老人家那里买的,一枝独秀!

上周在微信公众号发布的一篇名为反向代理) 的文章,居然真被微信给封掉了,当时在写的时候我还在想是不是会被微信给封杀掉,真是怕什么来什么,完全符合墨菲定律!那篇文章我就是想给大家分享一下如何设置 Nginx 的反向代理,其中的一些概念可能是因为牵涉到了敏感词汇,本无恶意!

不说这件事情了,还是回到今天分享的主题上面来吧~

在软件设计或者架构设计领域,架构师都会跟大家强调对业务一定要抽象,把具体的业务场景抽象为程序实现很考验一个人的编程和思维能力。所以在面向对象的编程中,抽象是一种必备的能力。

设计框架的时候,一般我们都会约定一些协议,各个模块在交互通信中能够遵守和实现这些协议,就可以保证通信的畅通性,就好比大家遵守排队规则一样,一个一个来就不会乱。

协议可以使用接口的形式或者抽象类的形式都可以定义,不同的编程语言有不同的形式。例如,在 Java 中提供给开发者的有接口 Interface 还有抽象类 Abstract ,示例如下:

/** 接口 */
interface ISeverListener{
    public void covertService(String name);
}

/** 抽象类 */
abstract class AbstractServer{
    protected abstract String destroyService(String serverID);
}

C++ 中也有抽象类,示例如下:

class Point
{
protected:
    int x, y;
public:
    //纯虚函数
    virtual void disp()=0;
};

Objective-C 中提供了协议 protocol ,没有提供抽象类的东西,但是我们可以借助 protocol 模拟实现抽象类。

DownloaderProtocol.h 协议

#import<Foundation/Foundation.h>

@protocol DownloaderProtocol<NSObject>

@required

- (BOOL)checkDownloader;

- (void)startDownload:(id)url;

- (void)stopDownload;

- (void)deleteAllDownloadFile;

@end

AbstractDownloader.h

#import<Foundation/Foundation.h>
#import"DownloaderProtocol.h"

@interface AbstractDownloader:NSObject<DownloaderProtocol>

- (void)setDownloadUrl:(NSString *)url;

@end

AbstractDownloader.m

#import"AbstractDownloader.h"

#define AbstractMethodNotImplemented() \
@throw [NSException exceptionWithName:NSInternalInconsistencyException \
reason:[NSString stringWithFormat:@"You must override %@ in a subclass.", NSStringFromSelector(_cmd)] \
userInfo:nil]


@implementation AbstractDownloader

- (instancetype)init
{
    NSAssert(![self isMemberOfClass:[AbstractDownloader class]], @"AbstractDownloader is an abstract class, you should not instantiate it directly.");
    
    return [super init];
}

- (BOOL)checkDownloader
{
    AbstractMethodNotImplemented();
}

- (void)startDownload:(id)url
{
    AbstractMethodNotImplemented();
}

- (void)stopDownload
{
    AbstractMethodNotImplemented();
}

- (void)deleteAllDownloadFile
{
    AbstractMethodNotImplemented();
}

- (void)setDownloadUrl:(NSString *)url
{
    NSLog(@"AbstractDownloader's url = %@", url);
}

@end

从上面的代码可以看出 AbstractMethodNotImplemented() 是模拟抽象类的关键,有了这个宏动议之后,凡是需要继承 AbstractDownloader 必须要实现对应的方法,否则会报错。 ImageDownloader 继承 AbstractDownloader 并且实现对应的方法,示例如下:

ImageDownloader.h

#import"AbstractDownloader.h"

@interface ImageDownloader:AbstractDownloader

@end

ImageDownloader.m

#import"ImageDownloader.h"

@implementation ImageDownloader

- (BOOL)checkDownloader
{
    NSLog(@"ImageDownloader checkDownloader...");
    
    return YES;
}

- (void)startDownload:(id)url
{
    NSLog(@"ImageDownloader startDownload...");
}

- (void)stopDownload
{
    NSLog(@"ImageDownloader stopDownload...");
}

- (void)deleteAllDownloadFile
{
    NSLog(@"ImageDownloader deleteAllDownloadFile...");
}

@end

这样做的话,团队相关的开发人员就知道自己继承自 AbstractDownloader 应该做什么了。

这里只是给大家提供一种设计思路,有更好的思路欢迎交流,感谢每一个关注者、阅读者。

一直致力于让开发变得更美好的公众号~

用Objective-C实现抽象类


以上所述就是小编给大家介绍的《用Objective-C实现抽象类》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Domain-Driven Design Distilled

Domain-Driven Design Distilled

Vaughn Vernon / Addison-Wesley Professional / 2016-6-2 / USD 36.99

Domain-Driven Design (DDD) software modeling delivers powerful results in practice, not just in theory, which is why developers worldwide are rapidly moving to adopt it. Now, for the first time, there......一起来看看 《Domain-Driven Design Distilled》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

MD5 加密
MD5 加密

MD5 加密工具