Abstract Factory mode
Understand the factory method pattern, in fact, there are many similarities between abstract factory and factory method pattern. Abstract factories are also the separation of client object creation and logic code, but abstract factories often produce a set of data rather than just produce a product.
Abstract factories provide a class that creates a series of related or interdependent object interfaces without having to develop their specific classes.
Abstract Factory and Factory methods
Abstract Factory
Creating abstract products from object combinations
Create multi-Series products
The interface of the parent class must be modified to support the new product
Factory method
Creating abstract products with class inheritance
Create a product
Child Tear creator and Reload factory method to create new product
Demo
More than a few of the paste code, cut-type diagram
Client code:
#import<Foundation/Foundation.h>#import "Factory.h"#import "ProductType1.h"#import "ProductType2.h"#import "ConcreteFactory1.h"#import "ConcreteFactory2.h"intMainintargcConst Char*argv[]) {@autoreleasepool {Factory*factory = [ConcreteFactory1New]; ProductType1*PT1 =[Factory createProductType1]; ProductType2*pt2 =[Factory createProductType2]; NSLog (@"%@\n%@", Pt1.name,pt2.name); Factory*factory2 = [ConcreteFactory2New]; ProductType1*pt11 =[Factory2 CreateProductType1]; ProductType2*pt12 =[Factory2 createProductType2]; NSLog (@"%@\n%@", Pt11.name,pt12.name); } return 0;}
Run results
---£º04.277 Abstractfactory[35654: 8798008 ] PRODUCTTYPE1FAPRODUCTTYPE2FA--£ º 04.278 Abstract factory[35654:8798008] PRODUCTTYPE1FBPRODUCTTYPE2FB
As you can see, each factory produces a set of products.
It is also obvious to see the shortcomings of the abstract factory, whenever we need to add new products, we need to add products corresponding to the class, but also need to modify each factory to add a method for its creation.
Objective-c design mode-Abstract Factory mode abstraction Factory (object creation)