zoukankan      html  css  js  c++  java
  • iOS设计模式

    iOS设计模式 - 模板

    原理图

    说明

    定义一个操作中的算法的骨架,而将步骤延迟到子类中。模板方法使得子类可以不改变一个算法的结构即可重定义算法的某些特定步骤。

    源码

    https://github.com/YouXianMing/iOS-Design-Patterns

    //
    //  GameProtocol.h
    //  TemplatePattern
    //
    //  Created by YouXianMing on 15/10/27.
    //  Copyright © 2015年 ZiPeiYi. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    
    @protocol GameProtocol <NSObject>
    
    @required
    
    /**
     *  设置玩家个数
     *
     *  @param count 数目
     */
    - (void)setPlayerCount:(int)count;
    
    /**
     *  返回玩家数目
     *
     *  @return 玩家数目
     */
    - (int)playerCount;
    
    /**
     *  初始化游戏
     */
    - (void)initializeGame;
    
    /**
     *  开始游戏
     */
    - (void)makePlay;
    
    /**
     *  结束游戏
     */
    - (void)endOfGame;
    
    @end
    //
    //  Monopoly.h
    //  TemplatePattern
    //
    //  Created by YouXianMing on 15/10/27.
    //  Copyright © 2015年 ZiPeiYi. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    #import "GameProtocol.h"
    
    @interface Monopoly : NSObject <GameProtocol>
    
    @end
    //
    //  Monopoly.m
    //  TemplatePattern
    //
    //  Created by YouXianMing on 15/10/27.
    //  Copyright © 2015年 ZiPeiYi. All rights reserved.
    //
    
    #import "Monopoly.h"
    
    @interface Monopoly ()
    
    @property (nonatomic, assign) int gamePlayerCount;
    
    @end
    
    @implementation Monopoly
    
    - (void)setPlayerCount:(int)count {
    
        self.gamePlayerCount = count;
    }
    
    - (int)playerCount {
        
        return self.gamePlayerCount;
    }
    
    - (void)initializeGame {
    
        NSLog(@"Monopoly initialize");
    }
    
    - (void)makePlay {
    
        NSLog(@"Monopoly makePlay");
    }
    
    - (void)endOfGame {
    
        NSLog(@"Monopoly endOfGame");
    }
    
    @end
    //
    //  Chess.h
    //  TemplatePattern
    //
    //  Created by YouXianMing on 15/10/27.
    //  Copyright © 2015年 ZiPeiYi. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    #import "GameProtocol.h"
    
    @interface Chess : NSObject <GameProtocol>
    
    @end
    //
    //  Chess.m
    //  TemplatePattern
    //
    //  Created by YouXianMing on 15/10/27.
    //  Copyright © 2015年 ZiPeiYi. All rights reserved.
    //
    
    #import "Chess.h"
    
    @interface Chess ()
    
    @property (nonatomic, assign) int gamePlayerCount;
    
    @end
    
    @implementation Chess
    
    - (void)setPlayerCount:(int)count {
        
        self.gamePlayerCount = count;
    }
    
    - (int)playerCount {
        
        return self.gamePlayerCount;
    }
    
    - (void)initializeGame {
        
        NSLog(@"Chess initialize");
    }
    
    - (void)makePlay {
        
        NSLog(@"Chess makePlay");
    }
    
    - (void)endOfGame {
        
        NSLog(@"Chess endOfGame");
    }
    
    @end

    细节

  • 相关阅读:
    JavaScript事件冒泡简介及应用
    个人作业——软件工程实践总结&个人技术博客
    个人技术总结
    个人作业——软件测评
    寒假作业(2/2)
    寒假作业(1/2)
    gitlab-ci.yml可用关键字描述
    gitlab-Runner配置参数详解
    gitlab-ci部署实现持续集成(centos7)
    linux时间同步操作命令
  • 原文地址:https://www.cnblogs.com/YouXianMing/p/4914015.html
Copyright © 2011-2022 走看看