博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
华山论剑之浅谈iOS的生产线 工厂模式
阅读量:6984 次
发布时间:2019-06-27

本文共 2984 字,大约阅读时间需要 9 分钟。

工厂模式是一种快速创建大量对象的模式.简单易上手,今天我就谈一下我对工厂模式的理解.工厂模式的核心思想就是继承.子类继承父类的方法并且重写父类方法.下面我们就看一下实际中是如何使用工厂模式的.

*** 现在我们需要创建一百个对象,其中五十个是学生对象,五十是老师对象.两者都继承与Person类.

现在我们需要在Person.h中声明一个创建方法.但是在Person.m中不做实现.

#import 
@interface Person : NSObject@property(nonatomic,strong)NSString *name;@end复制代码

然后我们需要在Student类中和Teacher类中对createNewObject方法进行方法的实现.当然了,Student类和Teacher类是继承于Person类的.

Student.中代码实现

#import "Person.h"@interface Student : Person@end复制代码
#import "Student.h"@end复制代码

Teacher中代码实现如下

#import "Person.h"@interface Teacher : Person@end复制代码
#import "Teacher.h"@implementation Teacher@end复制代码

下面就需要创建批量生产的初始化工厂PersonFactory.

PersonFactory.h中

#import 
@class Teacher;@class Student;@interface PersonFactory : NSObject//声明创建新对象的方法.+(Student *)createNewStudent;+(Teacher *)createNewTeacher;@end复制代码

PersonFactory.m中

#import "PersonFactory.h"#import "Student.h"#import "Teacher.h"@implementation PersonFactory//实现创建新对象的方法.+(Student *)createNewStudent{    Student *student = [[Student alloc]init];        student.name = @"栋哥";        return student;    }+(Teacher *)createNewTeacher{    Teacher *teacher = [[Teacher alloc]init];        teacher.name = @"政哥";        return teacher;    }@end复制代码

这里,我就使用ViewController调用一下createNewObject方法.然后我们就能批量生产带有特定name属性的学生对象和教师对象了.

#import "ViewController.h"#import "Teacher.h"#import "Student.h"#import "PersonFactory.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];        Teacher *teacher = [PersonFactory createNewTeacher];        NSLog(@"%@",teacher.name);        Student *student = [PersonFactory createNewStudent];        NSLog(@"%@",student.name);    }@end复制代码

通过上面的方法,我们只能批量创建一些带有特殊属性的对象,如果现在我们需要把这些对象全部改变成另外的对象,我们改如何做呢? 这就需要我们再一步的细分我们的工厂.Person工厂中只做方法的声明,在Student工厂和Teacher中做出方法的实现.

PersonFactory.h中

#import 
@class Person;@interface PersonFactory : NSObject//只在.h中做方法的声明,在.m中不做方法的实现.+(Person *)createNewObject;@end复制代码

StudentFactory.m中

#import "StudentFactory.h"#import "Student.h"@implementation StudentFactory+(Person *)createNewObject{    Student *student = [[Student alloc]init];        student.name = @"栋哥";        return student;}@end复制代码

同样.TeacherFactory.m中.

#import "TeacherFactory.h"#import "Teacher.h"@implementation TeacherFactory+(Person *)createNewObject{        Teacher *teacher = [[Teacher alloc]init];        teacher.name = @"政哥";        return teacher;    }@end复制代码

然后我们在ViewController中创建我们的两种对象.

#import "ViewController.h"#import "Person.h"#import "PersonFactory.h"#import "StudentFactory.h"#import "TeacherFactory.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];        Person *teacher = [TeacherFactory createNewObject];        NSLog(@"%@",teacher.name);        Person *student = [StudentFactory createNewObject];        NSLog(@"%@",student.name);    }@end复制代码

如上,如果我们需要在对我们的两种对象创建做修改只需要修改我们的生产工厂的类中的代码就行,不需要修改我们徐如其他地方的代码了,大大的减省了冗杂度和耦合度,今天的iOS工厂模式就到此结束了,希望这一篇文章能对您有所帮助.?

转载地址:http://txtpl.baihongyu.com/

你可能感兴趣的文章
我的友情链接
查看>>
[IOS]clang diagnostic、Wprotocol ..
查看>>
HTML4.01规范-HTML文档的顶层结构(3)
查看>>
11个鲜为人知的实用Linux命令 - Part 2
查看>>
我国今日接连发生4起地震 震级均在3级以上
查看>>
优化PhoneGAP的Splashscreen 类
查看>>
增强 wp_list_authors 显示文章最多的作者
查看>>
代码:显示查询的日历
查看>>
完整的目标管理三段俱全
查看>>
AD 脚本kixtart运用之六(outlook邮件批量生成签名)
查看>>
Tomcat 日志
查看>>
优化SQL查询:如何写出高性能SQL语句
查看>>
误删/etc/passwd的修复
查看>>
Linux图形界面与命令行模式切换
查看>>
CSS选择器
查看>>
UV认证应用安全标准的目的
查看>>
用VisualVM分析MyEclipse
查看>>
在android开发中使用multdex的方法-IT蓝豹为你整理
查看>>
Oracle创建表空间和用户
查看>>
基于IP访问控制的局限性
查看>>