Compared to the simple factory model, only one factory can produce the mobile phone is also fixed
The abstract factory model is similar to having many factories that create a factory to produce when a user wants to buy a cell phone.
For example, a user who wants to buy an iphone creates an apple factory to produce a mobile phone, and a goolge factory to buy an Android phone.
This creates the factory, the factory, the user this is what we have to consider (in fact, there is a cell phone, this is skipped in the Simple Factory mode)
Create a factory to have a management object to automatically create the appropriate plant according to the customer's needs
The factory is only responsible for the production of mobile phones.
Now to build the code
Write a basic factory model first
BaseFactory.h
1 #import <foundation/ Foundation.h> 2 3 @interface basefactory:nsobject 4 5 /* * 6 Create phone method 7 8 @return return the phone object 9 */ 10 -(id ) Createphone; 11 12 @end
Basefactory.m
1 #import "BaseFactory.h"2 3 @implementationbasefactory4 5- (ID) Createphone {6 7 //You can add code again to prohibit Basefactory calling this method directly8 //only subclasses are allowed to invoke9 returnNil;Ten } One A @end
To create an Apple factory
AppleFactory.h
1 #import " BaseFactory.h " 2 3 @interface applefactory:basefactory 4 5 @end
Applefactory.m
1 #import "AppleFactory.h"2 3 @implementationapplefactory4 5- (ID) Createphone {6 7NSLog (@"create an Apple phone");8 //this should be for the project. Add phone model, create and return phone object9 //now it's just an example of not realizing this part .Ten returnNil; One } A - @end
Create a Google factory
GoogleFactory.h
1 #import " BaseFactory.h " 2 3 @interface googlefactory:basefactory 4 5 @end
Googlefactory.m
1 #import "GoogleFactory.h"2 3 @implementationgooglefactory4 5- (ID) Createphone {6 7NSLog (@"Create a Google phone");8 //this should be for the project. Add phone model, create and return phone object9 //now it's just an example of not realizing this part .Ten returnNil; One } A - @end
Here's the factory manager.
FactoryManager.h is responsible for creating factories to produce according to user's requirement.
1 #import<Foundation/Foundation.h>2 #import "AppleFactory.h"3 #import "GoogleFactory.h"4 5typedefenum: Nsuinteger {6Kapple =0x1,7 Kgoogle,8 } efactorytype;9 Ten @interfaceFactorymanager:nsobject One A /** - Create a phone factory - the @param type Factory category - @return Return factory objects - */ -+ (ID) Createfactorywithtype: (efactorytype) type; + - @end
Factorymanager.m
1 #import "FactoryManager.h"2 3 @implementationFactorymanager4 5+ (ID) Createfactorywithtype: (efactorytype) type {6 7Basefactory *Factory;8 if(Type = =kapple) {9 TenFactory =[[Applefactory alloc] init]; One A}Else if(Type = =kgoogle) { - -Factory =[[Googlefactory alloc] init]; the - } - - returnFactory; + } - + @end
Here is the implementation
1 #import "ViewController.h"2 #import "FactoryManager.h"3 4 @interfaceViewcontroller ()5 6 @end7 8 @implementationViewcontroller9 Ten- (void) Viewdidload { One A [Super Viewdidload]; - - //Create a factory theApplefactory *factory =[Factorymanager createfactorywithtype:kapple]; - - //Factory production cell phone - [Factory Createphone]; + - } + A at - @end
Abstract Factory mode is more flexible than simple Factory mode
Objective-c Factory mode (bottom)--Abstract Factory mode