iOS Design Patterns Decorator Pattern

First, what is the decorative pattern

  • Schema Definition
    Decorator contains all the interfaces and references are decorators, the method is fully referenced to call their own way, to add new features decorator subclass.
    Comment:
    Category Do not rewrite method was decorated objects, or change the behavior of the objects of decoration, does not meet the decorator pattern, only the applicable special scene. Classification is mainly used for the methods and properties decorator class expansion
  • Demand scenario
    Static library extensions. Does not change (the original class), no inheritance, dynamic extensions.
    • I do not know the original class implementation details, provide only the interface, dynamic extensions.
    • I do not want to have more subclasses, do not want to add functionality by way of inheritance.
    • Dynamic expansion of the object function.
    • Must hold a reference to the object contains an instance of the class being decorated.

Second, the decorative pattern configuration diagram

  Method to realize:

  1. Decorator defined, and all the methods are defined in the .h storm drain to .h decorator class (method of operation may be customized to add) and a reference to the class decorator.
  2. With the decorator object calls its method, it has output the results we want in the decorator class.
  3. If you want to expand the Decorator, classified by the method, which can not be destroyed Decorator native logic.  

Third, the sample code

  • Target (the Decorator)
    • Target.h
      the Target @interface: the NSObject
       / * * 
       * number of coins 
       * / 
      @Property (nonatomic, ASSIGN) NSInteger COIN; 
      
      / * * 
       * vertical and horizontal operation 
       * / 
      - ( void ) up;
      
       - ( void ) Down;
      
       - ( void ) left;
      
       - ( void ) right;
       / * * 
       * operation of selecting the start 
       * / 
      - ( void ) sELECT ;
       - ( void ) start;
       / * * 
       * the operation buttons a + B of 
       * / 
      - ( void ) Commanda;
       - ( void ) commandB; 
      @end
    • Target.m
      @implementation Target
      
      - (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  
  • Decorator (Decorator)
    • Decorator.h
      #import <Foundation / Foundation.h> 
      
      NS_ASSUME_NONNULL_BEGIN 
      
      @interface the Decorator: the NSObject 
      
      #pragma Mark - the method Decorator 
      
      @Property (nonatomic) NSInteger COIN;
      
       - ( void ) up;
       - ( void ) Down;
       - ( void ) left ;
       - ( void ) right;
       - ( void ) SELECT ;
       - ( void ) Start;
       - ( void ) Commanda;
       - ( void ) commandB; 
      
      #pragma Mark - the following is a decorative features newly added objects / * * 
       * remaining few command * / 
      @Property (nonatomic,
      
      
       Readonly ) NSInteger Lives; 
      
      / * * 
       * cheat (up and down vertically about ABAB) 
       * / 
      - ( void ) Cheat; 
      
      @end
    • Decorator.m
      #import " Decorator.h " 
      #import " Target.h " 
      @interface Decorator () 
      
      the @Property (nonatomic, strong) Target target; // hold the decorator class object 
      
      @end 
      
      @implementation Decorator 
      
      #pragma Mark - Initialization 
      
      - ( instanceType) the init { 
          
          IF (Self = [Super the init]) { 
              
              // decorative object contains a reference to the real object 
              self.target = [the target new new ]; 
          } 
          
          return Self; 
      } 
      
      
      #pragma Mark - let the real object corresponding to the execution reference method 
      
      - ( void ) {up 
          
          [_target up]; 
      }
      
       - (void)down {
          
          [_target down];
      }
      
      - (void)left {
          
          [_target left];
      }
      
      - (void)right {
          [_target right];
      }
      
      - (void)select {
          [_target select];
      }
      
      - (void)start {
          
          [_target start];
      }
      
      - (void)commandA {
          
          [_target commandA];
      }
      
      - (void)commandB {
          
          [_target commandB];
      }
      
      #pragma mark - 装饰器新添加的方法
      - (void)cheat {
          [_target up];
          [_target down];
          [_target up];
          [_target down];
          [_target left];
          [_target right];
          [_target left];
          [_target right];
          [_target commandA];
          [_target commandB];
          [_target commandA];
          [_target commandB];
      }
      
      @synthesize lives = _lives;
      - (NSInteger)lives {
          // 相关处理逻辑
          return 10;
      }
      
      @end

Fourth, the advantages and disadvantages of decorative patterns

  • Advantage
    in the case of certain classes without changing the business logic, and functions to expand the existing method

Five, demo

  Decorator Pattern

Guess you like

Origin www.cnblogs.com/lxlx1798/p/11483200.html