深入解析设计模式中的装饰器模式在iOS应用开发中的实现

2020-01-15 13:22:37王冬梅

#import"Decorator.h"

复制代码
@implementation Decorator
-(void)SetComponents:(Components*)component{
    components = component;
}
-(void)Operation{
    if(components!=nil){
        [components Operation];
    }
}
@end
ConcreteComponent类接口文件
复制代码
#import"Components.h"

 

@interface ConcreteComponent :Components
@end


ConcreteComponent类实现文件
复制代码
#import "ConcreteComponent.h"

 

@implementation ConcreteComponent
-(void)Operation{
    NSLog(@"具体操作的对象");
}
@end


ConcreteDecoratorA类接口文件
复制代码
#import "ConcreteDecoratorA.h"
#import "Decorator.h"

 

@interface ConcreteDecoratorA :Decorator
@end


ConcreteDecoratorA类实现文件

 

#import"ConcreteDecoratorA.h"

复制代码
@implementation ConcreteDecoratorA
-(void)Operation{
    NSLog(@"具体装饰对象A的操作");
    [super Operation];
}
@end
ConcreteDecoratorB类接口文件

 

#import "Decorator.h"

复制代码
@interface ConcreteDecoratorB :Decorator
@end
ConcreteDecoratorB类实现文件
复制代码
#import "ConcreteDecoratorB.h"

 

@implementation ConcreteDecoratorB
-(void)Operation{
    NSLog(@"具体装饰对象B的操作");
    [super Operation];
}
@end


Main方法
复制代码
#import <Foundation/Foundation.h>
#import "ConcreteComponent.h"
#import "ConcreteDecoratorA.h"
#import "ConcreteDecoratorB.h"

 

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