iOS设计模式 - 外观
原理图
说明
1. 当客服端需要使用一个复杂的子系统(子系统之间关系错综复杂),但又不想和他们扯上关系时,我们需要单独的写出一个类来与子系统交互,隔离客户端与子系统之间的联系,客户端只与这个单独写出来的类交互
2. 外观模式实质为为系统中的一组接口提供一个统一的接口,外观定义了一个高层接口,让子系统易于使用
源码
https://github.com/YouXianMing/iOS-Design-Patterns
//
// ShapeMaker.h
// FacadePattern
//
// Created by YouXianMing on 15/7/28.
// Copyright (c) 2015年 YouXianMing. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Shape.h"
#import "Circle.h"
#import "Rectangle.h"
#import "Square.h"
@interface ShapeMaker : NSObject
+ (void)drawCircleAndRectangle;
+ (void)drawCircleAndSquare;
+ (void)drawAll;
@end
//
// ShapeMaker.m
// FacadePattern
//
// Created by YouXianMing on 15/7/28.
// Copyright (c) 2015年 YouXianMing. All rights reserved.
//
#import "ShapeMaker.h"
@implementation ShapeMaker
+ (void)drawCircleAndRectangle {
Shape *circle = [Circle new];
Shape *rectangle = [Rectangle new];
[circle draw];
[rectangle draw];
NSLog(@"\n");
}
+ (void)drawCircleAndSquare {
Shape *circle = [Circle new];
Shape *square = [Square new];
[circle draw];
[square draw];
NSLog(@"\n");
}
+ (void)drawAll {
Shape *circle = [Circle new];
Shape *rectangle = [Rectangle new];
Shape *square = [Square new];
[circle draw];
[rectangle draw];
[square draw];
NSLog(@"\n");
}
@end
//
// Shape.h
// FacadePattern
//
// Created by YouXianMing on 15/7/28.
// Copyright (c) 2015年 YouXianMing. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Shape : NSObject
/**
* 绘制
*/
- (void)draw;
@end
//
// Shape.m
// FacadePattern
//
// Created by YouXianMing on 15/7/28.
// Copyright (c) 2015年 YouXianMing. All rights reserved.
//
#import "Shape.h"
@implementation Shape
- (void)draw {
// 由子类重写
}
@end
分析
详细对比示意图
时间: 2024-10-30 14:53:13