iOS design mode-decoration

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.