美文网首页
iOS 外观模式

iOS 外观模式

作者: 印林泉 | 来源:发表于2017-03-05 23:59 被阅读37次
    • 外观模式
      封装,隐藏实现细节。简化了操作,简化流程,解耦,简化操作逻辑。

    • 应用,适用场景

      • 复杂的子系统,改进使用操作类来操作子系统,通过使用操作类来启用子系统功能。
      • 不关心逻辑,只要结果
      • 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
    

    相关文章

      网友评论

          本文标题:iOS 外观模式

          本文链接:https://www.haomeiwen.com/subject/idfygttx.html