Factory mode is the most common design pattern in development, simple Factory mode, Factory mode and abstract Factory mode, all belong to the Factory mode. Simple Factory is the creation mode of the class, the static Factory method (Factory) mode, A simple factory model is a factory class that determines which product class to create based on the parameters passed in. Simple Factory mode will contain too many judgment conditions, maintenance is not particularly convenient, Factory mode is mainly through dependency inversion to defer the instantiation of the class into subclasses, to achieve dynamic expansion. Abstract Factory mode is an object product family that provides different objects depending on the requirements.
Simple Factory mode
A simple factory model was written in the previous article, assuming that we have a garment processing plant that can produce different costumes according to different needs, defining a clothing base class and a factory class:
@protocol dressprotocol <NSObject> @optional-(void) providematerial; @optional-(void) product; @end @interface Dress:nsobject<dressprotocol> @end
Costume Dress sub-class Foreigndress:
@implementation foreigndress-(void) providematerial{ NSLog (@ "foreigndress--prepare raw Materials");} -(void) product{ NSLog (@ "foreigndress--production");} @end
Chinadress sub-class:
@implementation chinadress-(void) providematerial{ NSLog (@ "Chinadress---prepare raw materials");} -(void) product{ NSLog (@ "chinadress---production");} @end
Factory class Manufactor:
@protocol manufactorprotocol <NSObject> @optional-(Dress *) createdress: (NSString *) type; @end @interface manufactor:nsobject<manufactorprotocol>-(void) Start: (NSString *) type;-(void) Simplestart: (NSString *) type ;-(void) Startbycondition: (NSString *) type; @end
Method implementation:
-(void) Start: (NSString *) type{ if ([Type isequaltostring:@ "Foreign"]) { dress=[[foreigndress alloc]init]; } else if ([Type isequaltostring:@ "China"]) { dress=[[chinadress alloc]init]; } [Dress providematerial]; [Dress product];} Blog Park-flyelephant Simple Factory-(void) Simplestart: (NSString *) type{ dress=[manufactory Dressinstance:type]; [Dress providematerial]; [Dress product];}
Method invocation:
Manufactor *factor=[[manufactor Alloc]init]; [Factor start:@ "Foreign"]; [Factor simplestart:@ "China"]; NSLog (@ "blog Park-flyelephant"); NSLog (@ "http://www.cnblogs.com/xiaofeixiang/");
Test results:
The first is that we are directly judging by type types in the factory, different types produce different garments, but if the type is too large and the type implements not necessarily the dress clothing class, so put in manufactor is not suitable, we put its implementation in a simple factory alone. You will find that each of the different types we have to modify the simple factory, pull a move, not in line with the design pattern of the "open to expand, modify the closed" principle, the factory model can solve the problem of simple factory cannot solve.
Factory mode
Assuming the company benefit is better, the factory needs to add Beijing branch, Shanghai branch time, this is the time through the simple factory can not solve the problem of class expansion, simple factory is simple is in the same place for object processing, factory method mode (Factory Pattern) creates an abstract method in a base class that can be overridden by a subclass to alter the process of creating an object. The factory method pattern allows subclasses to decide how to create objects to achieve encapsulation purposes. Suppose we add the Beijing branch, the clothing belonging to the Beijing branch can directly call the Beijing factory to complete:
@implementation bjmanufactor-(Dress *) createdress: (NSString *) type{ Dress *dress; if ([Type isequaltostring:@ "BJ"]) { dress=[[bjdress alloc]init]; } else if ([Type isequaltostring:@ "Bjspecial"]) { dress=[[bjspecialdress alloc]init]; } return dress;} @end
Manufactor the conditions in the base class:
-(void) Startbycondition: (NSString *) type{ Dress *mydress=[self Createdress:type]; [Mydress providematerial]; [Mydress product];}
Specific invocation implementations:
Manufactor *bjfactor=[[bjmanufactor Alloc]init]; [Bjfactor startbycondition:@ "BJ"]; NSLog (@ "blog Park-flyelephant"); NSLog (@ "http://www.cnblogs.com/xiaofeixiang/");
Effect:
from the object's creation, this extends the creation of objects to the object subclass, from the point of view of the dependency in the design principle, the original factory needs to rely on a variety of specific clothing sub-class references, and now these are classified, here is the form of factory classification, abstract components do not rely on specific implementation components, Extensions that have been completed may not need to be changed, and if there are additional requirements only need to add new functional interfaces, the high-level components do not need to change too much. Abstract Factory modeAbstract Factory mode provides an interface for creating an object family without specifying a specific class. The factory method only involves the creation of an object, sometimes we need a family of objects, which I object to the factory, when we need the factory to provide different raw materials, the need for different starting capital and number of people, in order to set up a real factory, each plant is aggregated together to form a unified interface, So we can call it
Abstract factory base class, which only provides the implementation of cloth:
@protocol abstractfactory <NSObject> @optional-(Cloth *) Providecloth; @end @interface Abstractfactory:nsobject <AbstractFactory> @end
Abstract Factory sub-class Bjabstractfactory:
@implementation bjabstractfactory-(cloth *) providecloth{ return [[Cloth alloc] init];} @end
Cloth class:
@implementation cloth-(void) clothmethod{ NSLog (@ "Cloth--cloth");} @end
Add two new methods to the Manufactor class:
-(void) Configfactory: (Abstractfactory *) factory{ abstractfactory=factory;} -(void) Startwithfactory: (NSString *) type{ Dress *mydress=[self Createdress:type]; Cloth=[abstractfactory Providecloth]; [Cloth clothmethod]; [Mydress providematerial]; [Mydress product];}
Method invocation:
Manufactor *factor=[[bjmanufactor Alloc]init]; [Factor Configfactory:[[bjabstractfactory alloc] init]]; [Factor startwithfactory:@ "BJ"]; NSLog (@ "blog Park-flyelephant"); NSLog (@ "http://www.cnblogs.com/xiaofeixiang/");
Effect:
From the implementation logic to the abstract factory pattern and the factory pattern and similar, the abstract factory pattern focuses on creating an abstract interface to create a set of related products. Abstract Factory mode, an abstract factory defines an interface that all specific factories must implement that interface, which contains a set of methods used to produce products. Each specific plant is capable of producing an entire set of products. The best choice in engineering practice according to the actual situation ~
iOS Development-Factory mode