博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS设计模式 - 抽象工厂
阅读量:7088 次
发布时间:2019-06-28

本文共 2590 字,大约阅读时间需要 8 分钟。

iOS设计模式 - 抽象工厂

 

原理图

 

说明

1. 抽象工厂指的是提供一个创建一系列相关或者相互依赖对象的接口,而无需指定它们具体的类

2. 如果多个类有相同的行为,但实际实现不同,则可能需要某种抽象类型作为其父类被继承,抽象类型定义了所有相关具体类将共有的共同行为

 

源码

////  BrandingFactory.h//  AbstractFactoryPattern////  Created by YouXianMing on 15/8/2.//  Copyright (c) 2015年 YouXianMing. All rights reserved.//#import 
#import
@interface BrandingFactory : NSObject/** * 抽象工厂方法 * * @return 具体的工厂 */+ (BrandingFactory *)factory;/** * 该工厂生产的brandedView(由具体工厂构造) * * @return 生产好的brandedView */- (UIView *)brandedView;/** * 该工厂生产的brandedMainButton(由具体工厂构造) * * @return 生产好的brandedMainButton */- (UIButton *)brandedMainButton;@end
////  BrandingFactory.m//  AbstractFactoryPattern////  Created by YouXianMing on 15/8/2.//  Copyright (c) 2015年 YouXianMing. All rights reserved.//#import "BrandingFactory.h"#import "AcmeBrandingFactory.h"#import "SierraBrandingFactory.h"@implementation BrandingFactory+ (BrandingFactory *)factory {        if ([[self class] isSubclassOfClass:[AcmeBrandingFactory class]]) {                return [AcmeBrandingFactory new];            } else if ([[self class] isSubclassOfClass:[SierraBrandingFactory class]]) {            return [SierraBrandingFactory new];            } else {            return nil;    }}- (UIView *)brandedView {    return nil;}- (UIButton *)brandedMainButton {    return nil;}@end
////  AcmeBrandingFactory.h//  AbstractFactoryPattern////  Created by YouXianMing on 15/8/2.//  Copyright (c) 2015年 YouXianMing. All rights reserved.//#import "BrandingFactory.h"@interface AcmeBrandingFactory : BrandingFactory@end
////  AcmeBrandingFactory.m//  AbstractFactoryPattern////  Created by YouXianMing on 15/8/2.//  Copyright (c) 2015年 YouXianMing. All rights reserved.//#import "AcmeBrandingFactory.h"@implementation AcmeBrandingFactory- (UIView *)brandedView {        NSLog(@"AcmeBrandedView");    return nil;}- (UIButton *)brandedMainButton {        NSLog(@"AcmeBrandedMainButton");    return nil;}@end
////  SierraBrandingFactory.h//  AbstractFactoryPattern////  Created by YouXianMing on 15/8/2.//  Copyright (c) 2015年 YouXianMing. All rights reserved.//#import "BrandingFactory.h"@interface SierraBrandingFactory : BrandingFactory@end
////  SierraBrandingFactory.m//  AbstractFactoryPattern////  Created by YouXianMing on 15/8/2.//  Copyright (c) 2015年 YouXianMing. All rights reserved.//#import "SierraBrandingFactory.h"@implementation SierraBrandingFactory- (UIView *)brandedView {        NSLog(@"SierraBrandedView");    return nil;}- (UIButton *)brandedMainButton {        NSLog(@"SierraBrandedMainButton");    return nil;}@end

分析

关系原理图

 

转载地址:http://slbql.baihongyu.com/

你可能感兴趣的文章
数据结构之链表【上】
查看>>
Go并发实战笔记整理
查看>>
我们的手机用上北斗导航了吗?
查看>>
6年来,Docker的这些变化你都知道吗?
查看>>
支付宝客户端架构解析:iOS 客户端启动性能优化初探
查看>>
Maven之pom.xml配置文件详解(转载)
查看>>
优化Git本地仓库
查看>>
对.NET Core未来发展趋势的浅层判断
查看>>
Python高级知识点学习(七)
查看>>
《人月神话》(P7)编写手册和组织开会
查看>>
WPF如何实现一个漂亮的页签导航UI
查看>>
Dubbo+zookeeper实现分布式服务框架
查看>>
HTML编码规范
查看>>
游戏开发者福音:微软开源部分 Minecraft 的 Java 代码
查看>>
Firefox 66 存在使 PPT 文字消失的 bug,v68 才修复
查看>>
Android 三星手机拍照,从图库选择照片旋转问题完美解决
查看>>
在线表格 x-spreadsheet 1.0.16 发布
查看>>
PostgreSQL 多值列的选择性 - Statistics, Cardinality, Selectivity, Estimate
查看>>
三大主流芯片架构特点
查看>>
Python Flask学习知识点(四)
查看>>