Factory mode My understanding is that he's just trying to create an object
When creating an object, we are generally alloc an object, if we need to create 100 such objects, if it is in a for loop is good to say, a straight sentence alloc on the line, but the fact is not so good, we may be in different places to create this object, Then we may need to write 100 sentences alloc, but if we create the object, we need to add a fixed value for one of its properties after the object is created, for example, the students of XXX school, then there may be more than 100 lines of duplicate code, then, if you write a-( void) Createobj method, the creation of objects and school properties written in this method inside, then it will be much easier, that is to say we can alloc create objects encapsulated into a method inside, call this method directly, this is the simple factory method
The code structure is as follows
Animal class
@interface Animal:nsobject
@proterty (Nonatomic,strong) NSString *name;
-(void) laugh;
@end
Dog class
@interface Dog:animal
@end
Cat class
@interface Cat:animal
@end
To create a factory class for an object
. h
@interface Animalfactory:nsobject
+ (DOG *) Createdog;
+ (Cat *) Createcat;
@end
. m
@implementation Animalfactory
+ (DOG *) createdog{
Dog *dog=[[dog Alloc]init];
[Email protected] "baby";
return dog;
}
+ (Cat *) createcat{
Cat *cat=[[cat Alloc]init];
return cat;
}
MAIN.M file
Dog *dog=[animalfactory Createdog];
Cat *cat=[animalfactory Createcat];
This is a simple factory model
Now create 100 dog objects, if these 100 objects are written in different places in the program, pressing the top method is required to put dog *dog=[animalfactory Createdog]; This sentence is written in a lot of different places in the program, then there is a need now, That is, if you need to change all of the 100 dog objects created into a Cat object, then as you just did, you need to turn the Createdog method into a Createcat method in these 100 lines of code, which is still complicated.
So this is the time to use the factory approach to solve this problem.
The factory method pattern is to create a factory for each class that contains the object to be created.
The code is as follows
@interface Animalfactory:nsobject
-(animal*) Createanimal;
@end;
Dog Factory class
@interface Dogfactory:animalfactory;
@implementation Dogfactory
-(Animal *) createanimal{
Retrurn [[Dog alloc]init];
}
@end
Cat Factory class
@interface Catfactory:animalfactory;
@implementation Cat Factory
-(Animal *) createanimal
Retrurn [[Cat alloc]init];
}
@end
Main.m
Animalfactory *dogfactory=[[dogfactory Alloc]init];
Animal *animal1=[dogfactory Createanimal];
[Animal1 laugh];
Animal *animal2=[dogfactory Createanimal];
[Animal2 laugh];
.......
Animal *animal100=[dogfactory Createanimal];
[Animal100 laugh];
So if you want to change 100 dog to cat, you just need to change the dogfactory to Catfactory.
But the factory approach also has its limitations:
1. The class to be created must have the same parent class
2. The class to be created must be the same method that is called in 100 different places
These are just a person sentiment, there will be some deficiencies, please help correct, hey
http://blog.csdn.net/xiazailushang/article/details/19040117
The Factory mode of IOS design mode