Decorating mode is the ability to dynamically extend an object without having to change the original class file and use inheritance. It is by creating a wrapper object, that is, decorating to wrap the real object.
For example, the game console has a GamePad class, now to add a cheat function (such as 100 life), if directly in the GamePad class to add may affect the use of other subclasses
We consider the decorative mode thinking, first set up an adorner to implement all the functions of gamepad, and then add the cheat method in the subclass of the adorner class.
On the Code
Like the GamePad class.
1 #import<Foundation/Foundation.h>2 3 @interfaceGamepad:nsobject4 5- (void) up;6- (void) down;7- (void) left;8- (void) right;9- (void) Buttona;Ten- (void) buttonb; One A @end
Let's create an adorner class that holds a gamepad instance and implements the same method interface
GamePadDecorator.h
1 #import<Foundation/Foundation.h>2 #import "GamePad.h"3 4 @interfaceGamepaddecorator:nsobject5 6- (void) up;7- (void) down;8- (void) left;9- (void) right;Ten- (void) Buttona; One- (void) buttonb; A - @end
Gamepaddecorator.m
1 #import "GamePadDecorator.h"2 3 @interfaceGamepaddecorator ()4 5@property (nonatomic, strong) GamePad *GamePad;6 7 @end8 9 @implementationGamepaddecoratorTen One-(instancetype) init { ASelf =[Super init]; - if(self) { -Self.gamepad =[[GamePad alloc] init]; the } - returnSelf ; - } - +- (void) up { - [Self.gamepad up]; + } A at- (void) down { - [Self.gamepad down]; - } - -- (void) left { - [Self.gamepad left]; in } - to- (void) Right { + [Self.gamepad right]; - } the *- (void) Buttona { $ [Self.gamepad Buttona];Panax Notoginseng } - the- (void) buttonb { + [Self.gamepad buttonb]; A } the + @end
Now let's add a subclass to implement the Cheat method
CheatGamePadDecorator.h
1 #import " GamePadDecorator.h " 2 3 @interface Cheatgamepaddecorator:gamepaddecorator 4 5 -(void) cheat; 6 7 @end
Cheatgamepaddecorator.m
1 #import " CheatGamePadDecorator.h " 2 3 @implementation Cheatgamepaddecorator 4 5 -(void) cheat {6 NSLog (@ "cheat") ); 7 }89@end
This allows us to use the Cheatgamepaddecorator class directly in the controller to implement all the functions of the gamepad and to implement the Cheat method in an extra way.
1 #import "ViewController.h"2 #import "CheatGamePadDecorator.h"3 4 @interfaceViewcontroller ()5 6 @end7 8 @implementationViewcontroller9 Ten- (void) Viewdidload { One [Super Viewdidload]; A - //Creating an Cheatgamepaddecorator instance -Cheatgamepaddecorator *cheatergamepad =[[Cheatgamepaddecorator alloc] init]; the - //realizing the GamePad function - [Cheatergamepad up]; - [Cheatergamepad down]; + - //implementing the Cheat method + [Cheatergamepad cheat]; A } at - - - @end
This completes the code building of a decorating mode idea
Let's talk about the category that comes with cocoa touch, and it's also an implementation of the decorative pattern.
We use category to implement the above GamePad add cheat 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 }89@end
This allows us to implement the above function directly in the controller by category.
1 #import "ViewController.h"2 #import "gamepad+cheat.h"3 4 @interfaceViewcontroller ()5 6 @end7 8 @implementationViewcontroller9 Ten- (void) Viewdidload { One [Super Viewdidload]; A - //Creating an GamePad instance -GamePad *gamepad =[[GamePad alloc] init]; the - //implement the original method of GamePad - [gamepad up]; - [GamePad down]; + - //implementing the Cheat method + [GamePad cheat]; A at}
More simple to use category
But there is a detail to be aware of when using category, try not to rewrite the base class method in category class
If we re-(void) The Up method in Gamepad+cheat.h, the up method in the entire project is overloaded
Even if we don't cite gamepad+cheat.h anywhere, as long as this file is in the project, the GamePad method is overloaded.
Objective-c Decoration Mode--Simple introduction and use