我学会了,访问者模式

简介: 访问者模式属于行为型模式,这个类型的设计模式总结出了 类、对象之间的经典交互方式,将类、对象的行为和使用解耦了,花式的去使用对象的行为来完成特定场景下的功能。

前言

访问者模式属于行为型模式,这个类型的设计模式总结出了 类、对象之间的经典交互方式,将类、对象的行为和使用解耦了,花式的去使用对象的行为来完成特定场景下的功能。

访问者模式

使用场景:用于将数据的操作和数据的结构解耦,数据的结构是稳定,但是数据的操作多变,这时候可以使用访问者模式。

理解:这是一种类、对象之间的经典交互方式,将类、对象的行为和使用解耦了。在不改变原有数据结构的情况下,定义对这些数据结构的新操作。访问者非常的强大,可以在不破坏原有的类、对象的结构的情况下给这个类、对象扩展功能。不过它向外暴露了内部的细节,尽管如此,它也拥有了很优秀的扩展。

namespace action_mode_07 {

    interface ITeacherVisitor {
        visit(student: IStudent): void;
    }

    interface IStudent {
        type: string
        accep(techer: ITeacherVisitor): void;
    }

    // 访问者:老师A
    class TecherA implements ITeacherVisitor {

        name: string

        constructor (name: string) {
            this.name = name
        }

        visit(student: IStudent): void {
            if (student.type === '好学生') {
                console.log(this.name + '很开心,和好学生的父母谈笑风生。')
            } else if (student.type === '坏学生') {
                console.log(this.name + '很尴尬,安慰坏学生的父母要多陪陪孩子。')
            } else {
                console.log(this.name + '正常的和学生父母进行交流。')
            }
        }
    }

    // 好学生
    class GoodStudent implements IStudent {

        name: string
        type: string
        
        constructor (name: string) {
            this.name = name
            this.type = '好学生'
        }

        accep(techer: ITeacherVisitor): void {
            techer.visit(this)
        }
    }

    // 坏学生
    class BadStudent implements IStudent {

        name: string
        type: string

        constructor (name: string) {

            this.name = name
            this.type = '坏学生'
        }

        accep(techer: ITeacherVisitor) {
            techer.visit(this)
        }
    }

    // 普通学生
    class NormalStudent implements IStudent {

        name: string
        type: string

        constructor (name: string) {

            this.name = name
            this.type = '普通学生'
        }

        accep(techer: ITeacherVisitor) {
            techer.visit(this)
        }

    }

    // 访问者A
    const techerA = new TecherA('女老师')

    const goodStudent = new GoodStudent('好学生-夏雪')
    const badStudent = new BadStudent('坏学生-刘星')
    const normalStudent = new NormalStudent('普通学生-夏雨')

    // 未改变数据结构,直接通过访问者扩展了行为
    goodStudent.accep(techerA)
    badStudent.accep(techerA)
    normalStudent.accep(techerA)

    // 再来一个
    class TecherB implements ITeacherVisitor {
        name: string

        constructor (name: string) {
            this.name = name
        }

        visit(student: IStudent): void {
            if (student.type === '好学生') {
                console.log(this.name + '很开心,和好学生的父母谈笑风生。')
            } else if (student.type === '坏学生') {
                console.log(this.name + '很开心,说坏学生很有天分,在数学方面很有前途。')
            } else {
                console.log(this.name + '很开心的和学生父母进行交流。')
            }
        }
    }

    // 访问者B
    const techerB = new TecherB('金牌老师')
    goodStudent.accep(techerB)
    badStudent.accep(techerB)
    normalStudent.accep(techerB)

}
目录
相关文章
|
6天前
|
人工智能 JSON 供应链
畅用7个月无影 JVS Claw |手把手教你把JVS改造成「科研与产业地理情报可视化大师」
LucianaiB分享零成本畅用JVS Claw教程(学生认证享7个月使用权),并开源GeoMind项目——将JVS改造为科研与产业地理情报可视化AI助手,支持飞书文档解析、地理编码与腾讯地图可视化,助力产业关系图谱构建。
23389 5
畅用7个月无影 JVS Claw |手把手教你把JVS改造成「科研与产业地理情报可视化大师」
|
15天前
|
缓存 人工智能 自然语言处理
我对比了8个Claude API中转站,踩了不少坑,总结给你
本文是个人开发者耗时1周实测的8大Claude中转平台横向评测,聚焦Claude Code真实体验:以加权均价(¥/M token)、内部汇率、缓存支持、模型真实性及稳定性为核心指标。
5472 25
|
11天前
|
人工智能 JSON BI
DeepSeek V4 来了!超越 Claude Sonnet 4.5,赶紧对接 Claude Code 体验一把
JeecgBoot AI专题研究 把 Claude Code 接入 DeepSeek V4Pro 的真实体验与避坑记录 本文记录我将 Claude Code 对接 DeepSeek 最新模型(V4Pro)后的真实体验,测试了 Skills 自动化查询和积木报表 AI 建表两个场景——有惊喜,也踩
3969 13
|
10天前
|
人工智能 缓存 BI
Claude Code + DeepSeek V4-Pro 真实评测:除了贵,没别的毛病
JeecgBoot AI专题研究 把 Claude Code 接入 DeepSeek V4Pro,跑完 Skills —— OA 审批、大屏、报表、部署 5 大实战场景后的真实体验 ![](https://oscimg.oschina.net/oscnet/up608d34aeb6bafc47f
3238 11
Claude Code + DeepSeek V4-Pro 真实评测:除了贵,没别的毛病
|
27天前
|
人工智能 自然语言处理 安全
Claude Code 全攻略:命令大全 + 实战工作流(建议收藏)
本文介绍了Claude Code终端AI助手的使用指南,主要内容包括:1)常用命令如版本查看、项目启动和更新;2)三种工作模式切换及界面说明;3)核心功能指令速查表,包含初始化、压缩对话、清除历史等操作;4)详细解析了/init、/help、/clear、/compact、/memory等关键命令的使用场景和语法。文章通过丰富的界面截图和场景示例,帮助开发者快速掌握如何通过命令行和交互界面高效使用Claude Code进行项目开发,特别强调了CLAUDE.md文件作为项目知识库的核心作用。
21362 64
Claude Code 全攻略:命令大全 + 实战工作流(建议收藏)

热门文章

最新文章