IOS design mode: Workshop Mode
IOS design mode: Workshop Mode
1 What is the factory model:
Define an interface for creating objects in the base class, and let the subclass decide the class to be actually listed. The factory method delays the real column of a class to the subclass. The problem solved by the factory method is
Object creation time. It provides an extension policy, which is in line with the open and closed principle.
2 factory model design:
Abstract The factory and product into a base class, define a unified interface in the base class, and then create a specific product in the specific factory.
3 The structure of the factory method pattern is shown below (from the Object-C design pattern book)
4. Why is this a security method for creating objects (from Object-C design patterns)
Compared to directly creating a specific object, using the factory method to create an object is considered a best practice. The factory method mode allows the customer program to be created by the factory method.
The object has a group of common behaviors. Therefore, you do not need to modify the client code to inject a new product into the class hierarchy, because the interfaces of any returned object are
The original interfaces used by the client are the same.
The following shows the factory mode using a program :/********************/
BreedOfDogProtocol. h
// Method for defining the dog breed
@ Protocol BreedOfDogProtocol
-(NSString *) getBreed; // obtain the common method of the dog breed
@ End
/********************/
BreedOfDog. h
@ Interface BreedOfDog: NSObject
@ Property NSString * _ breed;
@ End
BreedOfDog. m
@ Implementation BreedOfDog
-(NSString *) getBreed {
Return 0;
}
@ End
/********************/
BorderCollie. h
// Border sheepdog
@ Interface BorderCollie: BreedOfDog
@ End
BorderCollie. m
@ Implementation BorderCollie
-(NSString *) getBreed {
Return @ BorderCollie;
}
@ End
/********************/
JiWawa. h
// GIWA
@ Interface JiWawa: BreedOfDog
@ End
JiWawa. m
@ Implementation JiWawa
-(NSString *) getBreed {
Return @ jiWawa;
}
@ End
/********************/
Define the factory method BreedOfDogFactory. h
@ Interface BreedOfDogFactory: NSObject
-(BreedOfDog *) getDifferentBreedOfDog;
@ End
BreedOfDogFactory. m
@ Implementation BreedOfDogFactory
-(BreedOfDog *) getDifferentBreedOfDog {
Return nil;
}
@ End
/********************/
BorderCollieFactory. h
@ Interface BorderCollieFactory: BreedOfDogFactory
@ End
BorderCollieFactory. m
@ Implementation BorderCollieFactory
-(BreedOfDog *) getDifferentBreedOfDog {
Return [[BorderColliealloc] init];
}
@ End
/********************/
JiWawaFactory. h
@ Interface JiWawaFactory: BreedOfDogFactory
@ End
JiWawaFactory. m
@ Implementation JiWawaFactory
-(BreedOfDog *) getDifferentBreedOfDog {
Return [[JiWawaalloc] init];
}
@ End
/********************/
Main. m
Int main (int argc, const char * argv []) {
BreedOfDogFactory * breedFactory = [[BorderCollieFactoryalloc] init];
BorderCollie * borderCollie = [breedFactory getDifferentBreedOfDog];
NSLog (% @, [borderColliegetBreed]);
Return 0;
}
5. You may think of the NSNumber class when using the factory model. His design philosophy is similar to that of the factory model.
NSNumber subclass Diagram
To create an NSNumber object, follow these steps:
- + NumberWithBool:
- + NumberWithChar:
- + NumberWithDouble:
- + NumberWithFloat:
- + NumberWithInt:
- + NumberWithInteger:
- + NumberWithLong:
- + NumberWithLongLong:
- + NumberWithShort:
- + NumberWithUnsignedChar:
- + NumberWithUnsignedInt:
- + NumberWithUnsignedInteger:
- + NumberWithUnsignedLong:
- + NumberWithUnsignedLongLong:
- + NumberWithUnsignedShort:
6. Why is NSNumber used? When the basic class is complete?
For example:
NSInteger is a base class, but NSNumber is a class. If you want to store a value, NSInteger cannot be used directly, for example, in Array.
NSArray * array = [[NSArray alloc] init];
[Array addObject: 3]; // compilation errors may occur because NSArray stores a class, and 2 cannot directly store a value.
NSArray * array = [[array alloc] init];
[Array addObject: [NSNumber numberWithInt: 3]; // This will not cause compilation errors because NSNumber generates an object.
Cocoa provides the NSNumber packaging (ji is implemented as an object) Basic data type.