Simple Factory mode is not used much in iOS development. However, I think this is a good example of OC reflex mechanism,//So this article will take the calculator as an example, explain the simple Factory mode and OC reflection mechanism. The essence of the Simple factory model is that a factory class dynamically determines which product class (// These product classes inherit from a parent class or interface) should be created, based on the parameters passed in. Roles and responsibilities included in the model: plant roles, pumping// Elephant product roles, specific product roles "// --Baidu Encyclopedia Simple Factory mode// above this sentence may not be very good understanding, I found an example on the Internet, Perhaps the example itself does not fully explain the merits of this//design pattern, but it introduces roles and responsibilities very clearly:// "a boy wants to invite a girl to dinner, but the boy can't cook." So simply go to McDonald's, let the girls themselves// point like to eat things on it. "// in this case, McDonald's is the factory role, McDonald's products are abstract product roles, fries, hamburger is// specific product role. In this mode of thinking, we can draw a UML class diagram of the calculator:
Simple Factory mode class diagram
according to the class diagram we take the framework and encode:/*-----------------------------------------------------------------------*/
/** * Calculator Header file: Calculate.h */#import <Foundation/Foundation.h> #import "Operation.h" @interface calculate:nsobject A computed class method + (float) Calculate: (float) number1 number2: (float) number2 operators: (NSString *) operators; @end/** * Calculator Method Implementation: CALCULATE.M */#import "Calculate.h" @implementation calculate+ (float) Calculate: (float) number1 number2: ( FLOAT) number2 operators: (NSString *) operators{//using OC reflection to get a class with the same name as the string (which will be explained in detail later in this article) Class class = Nsclassfromstring (o Perators); Instantiate this class and pass in the two operands of the calculator operation *operation = [[Class alloc] Initwithnumebr1:number1 Number2:number2]; Call the Operation method and return the result return [operation operate];} @end/*-----------------------------------------------------------------------*//** * Abstract Operation class Operation.h */#import <Foundation/Foundation.h> @interface Operation:nsobject@property (nonatomic) float number1; @property ( nonatomic) float number2;-(float) operate; Operation Method: All specific operation classes will override this method to implement the subclass's own logic-(Operation *) INITWITHNUMEBR1: (float) number1 number2: (float) numBer2, @end/* Abstract operation class method implementation OPERATION.M (Implement Initwithnumebr1:number2: Method) *//*------------------------------------------- ----------------------------*//** * Addition Operation class Addition.h */#import "Operation.h"/* inherit abstract operation class Operation */@interface addition: The operation@end/* addition operation class method implements the ADDITION.M (overrides the operate method in the Operation class, implements the addition logic) *//*-------------------------------------- ---------------------------------*//** * Client main */#import <Foundation/Foundation.h> #import "Calculate.h" int Main (int argc, const char * argv[]) {@autoreleasepool {//operand 1, operand 2, and operator input into calculator//operator = Addition Class class name NS Log (@ "%.2f", [Calculate calculate:1 number2:2 operators:@ "addition"]); } return 0;} /* ----------------------------------------------------------------------- */
with the above code, we have successfully implemented a simple factory model calculator. The following is an explanation of the following OC's reflection mechanism:// "For any class, you can know all the properties and methods of this class, for any one object, can call// its any method and property; This dynamically obtained information and the function of dynamically invoking the method of the object is called the Java language the reflection mechanism of speech. "// --Baidu Encyclopedia Java Reflection//The above sentence is a description of Java reflection, we can also think of it as an introduction to OC reflex. For example, the above calculator program, we use the Nsclassfromstring method to get the class using a string. For// How to use reflection, get the methods in the class, call the object's methods, get the object belongs to a class, and so on, in my previous//article : "iOS Development Series" NSObject Method Description:// Reflection is the advantage of teamwork, If the class that the other party is responsible for is not fully implemented, then you can use the reflection if you introduce// wrong.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
The iOS Development series understands the OC reflection mechanism with a simple factory model