For example:
Pre-acquisition – Simple Factory Model:
Story as follows, a the elder brothers nicknamed daikin chain, daikin chain is influenced by parents as a child, especially with business acumen, on one occasion, daikin chain to the ground, look at the current do a clothes shop, business is too hot, in the beginning, shop production only pants, shop.our kid sou have no deceit in business philosophy, has won the popularity of distance merchants, So good business every day, and the subsequent also do all kinds of clothes, but daikin chain, business is good, but the size is too small just in the small town, thus daikin chain is wanting to buy the clothes shop, the shop owner is an old and older couple, don’t want to so busy because of the old, So it was transferred to the gold chain.
After Taking over the store, Dajinchain felt that the store had a good reputation, so it continued to expand with the business philosophy of the store. At the beginning, the store’s business scope was only in the local area, making clothes and pants, which could be called a simple factory at this time.
Post-acquisition – Factory Approach:
But after daikin chain made consolidation, due to the scope of sales in more than one county, shop does not follow to go up the output of this time, so the shwedagon chain will store business is divided into several single category of plant, such as clothing factory, pants factory and so on, he to the county seat of merchants to provide the single category of clothes. The merchant will order the clothes in the corresponding factory. At this time, the factory of each category is the factory method (there are several large categories of factories).
After the reform – Abstract Factory:
With the passage of time, the store’s reputation is becoming more and more big, because the store each category demand for clothes is different, the different levels of waste, which shop has carried on the reform, to enter the high-end apparel market, shop to provide at this time can only be garment, thus daikin chain is put forward a new type of plant – design of high-end clothing (design department), This factory is used to manage other factories of single category, and combine the clothes of other factories of single category to form high-end fashion clothes. At this time the design department can be called abstract factory, and after the business to buy clothes to his design department to order.
What is the difference between the factory method pattern and the Abstract factory pattern? 1. Both the factory method and the abstract factory are responsible for instantiating objects, but the factory method uses inheritance while the abstract factory method uses composition. 2. The factory approach focuses on decoupling object instantiation code from concrete classes — through subclass inheritance, or when you don’t know which concrete classes will be instantiated in the future. Abstract factories focus on when you need to create a family of products and concentrate the related products you want to make.
Before we get into factory mode, OC doesn’t provide an abstract class, so we have to implement it ourselves if we need to, which is essentially rewriting init, and if we call init of this class directly returns nil.
- (instancetype) init {the if ([self isMemberOfClass: YiFu. Class]) {/ * * must use isMemberOfClass here, Separate isKindOfClass * / [self doesNotRecognizeSelector: _cmd]; return nil; } self = [super init]; if (self) { } return self; }Copy the code
Simple factory
I’ll just do it in code
Let’s go shopping for clothes. We have sweaters and hoodies
-
Create a protocol YiFuProtocol
@protocol YiFuProtocol (NSString *)name; @end
2. Create MaoYi and WeiYi classes to comply with YiFuProtocol
@interface WeiYi : NSObject <YiFuProtocol>
@end
@implementation WeiYi
- (NSString *)name {
return @"WeiYi L";
}
@end
@interface MaoYi : NSObject <YiFuProtocol>
@end
@implementation MaoYi
- (NSString *)name {
return @"MaoYi L";
}
@end
Copy the code
3. Create the factory class
@interface YiFuFactory : NSObject + (id<YiFuProtocol>)yifuWithType:(NSString *)type; @end@implementation YiFuFactory + (id<YiFuProtocol>)yifuWithType:(NSString *)type {if ([type isEqualToString:@" 一 "]) { return [[MaoYi alloc] init]; }else if ([type isEqualToString:@" WeiYi "]) {return [[WeiYi alloc] init]; } return nil; } @endCopy the code
4. Call
Id <YiFuProtocol> obj = [YiFuFactory yifuWithType:@" sweater "];Copy the code
2. Factory method pattern
-
Create the factory abstract class
-
@protocol YiFuMakeProtocol <NSObject> - (id<YiFuProtocol>)createYiFu; @end Copy the code
-
Creating a factory entity
-
@interface MaoYiFactory: NSObject <YiFuMakeProtocol> @end @implementation MaoYiFactory - (id<YiFuProtocol>)createYiFu { return [[MaoYi alloc] init]; } @end // @interface WeiYiFactory: NSObject <YiFuMakeProtocol> @end @implementation WeiYiFactory - (id<YiFuProtocol>)createYiFu { return [[WeiYi alloc] init]; } @endCopy the code
-
The product abstractions are the same sweaters and hoodies that we had before
-
@interface WeiYi : NSObject <YiFuProtocol> @end @implementation WeiYi - (NSString *)name { return @"WeiYi L"; } @end @interface MaoYi : NSObject <YiFuProtocol> @end @implementation MaoYi - (NSString *)name { return @"MaoYi L"; } @end Copy the code
-
Client call
id factory = [[MaoYiFactory alloc] init];
id yifu = [factory createYiFu];
NSLog(@”size : %@”,[yifu name] );
3. Abstract factory pattern
Now customers are buying clothes and pants
First of all, an agreement is provided to create clothes and trousers. Now there are two factories: FACTORY A produces sweaters and woolen trousers, and factory B produces sweaters and trousers.
We have already had the agreement on clothes, so let’s have another agreement on pants. In order not to repeat the agreement on clothes, let’s assume that it is material
@protocol KuZiMakeProtocol <NSObject>
- (NSString *)cailiao;
@end
Copy the code
Create two product categories of fleece pants
@interface MaoKu : NSObject <KuZiProtocol>
@end
@implementation MaoKu
- (NSString *)cailiao {
return @"maoku C";
}
@end
@interface WeiKu : NSObject <KuZiProtocol>
@end
@implementation WeiKu
- (NSString *)cailiao {
return @"weiku C";
}
@end
Copy the code
Factory Abstract class
@protocol ShopProtocol <NSObject>
- (id<YiFuProtocol>)createYiFu;
- (id<KuZiProtocol>)createKuZi;
@end
Copy the code
A, B factory
@interface A : NSObject <ShopProtocol>
@end
@implementation A
- (id<YiFuProtocol>)createYiFu {
return [[MaoYi alloc] init];
}
- (id<KuZiProtocol>)createKuZi {
return [[MaoKu alloc] init];
}
@end
@interface B : NSObject <ShopProtocol>
@end
@implementation B
- (id<KuZiProtocol>)createKuZi {
return [[WeiKu alloc] init];
}
- (id<YiFuProtocol>)createYiFu {
return [[WeiYi alloc] init];
}
@end
Copy the code
Client call
id<ShopProtocol> shop = [[A alloc] init];
id<YiFuProtocol> yifu = [shop createYiFu];
id<KuZiProtocol> kuzi = [shop createKuZi];
Copy the code
4. To summarize
Simple factory
Advantages: Different objects can be created based on different conditions, separating responsibilities and permissions, and optimizing project structure Disadvantages: all instance creation is clustered together
The factory method
The core factory class is no longer responsible for concrete product creation, delegating work to subclasses and providing only the interfaces that subclasses must implement. In this case, protocol is used. A better approach would be an abstract class that provides a default implementation and can be overridden if subclasses need it. The user only needs to care about the methods provided by the interface when using the product.
The abstract factory
This pattern refers to the factory pattern used when there are multiple abstract roles. Definition: Provides an interface for creating a set of related or interdependent objects without specifying their concrete classes.