iOS common design Patterns-Simple factory design Patterns

Source: Internet
Author: User

Simple Factory design pattern

    • Simple Factory design pattern
      • Basic concepts
      • Structure diagram
      • A classic example
      • Sample code
      • Advantages
      • Disadvantages
      • Conditions of Use

Basic concepts

"specifically defines a class to be responsible for creating instances of other classes, and the instances that are created typically have a common parent class. ”
The world is made up of a factory class that dynamically decides which product class to create, based on the parameters passed in.

Structure diagram

ConcreteProduct1 and ConcreteProduct2 two products have a common parent class iproject, the Simple factory class is simplefactory, Responsible for determining the production ConcreteProduct1 or ConcreteProduct2 products according to the different parameters passed in.

A classic example

The most classic example is the operations class, plus, minus, multiply, except four operators inherit from the parent class operation, there are two properties and an action method, these subtraction objects are not created directly in Viewcontroller, but based on the input operator, Created by simple factory operationfactory.

Sample code

(1) Create a protocol Oprationprotocol, operation by the parent class to comply with the protocol

/* *  操作方法协议接口 */@protocol OperationProtocol <NSObject>-(double)getResult;@end

(2) Define the parent class of the subtraction operation operation

#import OperationProtocol.h/* *  操作方法父类 */@interface Operation : NSObject<operationprotocol>@propertydouble firstNum;//第一个操作数@propertydouble secondNum;//第二个操作数 @end

(3) Subtraction implementation class, here with "plus" example

//operationadd.h File#import Operation.h/ * * Addition implementation class */ @interface operationadd : operation @end//operationadd.m File#import "OperationAdd.h"  @implementation operationadd -(Double) getresult{Doubleresult =0; result = Self. Firstnum+ Self. Secondnum;//"+" is used when Operationadd, "+-*/" corresponds to "subtraction" respectively    returnResult;}@end

(4) Code for simple factory class

//opeartionfactory.h File#import Operation.h#import OperationAdd.h#import OperationSub.h#import OperationMultiply.h#import OperationDivide.h/ * * Operate factory class */ @interface operationfactory : nsobject //Get Action Object+ (operation*) Createoperate: (NSString*) Operatestr;@end//opeartionfactory.m File#import "OperationFactory.h"  @implementation operationfactory + (operation*) Createoperate: (NSString*) operatestr{operation* oper =Nil;//Create different operands according to different operators, "+-*/" corresponds to "subtraction" respectively    if([Operatestr isequaltostring:@"+"] {oper = [[Operationadd alloc] init]; }Else if([Operatestr isequaltostring:@"-"] {oper = [[Operationsub alloc] init]; }Else if([Operatestr isequaltostring:@"*"] {oper = [[Operationmultiply alloc] init]; }Else if([Operatestr isequaltostring:@"/"] {oper = [[Operationdivide alloc] init]; }returnOper;}@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 refactoring of the simple Factory mode, we are idle the code structure of the low coupling degree, so that the external expansion open, the modification is closed. If you add any additional action methods, simply inherit the action method parent class, create a new action subclass, and add an else if to the simple factory class.

Advantages

The simple factory method eliminates the coupling of the application-specific classes from the code, and the caller does not have to care about the implementation, cancels the responsibility of the incumbent, and realizes the division of responsibility.

Disadvantages

The Engineering class concentrates all product logic, once does not work properly, the entire system is affected, when the classification is more, all creation work is implemented in the factory method, the late expansion is more miscellaneous

Conditions of Use

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

(2) The client knows only the parameters of the incoming factory class, and does not care about how to create the object's logic.

iOS common design Patterns-Simple factory design Patterns

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.