欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

实例解析设计模式中的外观模式在iOS App开发中的运用

程序员文章站 2023-11-20 16:28:10
外观模式(facade),为子系统中的一组接口提供一个一致的界面,此模式定义 一个高层接口,这个接口使得这一子系统更加容易使用。 下面给大家展示一下类的结构图,想必大家一...

外观模式(facade),为子系统中的一组接口提供一个一致的界面,此模式定义 一个高层接口,这个接口使得这一子系统更加容易使用。

下面给大家展示一下类的结构图,想必大家一看就明白了:

实例解析设计模式中的外观模式在iOS App开发中的运用

其实这个模式中,没有类与类之间的继承关系,只是进行了简单的类引用,统一了对外的接口而已。看起来是不是很简单?废话不多说了,下面简单向大家展示一下代码吧!

注意:本文所有代码均在arc环境下编译通过。

subsystemone类接口

复制代码 代码如下:

#import <foundation/foundation.h>

@interface subsystemone:nsobject
-(void)methodone;
@end


subsystemone类实现
复制代码 代码如下:

#import "subsystemone.h"

@implementation subsystemone
-(void)methodone{
    nslog(@"子系统方法一");
}
@end


subsystemtwo类接口
复制代码 代码如下:

#import <foundation/foundation.h>

@interface subsystemtwo:nsobject
-(void)methodtwo;
@end


subsystemtwo类实现
复制代码 代码如下:

#import "subsystemtwo.h"

@implementation subsystemtwo
-(void)methodtwo{
    nslog(@"子系统方法二");
}
@end


subsystemthree类接口
复制代码 代码如下:

#import <foundation/foundation.h>

@interface subsystemthree:nsobject
-(void)methodthree;
@end


subsystemthree类实现
复制代码 代码如下:

#import "subsystemthree.h"

@implementation subsystemthree
-(void)methodthree{
    nslog(@"子系统方法三");
}
@end


subsystemfour类接口
复制代码 代码如下:

#import <foundation/foundation.h>

@interface subsystemfour:nsobject
-(void)methodfour;
@end


subsystemfour类实现
复制代码 代码如下:

#import "subsystemfour.h"

@implementation subsystemfour
-(void)methodfour{
    nslog(@"子系统方法四");
}
@end


facade类接口
复制代码 代码如下:

#import<foundation/foundation.h>

@class subsystemone;//此处@class关键字的作用是声明(不是定义哦)所引用的类
@class subsystemtwo;
@class subsystemthree;
@class subsystemfour;
@interface facade :nsobject{
@private subsystemone *one;
@private subsystemtwo *two;
@private subsystemthree *three;
@private subsystemfour *four;
}
-(facade*)myinit;
-(void)methoda;
-(void)methodb;
@end


facade类实现
复制代码 代码如下:

#import "facade.h"
#import "subsystemone.h"
#import "subsystemtwo.h"
#import "subsystemthree.h"
#import "subsystemfour.h"

@implementation facade
-(facade*)myinit{
    one= [[subsystemone alloc]init];
    two= [[subsystemtwo alloc]init];
    three= [[subsystemthree alloc]init];
    four= [[subsystemfour alloc]init];
    return self;
}
-(void)methoda{
    nslog(@"\n方法组a() ---- ");
    [one methodone];
    [two methodtwo];
    [three methodthree];
    [four methodfour];
}
-(void)methodb{
    nslog(@"\n方法组b() ---- ");
    [two methodtwo];
    [three methodthree];
}
@end


main()方法调用
复制代码 代码如下:

#import <foundation/foundation.h>
#import "facade.h"

int main (int argc,const char * argv[])
{
    @autoreleasepool{
        facade *facade = [[facade alloc]myinit];
        [facade methoda];
        [facade methodb];
    }
    return 0;
}


在开发软件时候,考虑使用外观模式的情况一般分为三种情况。第一种情况,设计初始阶段,应该要有意识的将不同的两个分层分离,层与层之间建立外观facade,这样可以为复杂的子系统提供一个简单的接口,使得耦合大大降低。第二种情况,在开发阶段子系统往往因为不断的重构演化而变得越来越复杂,增加外观facade可以提供一个简单的接口,减少它们之间的依赖。第三种情况,在维护一个遗留的大型系统时,可能这个系统已经非常难以维护和扩展了,如果有新的需求,那么可以为新系统开发一个外观facade类,来提供设计粗糙或高度复杂的遗留代码的比较清晰简单的接口,让新系统与facade对象交互,facade与遗留代码交互所有复杂的工作,这样可以保持较低的耦合度。

实例进阶
目前你有 persistencymanager 来在本地存储专辑数据,httpclient 处理远程通信。项目中其它的类跟这些逻辑都没关。

执行这个模式,只有 libraryapi 来保存 persistencymanager 和 httpclient 的实例。之后,libraryapi 将会公开一个简单的 api 来访问这些服务。

实例解析设计模式中的外观模式在iOS App开发中的运用

libraryapi 将会公开给其它代码,但是它隐藏了 app 中 httpclient 和 persistencymanager 的复杂部分。

打开 libraryapi.h,在顶部引入面文件:

#import "album.h"
接下来,在 libraryapi.h下面添加如下方法:

复制代码 代码如下:

- (nsarray*)getalbums;
- (void)addalbum:(album*)album atindex:(int)index;
- (void)deletealbumatindex:(int)index;

现在,这些方法都公开给了其它类。

在 libraryapi.m 文件引入如下两个文件:

#import "persistencymanager.h"
#import "httpclient.h"
只有在这个地方你才会需要引入这些类。记住:你的 api 将会是你「复杂」系统的唯一的接入点。

现在添加一些私有属性在你的类的扩展里(在 @implementation 上面)

复制代码 代码如下:

@interface libraryapi () {
    persistencymanager *persistencymanager;
    httpclient *httpclient;
    bool isonline;
}
@end

isonline 用来判断,如果专辑列表数据发生变化是否能够更新到服务器,例如添加或者删除专辑。

你现在需要在 init 方法中初始化这些变量,在 libraryapi.m 中添加下面代码:

复制代码 代码如下:

- (id)init
{
    self = [super init];
    if (self) {
        persistencymanager = [[persistencymanager alloc] init];
        httpclient = [[httpclient alloc] init];
        isonline = no;
    }
    return self;
}

这个 http 客户端在这里并不真正的工作,它只是在外观设计里面起一个示范用法的作用,所以 isonline 永远是 no 了。

接下来,在 libraryapi.m 里面添加下面三个方法:

复制代码 代码如下:

- (nsarray*)getalbums
{
    return [persistencymanager getalbums];
}

- (void)addalbum:(album*)album atindex:(int)index
{
    [persistencymanager addalbum:album atindex:index];
    if (isonline)
    {
        [httpclient postrequest:@"/api/addalbum" body:[album description]];
    }
}

- (void)deletealbumatindex:(int)index
{
    [persistencymanager deletealbumatindex:index];
    if (isonline)
    {
        [httpclient postrequest:@"/api/deletealbum" body:[@(index) description]];
    }
}


看一下 addalbum:atindex:。这个类首先更新本地数据,如果联网,它再更新远端服务器。这就是外观设计的长处;当一些系统外的类添加了一个新专辑,它不知道─也不需要知道─复杂的内部系统。

提示:当在你的子系统里设计一个外观类的时候,记住没有任何东西可能阻止客户访问这些「隐藏」类。要多写些防御性的代码,不要想当然的认为所有客户都会用同样的方式使用你的外观类。
运行你的程序,你会看一个黑底空白内容的屏幕,像下面这样:

实例解析设计模式中的外观模式在iOS App开发中的运用