iOS 外观模式

  • 外观模式
    封装,隐藏实现细节。简化了操作,简化流程,解耦,简化操作逻辑。

  • 应用,适用场景

    • 复杂的子系统,改进使用操作类来操作子系统,通过使用操作类来启用子系统功能。
    • 不关心逻辑,只要结果
    • ShapeMaker

图形基类

//
//  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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 176,719评论 25 709
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,212评论 19 139
  • 我们在黑暗中仰望阳光,一个人一生至少有一两个梦想,才能撑起我们一直走下去。 我记得儿时跟母亲去百货公司,看到那些玲...
    41b699c6840b阅读 4,253评论 3 2
  • 最近在看《和时间做朋友》这本书,切肤之痛的感觉让我罗列出生活中自己所犯的七宗罪,借此希望跟读文章的你产生共勉。 第...
    跃童ING阅读 3,262评论 6 3
  • 先科普一下什么是髂胫束,什么是髂胫束综合症。 髂胫束是在大腿外侧、链接髋部和胫骨的结缔组织(如下图所示),它的...
    兴礼阅读 12,509评论 0 0

友情链接更多精彩内容