设计模式是程序提升的必备知识,这里说下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. Anychart 破解备注

    由于项目里用到anychart组件,第一次破解了,后来升级再破解时忘了方法,所以在这里备注一下. 首先需要的工具: swfc  (http://www.buraks.com/swifty/swfc.h ...

  2. Topogun教学视频

    http://www.iqiyi.com/w_19rrfss6dd.html http://www.iqiyi.com/w_19rrfsvo3h.html http://www.iqiyi.com/w ...

  3. Intent传递数据

    方式比较多,先看看代码,一会儿再总结. activity_main.xml <RelativeLayout xmlns:android="http://schemas.android. ...

  4. @Html.Raw()

    asp.net mvc中把html字符串以html效果输出来, @string变更输出的是HTML代码, 如果想以HTML标签效果输出来可以用函数@Html.Raw(str) 输出来的就是网效果了, ...

  5. 用pdo实现的织梦后台留言板

    <?php //ini_set("display_errors", "On"); include("data/common.inc.php&qu ...

  6. ssh-keygen -t rsa -f cloud.key ssh -i cloud.key <username>@<instance_ip>

  7. django1.77+mod_wsgi+python2.79+apache2.24 在阿里云centos部署攻略

    心平气和的记录一下今天 踩的坑以防万一 以后还踩 首先我今天的平台是在 阿里云上的一台纯净版的 centos6.5 64位主机上进行的 首先装python2.7 去官网下载python2.7安装包 然 ...

  8. 第三百二十一天 how can I 坚持

    上班第一天,感觉时间过得好慢. 心里好烦,做什么都没心情,感觉没有勇气了,虽然早上说了那么多,但不敢去面对了. 咋整? <猪老三><野子>. 好想去看<美人鱼> 不 ...

  9. labview在线帮助网址

    http://zone.ni.com/reference/zhs-XX/help/371361L-0118/ labview网络讲坛 网址 http://v.eepw.com.cn/video/com ...

  10. ASP.NET前台AJAX方法调用后台的方法写法

    前台: <input id="AjaxDemo" type="button" onclick="get()" value=" ...