设计模式是程序提升的必备知识,这里说下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];

iOS 设计模式之抽象工厂的更多相关文章

  1. iOS设计模式 - (3)简单工厂模式

    iOS设计模式 - (3)简单工厂模式           by Colin丶 转载请注明出处:              http://blog.csdn.net/hitwhylz/article/ ...

  2. 乐在其中设计模式(C#) - 抽象工厂模式(Abstract Factory Pattern)

    原文:乐在其中设计模式(C#) - 抽象工厂模式(Abstract Factory Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 抽象工厂模式(Abstract Factor ...

  3. 桥接模式及C++实现 C++设计模式-AbstractFactory抽象工厂模式

    桥接模式及C++实现 桥接模式 先说说桥接模式的定义:将抽象化(Abstraction)与实现化(Implementation)分离,使得二者可以独立地变化. 桥接模式号称设计模式中最难理解的模式之一 ...

  4. Java 设计模式之抽象工厂模式(三)

    原文地址:Java 设计模式之抽象工厂模式(三) 博客地址:http://www.extlight.com 一.前言 上篇文章 <Java 设计模式之工厂模式(二)>,介绍了简单工厂模式和 ...

  5. java设计模式之抽象工厂模式

    上一篇文章(http://www.cnblogs.com/liaoweipeng/p/5768197.html)讲了简单工厂模式,但是简单工厂模式存在一定的问题,如果想要拓展程序,必须对工厂类进行修改 ...

  6. php设计模式之抽象工厂模式

    之前总结了一下工厂模式,其实准确地说是简单 工厂模式,在它的基础上再进行抽象就可以得到一种新的模式:抽象工厂模式. 专业一点的定义为: 抽象工厂模式(Abstact Factory)是一种常见的软件设 ...

  7. PHP设计模式:抽象工厂

    示例代码详见https://github.com/52fhy/design_patterns 抽象工厂 抽象工厂(Abstract Factory)是应对产品族概念的.比如说,每个汽车公司可能要同时生 ...

  8. 再起航,我的学习笔记之JavaScript设计模式06(抽象工厂模式)

    我的学习笔记是根据我的学习情况来定期更新的,预计2-3天更新一章,主要是给大家分享一下,我所学到的知识,如果有什么错误请在评论中指点出来,我一定虚心接受,那么废话不多说开始我们今天的学习分享吧! 前两 ...

  9. C#设计模式(4)-抽象工厂模式

    引言 上一篇介绍了设计模式中的简单工厂模式-C#设计模式(3)-工厂方法模式,本篇将介绍抽象工厂模式: 抽象工厂模式简介 抽象工厂模式(AbstractFactory):提供一个创建一系列相关或相互依 ...

随机推荐

  1. dword word byte 相互转换 .xml

    pre{ line-height:1; color:#800080; background-color:#d2c39b; font-size:16px;}.sysFunc{color:#627cf6; ...

  2. 详解centos下vi的用法

    vi编辑器是所有Unix及Linux系统下标准的编辑器,它的强大不逊色于任何最新的文本编辑器,这里只是简单地介绍一下它的用法和一小部分指令.由于对Unix及Linux系统的任何版本,vi编辑器是完全相 ...

  3. 40个最好的Tumblr主题

    如果安装了一款较好的Tumblr主题,你的Tumblr空间将焕然一新.然而找到一款合适的主题并不是一件容易的事,这正是本文中我整理那么多优质的Tumblr模板作为灵感的原因.其中有一些免费的Tumbl ...

  4. gVIM 简洁配置 in Windows

    原文链接:http://www.errdev.com/post/2/ 捣鼓了一段时间的VIM,神器终归是神器,果然编码效率提升了许多,当然还需要很多插件来配合.自己装插件很麻烦,还要有Vundle这个 ...

  5. F2063 Could not compile used unit 'tt.pas'

    install packge error F2063 Could not compile used unit 'tt.pas' 有可能是工程的pas文件相对路径不对.在工程管理看是否能打开文件,如果打 ...

  6. ZOJ 3195 Design the city LCA转RMQ

    题意:给定n个点,下面n-1行 u , v ,dis 表示一条无向边和边权值,这里给了一颗无向树 下面m表示m个询问,问 u v n 三点最短距离 典型的LCA转RMQ #include<std ...

  7. HDU 5707 Combine String (DP,LCS变形)

    题意:给定三个字符串,问你第三个是不是由第一个和第二个组成的. 析:当时比赛是没有做出来啊...一直WA,就是没有判断长度,第一个和第二个和是不是和第三个一样,这个忘记... 我们用d[i][j]表示 ...

  8. 在Ribbon中,中文换行位置不对怎么办

    在Ribbon代码的编写时,关于UI上面设置Button的中文字,经常会出现不想换行的地方换行的情况,比如下图: 我如果希望中国一行,然后只有一行怎么办呢. 网上流传的很多方法就是直接加空格,但是很遗 ...

  9. 将UIImage保存成JPG或PNG格式存储在本地

    -(void)pngAndJpg:(UIImage*)image{ NSString *pngPath = [NSHomeDirectory() stringByAppendingPathCompon ...

  10. hdoj 5335 Walk Out

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5335 #include<stdio.h> #include<cstring> ...