js设计模式:模板方法模式

作用:

父类定义一个整体的模板框架,将具体的方法行为定义到子类中。

模板方法主要是封装行为中的固定部分,同时允许子类对方法进行扩展

示例:

        //moba游戏原型设计方案
        class MobaGame{
            loadAssets(){
                return{
                    heroList:this.heroList(),
                    equipmentList:this.equipmentList(),
                    maps:this.maps()
                }

            }
            heroList(){
                throw new Error('具体游戏具体定义英雄')
            }
            equipmentList(){
                throw new Error('具体游戏具体定义装备')
            }
            maps(){
                throw new Error('具体游戏具体定义地图')
            }
        }
        //根据moba游戏模式创建dota
        class Dota extends MobaGame{
            constructor(){
                super()
                this.from = '从魔兽争霸单机游戏里进入的,不用账号'
            }
            heroList(){
                return ['末日使者','斧王','圣骑士','敌法师']
            }
            equipmentList(){
                return ['圣剑','阿哈利姆神杖']
            }
            maps(){
                return [{name:'天灾vs近卫地图'}]
            }
        }
        //根据moba游戏模式创建lol
        class LOL extends MobaGame{
            constructor(code,password){
                super()
             this.code = code
             this.password = password
            }
            heroList(){
                return ['德玛西亚皇子','虚空女皇','刀锋之影','诡术妖姬']
            }
            equipmentList(){
                return ['日炎斗篷','最后的轻语']
            }
            maps(){
                return [{name:'召唤师峡谷'},{name:'扭曲丛林'}]
            }
        }
        
        //下载war3
        let wjt_dota = new Dota()
        //加载dota游戏资源
        const dotaAssets = wjt_dota.loadAssets()
        console.log(dotaAssets,'dota资源')
        //下载英雄联盟
        let wjt_lol = new LOL('123456','654321')
        //加载lol资源
        const lolAssets = wjt_lol.loadAssets()
        console.log(lolAssets,'lol资源')

相关推荐

  1. 设计模式-模板方法模式

    2024-02-23 15:34:03       43 阅读
  2. 设计模式模板方法模式

    2024-02-23 15:34:03       27 阅读
  3. 设计模式——模板方法模式

    2024-02-23 15:34:03       36 阅读
  4. 设计模式: 模板方法模式

    2024-02-23 15:34:03       24 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-02-23 15:34:03       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-02-23 15:34:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-02-23 15:34:03       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-02-23 15:34:03       20 阅读

热门阅读

  1. 爬虫项目(1)

    2024-02-23 15:34:03       28 阅读
  2. ffmpeg静态编译 —— 筑梦之路

    2024-02-23 15:34:03       37 阅读
  3. Ubuntu/WSL下生产密钥脚本

    2024-02-23 15:34:03       27 阅读
  4. ubuntu 查询流量使用

    2024-02-23 15:34:03       28 阅读
  5. 从零学算法238

    2024-02-23 15:34:03       27 阅读
  6. C语言实现基础数据结构——栈

    2024-02-23 15:34:03       32 阅读
  7. Jquery Ajax—我耀学IT

    2024-02-23 15:34:03       30 阅读
  8. LUA 调用c#关于c#报错时lua调用堆栈的监听

    2024-02-23 15:34:03       25 阅读