抽象工厂模式:提供一个创建一系列相关或者相互依赖对象的接口,而无需指定它们具体的类。
优点:
1、抽象工厂模式:让具体的创建实例过程与客户端分离,客户端是通过它们的抽象接口操纵实例,产品的具体类名也被具体工厂的实现分离,不会出现在客户端中。
2、抽象工厂模式:完美的运用了开放-封闭原则、依赖倒转原则,在添加新的产品线时,不需要对原有代码做任何修改。
缺点:
1、抽象工厂模式:在添加新的行为时,需要编写大量类文件,切需要在原有的类中增加代码,长期维护可能造成项目过于庞大,不便于维护。
总结:
抽象工厂模式中主要的优点在于具体类的隔离,是的客户端不需要知道什么被创建了。其缺点在于增加新的等级产品结构比较复杂,需要修改接口及其所有子类
项目地址:https://github.com/GameRisker/Study-Design-Patterns.git
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
// // main.m // AbstractFactory // // Created by GameRisker on 16/2/14. // Copyright (c) 2016年 GameRisker. All rights reserved. // #import "AccessFactory.h" #import "IDepartment.h" #import "IFactory.h" #import "IUser.h" #import "SQLServerFactory.h" #import <Foundation/Foundation.h> int main(int argc, const char *argv[]) { @autoreleasepool { User *user = [[User alloc] init]; Department *depart = [[Department alloc] init]; //id<IFactory> factory = [[AccessFactory alloc] init]; id<IFactory> factory = [[SQLServerFactory alloc] init]; id<IUser> iu = [factory createUser]; [iu insert:user]; [iu getUser:@""]; id<IDepartment> ide = [factory createDepartment]; [ide insert:depart]; [ide getDepartment:@""]; } return 0; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
// // Header.h // AbstractFactory // // Created by GameRisker on 16/2/14. // Copyright (c) 2016年 GameRisker. All rights reserved. // #import "User.h" @protocol IUser <NSObject> - (void)insert:(User *)user; - (User *)getUser:(NSString *)userId; @end |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
// // IDepartment.h // AbstractFactory // // Created by GameRisker on 16/2/14. // Copyright (c) 2016年 GameRisker. All rights reserved. // #import "Department.h" #import <Foundation/Foundation.h> @protocol IDepartment <NSObject> - (void)insert:(Department *)department; - (Department *)getDepartment:(NSString *)departmentId; @end |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
// // IFactory.h // AbstractFactory // // Created by GameRisker on 16/2/14. // Copyright (c) 2016年 GameRisker. All rights reserved. // #import "IDepartment.h" #import "IUser.h" @protocol IFactory <NSObject> - (id<IUser>)createUser; - (id<IDepartment>)createDepartment; @end |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
// // SQLServerFactory.h // // // Created by GameRisker on 16/2/14. // // #import "IFactory.h" #import <Foundation/Foundation.h> @interface SQLServerFactory : NSObject <IFactory> @end |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
// // SQLServerFactory.m // // // Created by GameRisker on 16/2/14. // // #import "SQLServerDepartment.h" #import "SQLServerFactory.h" #import "SQLServerUser.h" @implementation SQLServerFactory - (id<IUser>)createUser { return [[SQLServerUser alloc] init]; } - (id<IDepartment>)createDepartment { return [[SQLServerDepartment alloc] init]; } @end |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
// // SQLServerUser.h // // // Created by GameRisker on 16/2/14. // // #import "IUser.h" #import <Foundation/Foundation.h> @interface SQLServerUser : NSObject <IUser> @end |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
// // SQLServerUser.m // // // Created by GameRisker on 16/2/14. // // #import "SQLServerUser.h" #import "User.h" @implementation SQLServerUser - (void)insert:(User *)user { NSLog(@"insert user to sqlserver!"); } - (User *)getUser:(NSString *)userId { NSLog(@"get department date form sqlserver!"); return nil; } @end |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
// // SQLServerDepartment.h // // // Created by GameRisker on 16/2/14. // // #import "IDepartment.h" #import <Foundation/Foundation.h> @interface SQLServerDepartment : NSObject <IDepartment> @end |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
// // SQLServerDepartment.m // // // Created by GameRisker on 16/2/14. // // #import "SQLServerDepartment.h" @implementation SQLServerDepartment - (void)insert:(Department *)department { NSLog(@"insert department to sqlserver!"); } - (Department *)getDepartment:(NSString *)departmentId { NSLog(@"get department date form sqlserver!"); return nil; } @end |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
// // AccessDepartment.h // // // Created by GameRisker on 16/2/14. // // #import "Department.h" #import "IDepartment.h" #import <Foundation/Foundation.h> @interface AccessDepartment : NSObject <IDepartment> @end |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
// // AccessDepartment.m // // // Created by GameRisker on 16/2/14. // // #import "AccessDepartment.h" @implementation AccessDepartment - (void)insert:(Department *)department { NSLog(@"insert department to access!"); } - (Department *)getDepartment:(NSString *)departmentId { NSLog(@"get department date form access!"); return nil; } @end |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
// // AccessFactory.h // // // Created by GameRisker on 16/2/14. // // #import "IFactory.h" #import <Foundation/Foundation.h> @interface AccessFactory : NSObject <IFactory> @end |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
// // AccessFactory.m // // // Created by GameRisker on 16/2/14. // // #import "AccessDepartment.h" #import "AccessFactory.h" #import "AccessUser.h" #import "IDepartment.h" #import "IUser.h" @implementation AccessFactory - (id<IUser>)createUser { return [[AccessUser alloc] init]; } - (id<IDepartment>)createDepartment { return [[AccessDepartment alloc] init]; } @end |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
// // AccessUser.h // // // Created by GameRisker on 16/2/14. // // #import "IUser.h" #import "User.h" #import <Foundation/Foundation.h> @interface AccessUser : NSObject <IUser> @end |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
// // AccessUser.m // // // Created by GameRisker on 16/2/14. // // #import "AccessUser.h" @implementation AccessUser - (void)insert:(User *)user { NSLog(@"insert user to access!"); } - (User *)getUser:(NSString *)userId { NSLog(@"get department date form access!"); return nil; } @end |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
// // User.h // // // Created by GameRisker on 16/2/14. // // #import <Foundation/Foundation.h> @interface User : NSObject @property(nonatomic, copy) NSString *userId; @property(nonatomic, copy) NSString *userName; @end |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
// // User.m // // // Created by GameRisker on 16/2/14. // // #import "User.h" @implementation User @end |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
// // Department.h // // // Created by GameRisker on 16/2/14. // // #import <Foundation/Foundation.h> @interface Department : NSObject @property(nonatomic, copy) NSString *departmentId; @property(nonatomic, copy) NSString *departmentName; @end |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
// // Department.m // // // Created by GameRisker on 16/2/14. // // #import "Department.h" @implementation Department @end |