iOS design mode-decoration
Schematic diagram
Description
1. The cocoa framework itself implements the decorative mode (category way to achieve the decorative mode)
2. Decoration mode refers to the dynamic to add some additional responsibilities to an object, the decorative mode is more flexible than inheriting subclasses
. I only realized the simplest decorative mode, the adorner class is a specific class, non-abstract class
Source
Https://github.com/YouXianMing/DecoratorPattern
////GamePlay.h//Decoratorpattern////Created by youxianming on 15/8/1.//Copyright (c) 2015 youxianming. All rights reserved.//#import<Foundation/Foundation.h>@interfaceGameplay:nsobject/** * Number of game coins*/@property (nonatomic) nsinteger coin;/** * up and down Operation*/- (void) up;- (void) down;- (void) left;- (void) right;/** * Select and start the Operation*/- (void) Select;- (void) Start;/** * Button A + B operation*/- (void) Commanda;- (void) commandb;@end
////gameplay.m//Decoratorpattern////Created by youxianming on 15/8/1.//Copyright (c) 2015 youxianming. All rights reserved.//#import "GamePlay.h"@implementationGamePlay- (void) up {NSLog (@" up");}- (void) down {NSLog (@" Down");}- (void) left {NSLog (@" Left");}- (void) Right {NSLog (@" Right");}- (void) Select {NSLog (@"Select");}- (void) Start {NSLog (@"Start");}- (void) Commanda {NSLog (@"Commanda");}- (void) commandb {NSLog (@"commandb");}@end
////DecoratorGamePlay.h//Decoratorpattern////Created by youxianming on 15/8/1.//Copyright (c) 2015 youxianming. All rights reserved.//#import<Foundation/Foundation.h>#import "GamePlay.h"@interfaceDecoratorgameplay:nsobject@property (nonatomic) nsinteger coin;- (void) up;- (void) down;- (void) left;- (void) right;- (void) Select;- (void) Start;- (void) Commanda;- (void) commandb;#pragmaMark-Below is a newly added feature of the adornment object/** * Several lives left*/@property (nonatomic,ReadOnly) Nsinteger lives;/** * Cheating ( up and down around Abab)*/- (void) cheat;@end
////decoratorgameplay.m//Decoratorpattern////Created by youxianming on 15/8/1.//Copyright (c) 2015 youxianming. All rights reserved.//#import "DecoratorGamePlay.h"@interfaceDecoratorgameplay () @property (nonatomic, strong) GamePlay*GamePlay;@end@implementationDecoratorgameplay#pragmaMark-Initialize-(instancetype) init { self=[Super Init]; if(self) {//a Decorative object contains a reference to a real objectSelf.gameplay = [GamePlayNew]; } returnSelf ;}#pragmaMark-Lets the real object's reference execute the corresponding method-(void) up {[_gameplay up];}- (void) down {[_gameplay];}- (void) left {[_gameplay left];}- (voidRight {[_gameplay];}- (void) Select {[_gameplay select];}- (void) Start {[_gameplay start];}- (void) Commanda {[_gameplay Commanda];}- (void) commandb {[_gameplay commandb];}#pragmaMark-The newly added method of the adorner-(void) Cheat {[_gameplay up]; [_gameplay down]; [_gameplay up]; [_gameplay down]; [_gameplay left]; [_gameplay right]; [_gameplay left]; [_gameplay right]; [_gameplay Commanda]; [_gameplay COMMANDB]; [_gameplay Commanda]; [_gameplay commandb];}@synthesizelives =_lives;-(Nsinteger) lives {//Related processing logic return Ten;}@end
Analysis
The following is a detailed contrast chart of the decorative pattern implementation
iOS design mode-decoration