Objective-C decoration mode-Brief introduction and use, simple introduction to design mode
The decoration mode dynamically extends the functions of an object without changing the original class file and using inheritance. It creates a packaging object, that is, decoration, to package a real object.
For example, if a game host has a GamePad class and you want to add a cheating function (for example, 100 million messages), adding a cheating function directly to the GamePad class may affect the use of other sub-classes.
We want to consider the decorative mode thinking. First, we should create a decorator to implement all the functions of GamePad, and then add the cheating Method to the sub-class of the decorator class.
Code on
For example, the GamePad class is like this.
1 #import <Foundation/Foundation.h> 2 3 @interface GamePad : NSObject 4 5 - (void)up; 6 - (void)down; 7 - (void)left; 8 - (void)right; 9 - (void)buttonA;10 - (void)buttonB;11 12 @end
We create a modifier class to hold a GamePad instance and implement the same method interface.
GamePadDecorator. h
1 #import <Foundation/Foundation.h> 2 #import "GamePad.h" 3 4 @interface GamePadDecorator : NSObject 5 6 - (void)up; 7 - (void)down; 8 - (void)left; 9 - (void)right;10 - (void)buttonA;11 - (void)buttonB;12 13 @end
GamePadDecorator. m
1 #import "GamePadDecorator.h" 2 3 @interface GamePadDecorator () 4 5 @property (nonatomic, strong) GamePad *gamePad; 6 7 @end 8 9 @implementation GamePadDecorator10 11 - (instancetype)init {12 self = [super init];13 if (self) {14 self.gamePad = [[GamePad alloc] init];15 }16 return self;17 }18 19 - (void)up {20 [self.gamePad up];21 }22 23 - (void)down {24 [self.gamePad down];25 }26 27 - (void)left {28 [self.gamePad left];29 }30 31 - (void)right {32 [self.gamePad right];33 }34 35 - (void)buttonA {36 [self.gamePad buttonA];37 }38 39 - (void)buttonB {40 [self.gamePad buttonB];41 }42 43 @end
Now we add a subclass to implement the cheating method.
CheatGamePadDecorator. h
1 #import "GamePadDecorator.h"2 3 @interface CheatGamePadDecorator : GamePadDecorator4 5 - (void)cheat;6 7 @end
CheatGamePadDecorator. m
1 #import "CheatGamePadDecorator.h"2 3 @implementation CheatGamePadDecorator4 5 - (void)cheat {6 NSLog(@"cheat");7 }8 9 @end
In this way, you can directly use the CheatGamePadDecorator class in the Controller to implement all the functions of GamePad and implement the cheating method.
1 # import "ViewController. h "2 # import" CheatGamePadDecorator. h "3 4 @ interface ViewController () 5 6 @ end 7 8 @ implementation ViewController 9 10-(void) viewDidLoad {11 [super viewDidLoad]; 12 13 // create a consumer instance 14 cores * cheaterGamePad = [[CheatGamePadDecorator alloc] init]; 15 16 // implement the GamePad function 17 [cheaterGamePad up]; 18 [cheaterGamePad down]; 19 20 // cheating Method 21 [cheaterGamePad cheat]; 22} 23 24 25 26 @ end
This completes the code construction of a decoration mode idea.
The Category in cocoa touch is also an implementation of the decoration mode.
We use Category to implement the above-mentioned GamePad cheating function.
We create a Cheat Category
GamePad + Cheat. h
1 #import "GamePad.h"2 3 @interface GamePad (Cheat)4 5 - (void)cheat;6 7 @end
GamePad + Cheat. m
1 #import "GamePad+Cheat.h"2 3 @implementation GamePad (Cheat)4 5 - (void)cheat {6 NSLog(@"cheat");7 }8 9 @end
In this way, we can directly implement the above functions through Category in the Controller.
1 # import "ViewController. h "2 # import" GamePad + Cheat. h "3 4 @ interface ViewController () 5 6 @ end 7 8 @ implementation ViewController 9 10-(void) viewDidLoad {11 [super viewDidLoad]; 12 13 // create a GamePad instance 14 GamePad * gamePad = [[GamePad alloc] init]; 15 16 // implement the original GamePad Method 17 [gamePad up]; 18 [gamePad down]; 19 20 // cheating Method 21 [gamePad cheat]; 22 23}
Easier to use Category
However, pay attention to the details when using Category. Do not overwrite the base class method in the Category class.
If the-(void) up method is rewritten in GamePad + Cheat. h, the up method in the project is overloaded.
Even if we do not reference GamePad + Cheat. h anywhere, as long as this file is in the project, the GamePad method will be overloaded.