iOS 设计模式之抽象工厂
设计模式是程序提升的必备知识,这里说下iOS怎样实现抽象工厂设计模式。本文是看过oc编程之道这本的抽象工厂这章后写出的,假设不明确原理能够看看那本书。
TestView.h首先创建一个视图
//
// TestView.h
// AbstractFactory
//
// Created by 杜甲 on 11/10/14.
// Copyright (c) 2014 杜甲. All rights reserved.
//#import <UIKit/UIKit.h>@interface TestView : UIView@end
TestView.m
//
// TestView.m
// AbstractFactory
//
// Created by 杜甲 on 11/10/14.
// Copyright (c) 2014 杜甲. All rights reserved.
//#import "TestView.h"@implementation TestView- (id)initWithFrame:(CGRect)frame
{if (self = [super initWithFrame:frame]) {self.backgroundColor = [UIColor redColor];}return self;
}@end
接下来创建两个类TestFactory、TestBrandingFactory 当中TestFactory继承TestBrandingFactory。详细实现例如以下:
TestBrandingFactory.h
//
// TestBrandingFactory.h
// AbstractFactory
//
// Created by 杜甲 on 11/10/14.
// Copyright (c) 2014 杜甲. All rights reserved.
//#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>@interface TestBrandingFactory : NSObject+ (TestBrandingFactory *)factory;- (UIView *)createTestView:(CGRect)frame;@end
TestBrandingFactory.m
//
// TestBrandingFactory.m
// AbstractFactory
//
// Created by 杜甲 on 11/10/14.
// Copyright (c) 2014 杜甲. All rights reserved.
//#import "TestBrandingFactory.h"
#import "TestFactory.h"@implementation TestBrandingFactory+ (TestBrandingFactory *) factory
{return [[TestFactory alloc] init];
}- (UIView *) createTestView:(CGRect)frame
{return nil;
}@end
TestFactory.h
//
// TestFactory.h
// AbstractFactory
//
// Created by 杜甲 on 11/10/14.
// Copyright (c) 2014 杜甲. All rights reserved.
//#import "TestBrandingFactory.h"@interface TestFactory : TestBrandingFactory@end
TestFactory.m
//
// TestFactory.m
// AbstractFactory
//
// Created by 杜甲 on 11/10/14.
// Copyright (c) 2014 杜甲. All rights reserved.
//#import "TestFactory.h"
#import "TestView.h"@implementation TestFactory- (UIView *)createTestView:(CGRect)frame
{return [[TestView alloc] initWithFrame:frame];
}@end
最后贴出实现
TestBrandingFactory * tmp = [TestBrandingFactory factory];UIView *v = [tmp createTestView:CGRectMake(50, 110, 100, 50)];[self.view addSubview:v];
转载于:https://www.cnblogs.com/bhlsheji/p/5368881.html
iOS 设计模式之抽象工厂相关推荐
- 设计模式系列·抽象工厂模式
前言 以小说的笔法写的设计模式系列文章,你绝对看得懂![首发于公众号:"聊聊代码"] 设计模式系列·王小二需求历险记(一) 设计模式系列·王小二需求历险记(二) 设计模式系列·封装 ...
- 设计模式三—抽象工厂模式
设计模式三-抽象工厂模式 一.定义 抽象工厂模式是工厂方法模式的进一步抽象.如果产品簇中只有一种产品,则退化为工厂方法模式. 二.原理图 三.代码实例 * 苹果和土豆是园丁1的杰作 * 葡萄和西红柿是 ...
- 乐在其中设计模式(C#) - 抽象工厂模式(Abstract Factory Pattern)
原文:乐在其中设计模式(C#) - 抽象工厂模式(Abstract Factory Pattern) [索引页] [源码下载] 乐在其中设计模式(C#) - 抽象工厂模式(Abstract Facto ...
- 设计模式复习-抽象工厂模式
设计模式复习-抽象工厂模式 有两种硬件,PC和Phone,有两种系统,Windows和Linux,现在假设PC和Phone上全都能安装这两个系统,并且将来硬件不会在变化,但是系统可能需要扩展,比如扩展 ...
- 设计模式之四(抽象工厂模式第三回合)
原文:设计模式之四(抽象工厂模式第三回合) 前言 抽象工厂模式:提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类. 抽象工厂模式最大的好处便是易于交换产品系列,由于具体工厂类,例如I ...
- python抽象工厂模式_Python设计模式之抽象工厂模式
Python设计模式之抽象工厂模式 这篇文章主要为大家详细介绍了Python设计模式之抽象工厂模式,感兴趣的小伙伴们可以参考一下 python面向对象编程入门,我们需要不断学习进步 "&qu ...
- 抽象工厂模式设计模式_抽象工厂设计模式解释
抽象工厂模式设计模式 抽象工厂设计模式是工厂设计模式的另一种形式. 这种模式可以被视为"超级工厂"或"工厂工厂". 抽象工厂设计模式(属于"四人帮&q ...
- 设计模式-04抽象工厂模式
设计模式-04抽象工厂模式 文章中涉及到的代码,请自行下载 https://gitee.com/pan_xiao_lei123/designmode.git 前面介绍的工厂方法模式中考虑的是一类产品的 ...
- C#设计模式(4)——抽象工厂模式
C#设计模式(4)--抽象工厂模式 一.引言 在上一专题中介绍了工厂方法模式,工厂方法模式是为了克服简单工厂模式的缺点而设计出来的,简单工厂模式的工厂类随着产品类的增加需要增加额外的代码),而工厂方法 ...
最新文章
- 如何解决ORA-00054资源正忙,要求指定NOWAIT?
- unity5x --------Music Mixer参数详解
- Go + Excel 学习 Excelize rows.go
- 护肤产生共鸣_通过以人为本的设计编织共鸣的20个指针
- php base64尺寸,PHP base64编码的图像显示不正确
- 工业相机像元与像素之间的关系
- 【研一周小结】第八-九周个人学习总结
- 上海学车科目二,科目三容易扣分点分享,你了解多少
- 【2019春招准备:A1】
- E. Binary Inversions——前缀+后缀
- nuxt百度地图引入基础使用,百度地图初始化,行政区划划分
- 李笑来:“我们不要过度乐观”
- nanopi neo core学习(二)网络设置
- 科技创新如何托举湖湘未来?岳麓峰会夯实“长沙厚度”
- 《IT经理世界》:腾讯的瓶颈
- 用ChatGPT处理Excel。。真有你的!
- 永磁同步电机 SVPWM 原理
- 宽电压输入高电压输出 电压控制型
- 创新型服务“智能推荐”——互联网平台建设
- 项目管理 | 项目成本估算都有哪些方法?