Application of the model of simple factory and factory method in the development of IOS applied Design Model _ios

Source: Internet
Author: User

Simple Factory mode
just like the name of the pattern, the simple factory pattern is basically the simplest of all design patterns, and the relationship between classes and classes is straightforward. This time I will use the example of a lot of places--calculator, to illustrate this pattern. First, let's show you the structure diagram between classes:

Through this chart, you can see clearly, addition class, subtraction class, multiplication class, Division class inherits from the operation class, Simple factory class relies on the instantiation of the Operation class to realize the corresponding operation function, well, it doesn't look complicated, let's show the code directly (given the current point does not support objective C's code is highlighted, so write it directly, try to keep it neat. Another, in order to take care of like me, the foundation is not very good students, I try to write the code, convenient for everyone to debug.

Note: All of the code in this article is compiled in an arc environment.

The first is the Operation class (The parent Class):
Interface file:

Copy Code code as follows:

#import <Foundation/Foundation.h>

@interface operation:nsobject{
Double Numbera;
Double Numberb;
}
@property double Numbera;
@property double Numberb;
-(double) getresult;
@end


Implementation file:
Copy Code code as follows:

#import "Operation.h"

@implementation Operation
@synthesize Numbera, Numberb;

-(double) getresult{
return-1.0; Return by default here-1.0, no other meaning
}

@end


Addition Class (operator subclass):
Interface file:
Copy Code code as follows:

#import "Operation.h"

@interface operationadd:operation
@end


Implementation file:
Copy Code code as follows:

#import "OperationAdd.h"

@implementation Operationadd

-(double) getresult{
Double result = 0;
Result =numbera+numberb;
return result;
}

@end


Subtraction Class (operator subclass):
Interface file:
Copy Code code as follows:

#import "Operation.h"
@interface operationsub:operation
@end

Implementation file:
Copy Code code as follows:

#import "OperationSub.h"

@implementation Operationsub

-(double) getresult{
Double result = 0;
result = Numbera-numberb;
return result;
}

@end


Multiplication Class (operator subclass)
Copy Code code as follows:

#import "Operation.h"
@interface operationmul:operation
@end

Implementation file:
Copy Code code as follows:

#import "OperationMul.h"

@implementation Operationmul

-(double) getresult{
Double result = 0;
result = Numbera*numberb;
return result;
}

@end


Division Class (operator subclass):
Interface file:
Copy Code code as follows:

#import "Operation.h"

@interface operationdiv:operation
@end


Implementation file:
Copy Code code as follows:

#import "OperationDiv.h"

@implementation Operationdiv

-(double) getresult{
Double result = 0;
@try {
result = Numbera/numberb;
}
@catch (NSException *exception) {
NSLog (@ "divisor cannot be 0");
}
return result;
}

@end


Below is the factory class (depending on the strength of the operational class to achieve specific functions):
Interface file:
Copy Code code as follows:

#import <Foundation/Foundation.h>
#import "OperationAdd.h"
#import "OperationDiv.h"
#import "OperationSub.h"
#import "OperationMul.h"

@interface Operationfactory:nsobject
+ (operation*) createoperate: (char) operate;
@end


Implementation file:
Copy Code code as follows:

#import "OperationFactory.h"

+ (operation*) createoperate: (char) operate{
Operation *oper;
Switch (operate) {
Case ' + ':
oper = [[Operationadd alloc]init];
Break
Case '-':
oper = [[Operationsub alloc]init];
Break
Case ' * ':
oper = [[Operationmul alloc]init];
Break
Case '/':
oper = [[Operationdiv alloc]init];
Break
Default
oper = nil;
Break
}
return oper;
}


Specific call
Copy Code code as follows:

#import <Foundation/Foundation.h>
#import "OperationAdd.h"
#import "OperationDiv.h"
#import "OperationMul.h"
#import "OperationSub.h"
#import "OperationFactory.h"

int main (int argc,const char* argv[])
{
@autoreleasepool {
Operation *oper = [operationfactory createoperate: ' * '];
[Oper setnumbera:1];
[Oper Setnumberb:2];
Double result = 0;
result = [Oper GetResult];
NSLog (@ "result are%f", result);
}
return 0;
}


OK, here's a list of the basic code for the simple factory pattern. In fact, it is quite simple, right, only a layer of inheritance relationship, a dependency, in the factory class with the switch statement to determine what type to instantiate, then calculate, get the results.

Factory method Mode
There is a mention of the factory class mode in the simple factory model above. To help you recall the simple factory model, to mention the advantages of the simple factory model, the biggest advantage of the simple factory model is that the factory class contains the necessary logical judgment, dynamically instantiating the related classes according to the client's selection conditions, and to the client, to remove the dependence on the specific product. In fact, the factory method pattern is a further abstraction and generalization of the simple factory model. Because of the use of polymorphism, the factory method pattern maintains the advantages of a simple factory model and overcomes its drawbacks. But the disadvantage is that, with each addition of a product, you need to add a product factory class, adding additional development volume.

The following is a calculator for example, detailed description of the factory method model, or the same as the old, first show you the class structure diagram.

The above diagram shows you the relationships between the various classes. In fact, unlike the simple factory model, the abstract factory interface to the right of the class diagram is an abstract interface that is more than a simple factory pattern.

Let's go directly to the code below and say nothing more.

Note: All of the code in this article is compiled in an arc environment.

Operation class Interface

Copy Code code as follows:

#import <Foundation/Foundation.h>

@interface operation:nsobject{
Double Numbera;
Double Numberb;
}
@property double Numbera;
@property double Numberb;
-(double) getresult;
@end


Operation class Implementation
Copy Code code as follows:

#import "Operation.h"

@implementation Operation
@synthesize Numbera, Numberb;
-(double) getresult{
return-1.0;
}
@end


Operationadd class Interface
Copy Code code as follows:

#import "Operation.h"

@interface operationadd:operation
@end


Operationadd class implementation
Copy Code code as follows:

#import "OperationAdd.h"

@implementation Operationadd
-(double) getresult{
Double result = 0;
result = Numbera+numberb;
return result;
}
@end


Operationdiv class Interface
Copy Code code as follows:

#import "Operation.h"

@interface operationdiv:operation
@end


Operationdiv class implementation
Copy Code code as follows:

#import "OperationDiv.h"

@implementation Operationdiv
-(double) getresult{
Double result = 0;
@try {
result = Numbera/numberb;
}
@catch (NSException *exception) {
NSLog (@ "divisor cannot be 0");
}
return result;
}
@end


Operationmul class Interface
Copy Code code as follows:

#import "Operation.h"

@interface operationmul:operation
@end
Operationmul class implementation

#import "OperationMul.h"

@implementation Operationmul
-(double) getresult{
Double result = 0;
result = Numbera*numberb;
return result;
}
@end


Operationsub class Interface
Copy Code code as follows:

#import "Operation.h"

@interface operationsub:operation
@end


Operationsub class implementation
Copy Code code as follows:

#import "OperationSub.h"

@implementation Operationsub
-(double) getresult{
Double result = 0;
result = Numbera-numberb;
return result;
}
@end


Ifactory class Interface
Copy Code code as follows:

#import <Foundation/Foundation.h>

#import "Operation.h"
@interface Ifactory:nsobject
-(operation*) createoperation;
@end


Ifactory class implementation
Copy Code code as follows:

#import "IFactory.h"

@implementation Ifactory
-(operation*) createoperation{
return [[Operation alloc]init];
}
@end


Addfactory class Interface
Copy Code code as follows:

#import "IFactory.h"

@interface Addfactory:ifactory
@end


Addfactory class implementation
Copy Code code as follows:

#import "AddFactory.h"
#import "OperationAdd.h"

@implementation Addfactory
-(operation*) createoperation{
return [[Operationadd alloc]init];
}
@end


Subfactory class Interface
Copy Code code as follows:

#import "IFactory.h"

@interface Subfactory:ifactory
@end


Subfactory class implementation
Copy Code code as follows:

#import "SubFactory.h"
#import "OperationSub.h"

@implementation Subfactory
-(operation*) createoperation{
return [[Operationsub alloc]init];
}
@end


Mulfactory class Interface
Copy Code code as follows:

#import "IFactory.h"

@interface Mulfactory:ifactory
@end


Mulfactory class implementation
Copy Code code as follows:

#import "MulFactory.h"
#import "OperationMul.h"

@implementation Mulfactory
-(operation*) createoperation{
return [[Operationmul alloc]init];
}
@end


Divfactory class Interface
Copy Code code as follows:

#import "IFactory.h"

@interfaceDiv factory:ifactory
@end


Divfactory class implementation
Copy Code code as follows:

#import "DivFactory.h"
#import "OperationDiv.h"

@implementation Divfactory
-(operation*) createoperation{
return [[Operationdiv alloc]init];
}
@end


Main method call
Copy Code code as follows:

#import <Foundation/Foundation.h>
#import "OperationAdd.h"
#import "AddFactory.h"//addition factory, you can add other computing plants as needed

int main (int argc,const char* argv[])
{
@autoreleasepool {
Ifactory *operfactory = [[Addfactory alloc]init];
Operation *oper = [Operfactory createoperation];
[Oper setnumbera:1];
[Oper Setnumberb:2];
Double result = [Oper GetResult];
NSLog (@ "The result is%f", result);
}
return 0;
}


OK, above is the class code for Objective C of the factory method pattern.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.