外觀模式

外觀模式

外觀模式(Facade Pattern):外部與一個(gè)子系統(tǒng)的通信必須通過一個(gè)統(tǒng)一的外觀對(duì)象進(jìn)行,為子系統(tǒng)中的一組接口提供一個(gè)一致的界面,外觀模式定義了一個(gè)高層接口,這個(gè)接口使得這一子系統(tǒng)更加容易使用。外觀模式又稱為門面模式,它是一種對(duì)象結(jié)構(gòu)型模式。

外觀模式
封裝,隱藏實(shí)現(xiàn)細(xì)節(jié)。簡化了操作,簡化流程,解耦,簡化操作邏輯。
應(yīng)用,適用場景
o 復(fù)雜的子系統(tǒng),改進(jìn)使用操作類來操作子系統(tǒng),通過使用操作類來啟用子系統(tǒng)功能。
o 不關(guān)心邏輯,只要結(jié)果
o ShapeMaker
子系統(tǒng)正逐漸變得復(fù)雜。應(yīng)用模式的過程中演化出許多類??梢允褂猛庥^為這些子系統(tǒng)類提供一個(gè)較簡單的接口。
可以使用外觀對(duì)子系統(tǒng)進(jìn)行分層。每個(gè)子系統(tǒng)級(jí)別有一個(gè)外觀作為入口點(diǎn)。讓它們通過其外觀進(jìn)行通信,可以簡化它們的依賴關(guān)系。

外觀模式的UML類圖

外觀模式的UML類圖.png

這里的Facade類就是門面類,依賴SystemOne、SystemTwo、SystemThree、SystemFour四個(gè)系統(tǒng)類,將其封裝調(diào)用供外部使用。

測試代碼:

//買車
- (IBAction)test:(UIButton *)sender {
    Facade* facadeObj = [Facade new];
    [facadeObj buyCars:0];
}

Facade文件:

#import <Foundation/Foundation.h>

@interface Facade : NSObject
-(void)buyCars:(NSInteger)tag;
@end


#import "Facade.h"
#import "SystemOne.h"
#import "SystemTwo.h"
#import "SystemThree.h"

@interface Facade()
@property (strong, nonatomic)SystemOne *one;
@property (strong, nonatomic)SystemTwo *two;
@property (strong, nonatomic)SystemThree *three;

@end

@implementation Facade

#pragma mark - Public

-(void)buyCars:(NSInteger)tag{
    if(tag == 0){
        //  調(diào)用兩個(gè)系統(tǒng)
        [self methodA];
        [self methodB];
    } else {
        //  調(diào)用三個(gè)系統(tǒng)
        [self methodA];
        [self methodB];
        [self methodC];
    }
}

- (void)methodA {
    NSLog(@"\n方法組 A-------------------------");
    [self.one methodOne];
}

- (void)methodB {
    NSLog(@"\n方法組 B-------------------------");
    [self.two methodTwo];
}

- (void)methodC {
    NSLog(@"\n方法組 C-------------------------");
    [self.three methodThree];
}

#pragma mark - Setters & Getters
- (SystemOne *)one {
    if (!_one) {
        _one = [[SystemOne alloc]init];
    }
    return _one;
}

- (SystemTwo *)two {
    if (!_two) {
        _two = [[SystemTwo alloc]init];
    }
    return _two;
}

- (SystemThree *)three {
    if (!_three) {
        _three = [[SystemThree alloc]init];
    }
    return _three;
}

@end

其他三個(gè)系統(tǒng)文件:

#import <Foundation/Foundation.h>
@interface SystemOne : NSObject
- (void)methodOne;
@end

#import "SystemOne.h"
@implementation SystemOne
- (void)methodOne {
    NSLog(@"\nSystemOne methodOne");
}
@end




#import <Foundation/Foundation.h>
@interface SystemTwo : NSObject
- (void)methodTwo;
@end

#import "SystemTwo.h"
@implementation SystemTwo
- (void)methodTwo {
    NSLog(@"\nSystemTwo methodTwo");
}
@end




#import <Foundation/Foundation.h>
@interface SystemThree : NSObject
- (void)methodThree;
@end

#import "SystemThree.h"
@implementation SystemThree
- (void)methodThree {
    NSLog(@"\nSystemThree methodThree");
}
@end
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容