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];
iOS 设计模式之抽象工厂的更多相关文章
- iOS设计模式 - (3)简单工厂模式
iOS设计模式 - (3)简单工厂模式 by Colin丶 转载请注明出处: http://blog.csdn.net/hitwhylz/article/ ...
- 乐在其中设计模式(C#) - 抽象工厂模式(Abstract Factory Pattern)
原文:乐在其中设计模式(C#) - 抽象工厂模式(Abstract Factory Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 抽象工厂模式(Abstract Factor ...
- 桥接模式及C++实现 C++设计模式-AbstractFactory抽象工厂模式
桥接模式及C++实现 桥接模式 先说说桥接模式的定义:将抽象化(Abstraction)与实现化(Implementation)分离,使得二者可以独立地变化. 桥接模式号称设计模式中最难理解的模式之一 ...
- Java 设计模式之抽象工厂模式(三)
原文地址:Java 设计模式之抽象工厂模式(三) 博客地址:http://www.extlight.com 一.前言 上篇文章 <Java 设计模式之工厂模式(二)>,介绍了简单工厂模式和 ...
- java设计模式之抽象工厂模式
上一篇文章(http://www.cnblogs.com/liaoweipeng/p/5768197.html)讲了简单工厂模式,但是简单工厂模式存在一定的问题,如果想要拓展程序,必须对工厂类进行修改 ...
- php设计模式之抽象工厂模式
之前总结了一下工厂模式,其实准确地说是简单 工厂模式,在它的基础上再进行抽象就可以得到一种新的模式:抽象工厂模式. 专业一点的定义为: 抽象工厂模式(Abstact Factory)是一种常见的软件设 ...
- PHP设计模式:抽象工厂
示例代码详见https://github.com/52fhy/design_patterns 抽象工厂 抽象工厂(Abstract Factory)是应对产品族概念的.比如说,每个汽车公司可能要同时生 ...
- 再起航,我的学习笔记之JavaScript设计模式06(抽象工厂模式)
我的学习笔记是根据我的学习情况来定期更新的,预计2-3天更新一章,主要是给大家分享一下,我所学到的知识,如果有什么错误请在评论中指点出来,我一定虚心接受,那么废话不多说开始我们今天的学习分享吧! 前两 ...
- C#设计模式(4)-抽象工厂模式
引言 上一篇介绍了设计模式中的简单工厂模式-C#设计模式(3)-工厂方法模式,本篇将介绍抽象工厂模式: 抽象工厂模式简介 抽象工厂模式(AbstractFactory):提供一个创建一系列相关或相互依 ...
随机推荐
- 关于C++虚函数的一些东西
先上概念,C++的多态性:系统在运行时根据对象类型,来确定调用哪个重载的成员函数的能力. 多态性是通过虚函数实现的.成员函数之前加了virtual,即成为虚函数. 有虚成员函数的类,编译器在其每个对象 ...
- ref 参数
当使用ref 作为参数赋值时,ref 得需要初始化,就是在从新定义一下 参数的值,下面有列子: 在控制台中运行如下: //定义一个方法,两个参数 i和a . public static void ge ...
- 免费CDN
什么是CDN? CDN (Content Delivery Network) ,CDN 是包含可分享代码库的服务器网络. CDN公共库是指将常用的JS库存放在CDN节点,以方便广大开发者直接调用.与将 ...
- TCP/IP 相关知识点与面试题集
第一部分:TCP/IP相关知识点 对TCP/IP的整体认 链路层知识点 IP层知识点 运输层知识点 应用层知识点 (这些知识点都可以参考:http://www.cnblogs.com/newwy/p/ ...
- Redis批量导入数据
首先准备数据文件 格式为 SET Key0 Value0 SET Key1 Value1 ... SET KeyN ValueN 利用shell转换数据 #!/bin/bash while read ...
- Swift 脚本(运行时带参数)
#!/usr/bin/env xcrun swift import Foundation let args = Process.arguments print("Arg:\(args)&qu ...
- html5 canvas 钟表
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- 轻松学习Linux之VI编辑器的使用
本文出自 "李晨光原创技术博客" 博客,谢绝转载!
- 轻松学Shell之认识正规表达式
离线下载观看:http://down.51cto.com/data/148117 650) this.width=650;" onclick='window.open("htt ...
- Advanced Scene Processing
[How a Scene Processes Frames of Animation] In the traditional view system, the contents of a view a ...