Factory method Mode:Defines the interface that creates an object, letting subclasses decide which class to instantiate. The factory method delays the instantiation of a class to its subclasses. Almost all applications written in object-oriented languages can see factory methods. "specifically defines a class that is responsible for creating instances of other classes, which typically have common parent classes." ”
Factory Method Mode usage scenario:The class of the object to be created is not exactly expected at compile time, and the class wants its subclasses to decide what to create at run time; The class has several helper classes for its subclasses, and the information that you want to return the subclass of is localized
Structure Chart:
ConcreteProduct1 and ConcreteProduct2 two products have a common parent class Iproject, Simple factory class for Simplefactory, Responsible for deciding whether to produce ConcreteProduct1 or ConcreteProduct2 products according to the different parameters passed in.
The code example 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
Create the factory class for the object
. h
@interface Animalfactory:nsobject
+ (DOG *) Createdog;
+ (Cat *) Createcat;
@end
. m
@implementation Animalfactory
+ (DOG *) createdog{
Dog *dog=[[dog Alloc]init];
dog.name=@ "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, the above method is required to dog *dog=[animalfactory Createdog]; This sentence is written in many different places in the program, so now there is a need, If you need to make all of the 100 dog objects that you create into a cat object, then, as you just did, you need to turn the Createdog method into the Createcat method in the 100-sentence code, which is complicated.
Then we can solve this problem by using the factory method model.
The factory method pattern creates 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 the 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 as the method invoked in 100 different places
Example
Start with the simplest simple factory model, and give an example of implementing a calculator to complete a simple factory model.
A simple calculator, with arithmetic to consider, subtraction, then beginners will feel very simple, use if conditions to judge, after the judge can complete the request, and a little bit of experience may choose switch case judgment, such as the following code:
Operation Logic
-(void) Operationwithnumbera: (double) Numbera withoperator: (char) operator Withnumberb: (double Numberb
{
/**
* encapsulates a method of passing a value *
* @param numbera number a
* @param operator Operator
* @param numberb number B
*
/double result = 0;
Switch (operator) {case
' a ': result
= Numbera + numberb;
break;
Case ' B ': result
= Numbera-numberb;
break;
Case ' C ': result
= Numbera * NUMBERB;
break;
Case ' d ':
if (numberb = 0) {
NSLog (@ "Divisor cannot be 0 please re-enter");
} else{result
= Numbera/numberb;
}
Case ' E ':
NSLog (@ "exit");
break;
Default: Break
;
}
And the client side of the code we can write this
/**
* arithmetic
-(void) operation
{
char A;
Double Numbera;
NSLog (@ "Please enter a number a");
scanf ("%lf", &numbera);
Double Numberb;
NSLog (@ "Please enter a number B");
scanf ("%lf", &numberb);
NSLog (@ "Add Please enter a");
NSLog (@ "Subtract Please enter B");
NSLog (@ "Multiply Please enter C");
NSLog (@ "Division Please enter D");
NSLog (@ "Exit Please enter E");
scanf ("%c", &a);
[Self Operationwithnumbera:numbera withoperator:a withnumberb:numberb];
}
After we get the value we need, we call the arithmetic to make a judgment and work out the result.
This will be clearer than if, because we have completely separated the business logic from the part shown in the interface, and we can copy the code directly to complete the operation wherever we need it.
But if, I have one day of operation demand is not satisfied with arithmetic, but want to add open radical or square of operation method, what to do. Do we have to go back to the switch statement to add judgment conditions, and then add a hint on the interface.
Before the code, we used only one of the three object-oriented features, that is, encapsulation, and to solve the question I raised in the previous paragraph, we can use two other features, polymorphism and inheritance to achieve.
In order to achieve the requirements before, without changing the other code, you can add more operations, or modify the problem of the operation method. Then we first put the arithmetic, encapsulated into four classes, namely the addition class, subtraction class, multiplication class, Division class.
@implementation addoperation/** * Addition * * + (Double) Addoperationwithnumbera: (double) Numbera Withnumberb: (Double)
B {Double result = 0;
result = Numbera + Numberb;
NSLog (@ "%f", result);
return result; @implementation suboperation/** * Subtraction * * (double) Suboperationwithnumbera: (double) Numbera Withnumberb: (Double)
Numberb {Double result = 0;
result = Numbera-numberb;
NSLog (@ "%f", result);
return result; @implementation muloperation/** * multiplication * * (double) Muloperationwithnumbera: (double) Numbera Withnumberb: (double) nu
Mberb {Double result = 0;
result = Numbera * NUMBERB;
NSLog (@ "%f", result);
return result; } @implementation divoperation/** * Division * * (Double) Divoperationwithnumbera: (double) Numbera Withnumberb: (double) num
Berb {Double result = 0;
if (Numberb = = 0) {NSLog (@ "Divisor cannot be 0 please re-enter");
}else{result = Numbera/numberb;
}
NSLog (@ "%f", result);
return result;
}
So we've encapsulated the arithmetic into four classes. Because lazy, I did not design interface model, just output the results, so the output of each nslog please do not mind.
Next, in the operation class of the simple factory, we implement the operation methods that call these four classes.
/**
* encapsulates an operation method *
* @param numbera number a
* @param operator operator
* @param Numberb Digital B
/
+ (void) Operationwithnumbera: (double) Numbera withoperator: (char) operator Withnumberb: (double) numberb
{
switch (operator) {case
' a ':
[addoperation Addoperationwithnumbera:numbera Withnumberb:numberb];
break;
Case ' B ':
[suboperation Suboperationwithnumbera:numbera withnumberb:numberb];
break;
Case ' C ':
[muloperation Muloperationwithnumbera:numbera withnumberb:numberb];
break;
Case ' d ':
[divoperation Divoperationwithnumbera:numbera withnumberb:numberb];
break;
Case ' E ':
NSLog (@ "exit");
break;
Default: Break
;
}
}
The above is in the simple factory class, calls the four arithmetic method class, realizes the computation, and the successful solution coupling, is advantageous in the later maintenance and the extension. The client side of the code is also very simple.
/**
* arithmetic
-(void) operation
{
char a = ' a ';
Double Numbera = ten;
Double numberb =; NSLog (@ "Add Please enter a"); NSLog (@ "Subtract Please enter B"); NSLog (@ "Multiply Please enter C"); NSLog (@ "Division Please enter D"); NSLog (@ "Exit Please enter E");
[Operation Operationwithnumbera:numbera withoperator:a withnumberb:numberb];
}
Have a better please recommend me to recommend thank you. Learn ing