iOS 外观模式

2017-03-05  本文已影响37人  印林泉

图形基类

//
//  Shape.h
//  LearnFacade
//
//  Created by 印林泉 on 2017/3/5.
//  Copyright © 2017年 ylq. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Shape : NSObject

- (void)draw;

@end
//
//  Shape.m
//  LearnFacade
//
//  Created by 印林泉 on 2017/3/5.
//  Copyright © 2017年 ylq. All rights reserved.
//

#import "Shape.h"

@implementation Shape

- (void)draw {
    
}

@end

矩形

//
//  Rectangle.h
//  LearnFacade
//
//  Created by 印林泉 on 2017/3/5.
//  Copyright © 2017年 ylq. All rights reserved.
//

#import "Shape.h"
#import <UIKit/UIKit.h>

@interface Rectangle : Shape

@property (nonatomic) CGFloat  width;
@property (nonatomic) CGFloat  height;

- (void)draw;

@end
//
//  Rectangle.m
//  LearnFacade
//
//  Created by 印林泉 on 2017/3/5.
//  Copyright © 2017年 ylq. All rights reserved.
//

#import "Rectangle.h"

@implementation Rectangle

- (void)draw {
    ///具体的实现
}

@end

圆形

//
//  Circle.h
//  LearnFacade
//
//  Created by 印林泉 on 2017/3/5.
//  Copyright © 2017年 ylq. All rights reserved.
//

#import "Shape.h"
#import <UIKit/UIKit.h>

@interface Circle : Shape

@property (nonatomic) CGFloat  radius;

- (void)draw;

@end
//
//  Circle.m
//  LearnFacade
//
//  Created by 印林泉 on 2017/3/5.
//  Copyright © 2017年 ylq. All rights reserved.
//

#import "Circle.h"

@implementation Circle

- (void)draw {
    ///具体的实现
}

@end

图形操作类

//
//  ShapeMaker.h
//  LearnFacade
//
//  Created by 印林泉 on 2017/3/5.
//  Copyright © 2017年 ylq. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Rectangle.h"
#import "Circle.h"

@interface ShapeMaker : NSObject

///绘制圆
+ (void)drawCircleWithParas:(NSDictionary *)paras;
///绘制圆 + 矩形
+ (void)drawCircleAndRectangle:(NSDictionary *)paras;

@end
//
//  ShapeMaker.m
//  LearnFacade
//
//  Created by 印林泉 on 2017/3/5.
//  Copyright © 2017年 ylq. All rights reserved.
//

#import "ShapeMaker.h"

@implementation ShapeMaker

+ (void)drawCircleWithParas:(NSDictionary *)paras {
    ///绘制了一个圆
    Circle *circle = [Circle new];
    circle.radius  = 10.f;///paras里取
    [circle draw];
}

+ (void)drawCircleAndRectangle:(NSDictionary *)paras {
    ///绘制了一个圆
    Circle *circle = [Circle new];
    circle.radius  = 10.f;///paras里取
    [circle draw];
    ///绘制了一个矩形
    Rectangle *rectangle = [Rectangle new];
    rectangle.width = 10.f;///paras里取
    rectangle.height = 20.f;///paras里取
    [rectangle draw];
}

@end

使用

//
//  ViewController.m
//  LearnFacade
//
//  Created by 印林泉 on 2017/3/5.
//  Copyright © 2017年 ylq. All rights reserved.
//

#import "ViewController.h"
#import "Circle.h"
#import "Rectangle.h"
#import "ShapeMaker.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //[self simple];
    [self facade];
}

- (void)facade {
    ///绘制一个圆的操作
    [ShapeMaker drawCircleWithParas:@{@"a" : @"b"}];
    ///绘制圆 + 矩形操作
    [ShapeMaker drawCircleAndRectangle:@{@"a" : @"b", @"c" : @"d"}];
}

- (void)simple {
    ///绘制了一个圆
    Circle *circle = [Circle new];
    circle.radius = 10.f;
    [circle draw];
    ///绘制了一个矩形
    Rectangle *rectangle = [Rectangle new];
    rectangle.width = 10.f;
    rectangle.height = 20.f;
    [rectangle draw];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
上一篇下一篇

猜你喜欢

热点阅读