Common iOS design patterns-simple factory design patterns

Source: Internet
Author: User

Common iOS design patterns-simple factory design patterns
Detailed description of simple factory design mode

Simple factory design patterns basic concepts structure diagram classic sample code advantages and disadvantages usage conditions

Basic Concepts

"A dedicated class is defined to create instances of other classes. The created instance usually has a common parent class ."
In the world, a factory class dynamically decides which product class instance to create based on input parameters.

Structure chart

ConcreteProduct1 and ConcreteProduct2 have a common parent class IProject. The simple factory class is SimpleFactory, which decides whether to produce ConcreteProduct1 or ConcreteProduct2 products based on different input parameters.

Classic example

The most typical example is an Operation class. The addition, subtraction, multiplication, and Division operators all inherit from the parent class Operation. They have two attributes and an Operation method, these addition, subtraction, multiplication, and Division objects are not directly created in ViewController, but created by the simple factory OperationFactory based on the input operator.

Sample Code

(1) create a protocol OprationProtocol, which is followed by the parent class Operation.

/** Operation method protocol interface */@ protocol OperationProtocol
  
   
-(Double) getResult; @ end
  

(2) define the parent class Operation for addition, subtraction, multiplication, division operations

# Import OperationProtocol. h/** Operation method parent class */@ interface Operation: NSObject
  
   
@ Property double firstNum; // The first operand @ property double secondNum; // The second operand @ end
  

(3) add, subtract, multiply, divide, and implement classes. Here we will use "add" as an example.

// OperationAdd. h file # import Operation. h/** addition implementation class */@ interface OperationAdd: Operation @ end // OperationAdd. m file # import "OperationAdd. h "@ implementation OperationAdd-(double) getResult {double result = 0; result = self. firstNum + self. secondNum; // "+" is used when OperationAdd, "+-*/" corresponds to "addition, subtraction, multiplication, division" return result;} @ end

(4) Simple Factory Code

// OpeartionFactory. h file # import Operation. h # import OperationAdd. h # import OperationSub. h # import OperationMultiply. h # import OperationDivide. h/** Operation factory class */@ interface OperationFactory: NSObject // get Operation object + (Operation *) createOperate :( NSString *) operateStr; @ end // OpeartionFactory. m file # import "OperationFactory. h "@ implementation OperationFactory + (Operation *) createOperate :( NSString *) operateStr {Operation * operator = nil; // create different Operation objects based on different operators, "+-*/" corresponds to "addition, subtraction, multiplication, division" if ([operateStr isEqualToString: @ "+"]) {operator = [[OperationAdd alloc] init];} else if ([operateStr isEqualToString: @ "-"]) {response = [[OperationSub alloc] init];} else if ([operateStr isEqualToString: @ "*"]) {operator = [[OperationMultiply alloc] init];} else if ([operateStr is1_tostring: @ "/"]) {operator = [[OperationDivide alloc] init];} return response;} @ end

(5) client code, using OperationFactory in ViewController

 NSString* strFirstNum = self.firstNumTextField.text;    NSString* strSecondNum = self.secondNumTextField.text;    Operation* oper;    oper = [OperationFactory createOperate:self.operationTextField.text];    oper.firstNum = [strFirstNum doubleValue];    oper.secondNum = [strSecondNum doubleValue];    self.resultTextField.text = [NSString stringWithFormat:@%f,[oper getResult]];

Through the reconstruction of the simple factory model, we are idle in the code structure with a low Coupling Degree, enabling external expansion and opening, and disabling modification. If you add any operation method, you only need to inherit the operation method parent class, create an operation subclass, and add an else if judgment in the simple factory class.

Advantages

The simple factory method removes the coupling of the special classes of the application from the code. The caller does not have to care about the specific implementation. This removes the responsibilities of the common builder and separates the responsibilities.

Disadvantages

Engineering has concentrated the logic of all products. Once it fails to work normally, the entire system will be affected. When there are many categories, all creation work will be implemented in the factory method, and subsequent expansion will be more complicated.

Usage conditions

(1) The factory class is responsible for creating fewer objects;

(2) The client only knows the parameters passed into the factory class and does not have to worry about how to create the object logic.

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.