外观模式的应用场景

外观模式的使用场景:1.首先,在设计初期阶段,应该要有意识的将不同的两个层分离,比如经典的三层架构,就需要考虑在数据访问层和业务逻辑,业务逻辑层和表示层的层与层之间建立外观Facade(外观),这样可以为复杂的子系统提供应该简单的接口,使得耦合大大降低。2.其次,在开发阶段,子系统往往因为不断的重构演化而变得越来越复杂,大多数的模式使用时也都会产生很多很小的类,这本是好事,但也给外部调用它们的用户程序带来使用上的困难,增加外观Facade可以提供一个简单接口,减少它们之间的依赖。3.在维护一个遗留的大型系统时,可能这个系统已经非常难以维护和扩展了,但因为它包含非常重要的功能,新的需求开发必须依赖它。此时用外观模式Facade也是非常合适的。你可以为新系统开发一个外观Facade类,来提供设计粗糙或高度复杂的遗留代码的比较清晰简单的接口,让新系统与Facade对象进行交互,Facade与遗留代码交互所有复杂的工作。

#import <Foundation/Foundation.h>

#import "Fund.h"

int main(int argc, const char * argv[]) {

    @autoreleasepool {

        Fund *jijin = [[Fund alloc] init];

        [jijin SellFound];

        [jijin BuFund];   

    }

    return 0;

}

//基金类

#import <Foundation/Foundation.h>

#import "Stock1.h"

#import "Stock2.h"

#import "Stock3.h"

#import "National1Debt1.h"

#import "Realty1.h"

@interface Fund : NSObject

-(void)BuFund;

-(void)SellFound;

@end

#import "Fund.h"

 

@implementation Fund

-(void)BuFund{

    [Stock1 Buy];

    [Stock2 Buy];

    [Stock3 Buy];

    [National1Debt1 Buy];

    [Realty1 Buy];

}

-(void)SellFound{

    [Stock1 Sell];

    [Stock2 Sell];

    [Stock3 Sell];

    [National1Debt1 Sell];

    [Realty1 Sell];

}

@end

#import <Foundation/Foundation.h>

 

@interface Stock1 : NSObject

//卖股票方法

+(void)Sell;

//买股票的方法

+(void)Buy;

@end

#import "Stock1.h"

 

@implementation Stock1

//买股票方法的实现

+(void)Sell{

    NSLog(@"股票1卖出");

}

+(void)Buy{

    NSLog(@"股票1买入");

}

@end

#import <Foundation/Foundation.h>

 

@interface Stock2 : NSObject

//卖股票方法

+(void)Sell;

//买股票的方法

+(void)Buy;

@end

#import "Stock2.h"

 

@implementation Stock2

+(void)Sell{

    NSLog(@"股票2卖出");

}

+(void)Buy{

    NSLog(@"股票2买入");

}

@end

#import <Foundation/Foundation.h>

 

@interface Stock3 : NSObject

//卖股票方法

+(void)Sell;

//买股票的方法

+(void)Buy;

@end

#import "Stock3.h"

 

@implementation Stock3

+(void)Sell{

    NSLog(@"股票3卖出");

}

+(void)Buy{

    NSLog(@"股票3买入");

}

@end

#import <Foundation/Foundation.h>

 

@interface National1Debt1 : NSObject

//买国债的方法

+(void)Sell;

//买国债的方法

+(void)Buy;

@end

#import "National1Debt1.h"

 

@implementation National1Debt1

+(void)Sell{

    NSLog(@"国债1卖出");

}

+(void)Buy{

    NSLog(@"国债1买入");

}

@end

#import <Foundation/Foundation.h>

 

@interface Realty1 : NSObject

//买房地产的方法

+(void)Sell;

//买房地产的方法

+(void)Buy;

@end

#import "Realty1.h"

 

@implementation Realty1

+(void)Sell{

    NSLog(@"房地产1卖出");

}

+(void)Buy{

    NSLog(@"房地产1买入");

}

@end

 

posted on 2016-03-05 19:59  liumu1994  阅读(1541)  评论(1编辑  收藏  举报