当前位置: 首页>编程语言>正文

iOS 工厂模式-简单工厂模式

工厂模式-简单工厂模式---简介:  工厂模式是创建类型的模式,为什么使用工厂模式?

原因:解除耦合

示例1:

```

//直接实例化对象  控制器和Button 之间建立关系 就有了耦合UIButton *button = [[UIButton alloc] init]; 解决方案:  间接创建 简单工厂模式(静态工厂模式)

ViewFactory.h

#import <Foundtion/Foundation.h>

#import <UIKIT/UIKit.h>

@interface ViewFactory : NSObject+ (NSObject *)creatView;

@endViewFactory.m

#import ViewFactory.h

@implamentation ViewFactiory

+(NSObject *)creatView

{return [[UIButton alloc] init];}

@end

```

![IMAGE001](image/9F203959-9442-4094-B198-A655BC3E97E5.png)

由于[**示例1**](#markdown-pane)并不完善,相对来说 一个类只能生成一个对象,会造成大量不变,可以查看[**示例2**](#示例2)

iOS 工厂模式-简单工厂模式,第1张

示例2:

```

ViewFactory.h

#import

#import

@interface ViewFactory1 : NSObject

+ (NSObject *)creatView:(NSIntger):chose;

@end

ViewFactory.m

#import

@implamentation ViewFactiory1

//根据条件定制对象,有一种增加一种,相对于[示例1]来说 增加了少许的拓展性,这种拓展方法跟[UIButton buttonWithType:UIButtonTypeContactAdd] 实现方式是类似的 buttonWithType: 基于在在父类中做了一系列的判断 并且创建出来

+ (NSObject *)creatView:(NSIntger):chose

{

NSObject *obj = nil;

switch(chose)

{case 1:obj = [[UIButton alloc] init];

break;case 2:

//也可以创建不同的类  比如UIImageView  UITextField 等等

obj = [UIButton buttonWithType:UIButtonTypeContactAdd];

break;

default:

obj = [[UIButton alloc] init];

break;

}

return obj;

}@end

```

![IMAGE002](image/825CB5B5-01F5-4555-900A-7FF4AD8D878F.PNG)

iOS 工厂模式-简单工厂模式,第2张

相对应于[**示例2**](#示例2), 如果添加新的类,就需要不停的更改ViewFactory类文件,这是我们不想看到的,频繁的更改中间文件,所以我们拓展了[**示例3**](#示例3)

示例3:

[ViewFactoryConfing](image/ViewFactoryConfing.xml)

```ViewFactory.h

#import

typedef enum 

{    kCreatedControlTypeButton      = 1, 

   kCreatedControlTypeImageView    = 2   

 }kCreatedControlType;

@interface ViewFactory : NSObject

+ (id)getInstance:(NSInteger)chosse;

@end

ViewFactory.m

#import "ViewFactory.h"

#import "ViewXmlParser.h"

@implementation ViewFactory

+ (id)getInstance:(NSInteger)chosse { 

   ViewXmlParser *parser = [[ViewXmlParser alloc] init];   

 NSMutableDictionary *dict = [parser parser];   

 NSString *className = [NSString stringWithFormat:@"%ld",(long)chosse];   

 return [[[NSClassFromString([dict objectForKey:className]) class] alloc] init];}

@end

ViewXmlParser.h

#import

@interface ViewXmlParser : NSObject

- (NSMutableDictionary *)parser;

@endViewXmlParser.m

#import 

"ViewXmlParser.h"

@interface ViewXmlParser ()

@property (nonatomic, strong) NSMutableDictionary *dict;

@property (nonatomic, strong) NSString *currentElementName;

@end

@implementation ViewXmlParser

- (instancetype)init{   

 self = [super init]; 

   if (self) 

{      

  _dict = [NSMutableDictionary dictionary];   

 }    

return self;}

#pragma mark - Getter

- (NSMutableDictionary *)parser {  

  NSString *path = [[NSBundle mainBundle] pathForResource:@"ViewFactoryConfing" ofType:@"xml"];    

NSURL *url = [[NSURL alloc] initFileURLWithPath:path];   

 NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:url];    parser.delegate = self;    

[parser parse];  

  return _dict;}

#pragma mark - NSXMLParserDelegate

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary*)attributeDict{

_currentElementName = elementName;

if ([elementName isEqualToString:@"view"]) {

NSString *key = [attributeDict objectForKey:@"key"];

NSString *className = [attributeDict objectForKey:@"className"];

[_dict setObject:className forKey:key];

}

}

```

相对来说[示例3](#示例3)只需要**ViewFacyoryConfing.xml**这个配置文件, 其实苹果的**故事版**和**XIB**也是用工厂模式写的

上诉,说的不对的,请老铁们斧正


https://www.xamrdz.com/lan/5vg2016068.html

相关文章: