我学会了,原型模式

简介: 原型模式属于创建型模式,这个类型的设计模式是将 对象的创建和使用解耦了,花式的去创建对象。

前言

原型模式属于创建型模式,这个类型的设计模式是将 对象的创建和使用解耦了,花式的去创建对象。

原型模式

应用场景:克隆已有对象,分为浅克隆、深克隆。意在减少创建重复对象的成本,有时你要创建的对象可能与已创建对象仅有细微差别,这时候你可以直接克隆已存在的对象,然后再变更你克隆的对象的数据,最终快速达到你预期的结果。

理解:复杂对象的创建和使用解耦了,通过对象克隆可以很简单的获得一个一摸一样的对象,内部还是进行了对象创建的操作。克隆对象的操作分为浅克隆和深克隆,浅克隆操作比较简单,而深克隆相对来说复杂一些,通过以上的代码示例可以看出来。浅克隆只能克隆一些皮毛,而且有时你修改了浅克隆后的对象会影响之前被你克隆的对象,这样的副作用很不友好,所以一般都使用深克隆。当然实际开发中一个复杂的对象会比代码示例中复杂几倍、十几倍、几十倍等等,所以一般都会采取三种方式:一、通过js动态语言的特性以及递归循环判断类型的方式来进行深克隆。二、通过 JSON.parse(JSON.stringify(obj))的方式,这种方式会将对象序列化为json字符串然后再反序列化为js对象,不过这种方式只能克隆数据,遇到复杂的就会有副作用,比如无法克隆对象的函数。三、采用面向对象的方式针对性的去硬编码完成这个复杂对象的克隆操作,虽然很标准,但是用在动态语言上就显得不那么灵活了,比如下面的代码示例。

namespace creative_mode_04 {

    // 产品类
    class Student {

        name: string = ''
        age: string = ''
        height: string = ''
        // classNum: string = ''
        // levelNum: string = ''
        // teacher: string = ''
        // father: string = ''
        // mother: string = ''
        dog?: Dog 
        cat?: Cat
    }

    class Dog {
        dogName: string = ''
        dogIQ?: number
    }

    class Cat {
        catName: string = ''
        catIQ?: number
    }

    // 浅克隆、深克隆的接口
    interface ICloneStudentable {
        clone (student: Student): Student
        deepClone (student: Student): Student
    }

    // 克隆机器
    class CloneMachine implements ICloneStudentable {
        // 浅克隆
        clone(student: Student): Student {
            if (!student || !(student instanceof Student)) {
                throw new Error("param is null or not Student instance.")
            }

            const newStudent = new Student()
            newStudent.name = student.name
            newStudent.age = student.age
            newStudent.height = student.height
            newStudent.dog = student.dog
            newStudent.cat = student.cat

            return newStudent
        }
        // 深克隆
        deepClone(student: Student): Student {
            if (!student || !(student instanceof Student)) {
                throw new Error("param is null or not Student instance.")
            }

            const newStudent = new Student()
            newStudent.name = student.name
            newStudent.age = student.age
            newStudent.height = student.height
            newStudent.dog = new Dog()
            newStudent.dog.dogName = student.dog?.dogName || ''
            newStudent.dog.dogIQ = student.dog?.dogIQ
            newStudent.cat = new Cat()
            newStudent.cat.catName = student.cat?.catName || ''
            newStudent.cat.catIQ = student.cat?.catIQ

            // const newStudent = JSON.parse(JSON.stringify(student))
            return newStudent
        }
    }

    // 复杂对象初始化
    const student1 = new Student()
    student1.name = '马文'
    student1.age = '8岁'
    student1.height = '120cm'
    student1.dog = new Dog()
    student1.dog.dogName = '黄元帅'
    student1.dog.dogIQ = 20
    student1.cat = new Cat()
    student1.cat.catName = '毛牙'
    student1.cat.catIQ = 12

    // 使用
    const cloneMachine = new CloneMachine()

    // 浅克隆
    const newStudent1 = cloneMachine.clone(student1)
    newStudent1.name = '马子明' // 部分修改
    console.log('newStudent1 === student1 ===>>> ', newStudent1 === student1) // false
    console.log('newStudent1.dog === student1.dog ===>>> ', newStudent1.dog === student1.dog) // true
    console.log('newStudent1.cat === student1.cat ===>>> ', newStudent1.cat === student1.cat) // true

    // 深克隆
    const newStudent2 = cloneMachine.deepClone(student1)
    newStudent2.name = '江钰' // 部分修改
    console.log('newStudent2 === student1 ===>>> ', newStudent2 === student1) // false
    console.log('newStudent2.dog === student1.dog ===>>> ', newStudent2.dog === student1.dog) // false
    console.log('newStudent2.cat === student1.cat ===>>> ', newStudent2.cat === student1.cat) // false
}
目录
相关文章
|
域名解析 Kubernetes 负载均衡
在K8S中,外部访问容器服务,比如说提供了一个域名,链路怎么走?数据经过哪些组件?
在K8S中,外部访问容器服务,比如说提供了一个域名,链路怎么走?数据经过哪些组件?
|
网络安全 Nacos 数据安全/隐私保护
nacos常见问题之配置内容不显示也修改不了如何解决
Nacos是阿里云开源的服务发现和配置管理平台,用于构建动态微服务应用架构;本汇总针对Nacos在实际应用中用户常遇到的问题进行了归纳和解答,旨在帮助开发者和运维人员高效解决使用Nacos时的各类疑难杂症。
2702 0
|
7天前
|
人工智能 运维 安全
|
5天前
|
人工智能 异构计算
敬请锁定《C位面对面》,洞察通用计算如何在AI时代持续赋能企业创新,助力业务发展!
敬请锁定《C位面对面》,洞察通用计算如何在AI时代持续赋能企业创新,助力业务发展!
|
6天前
|
机器学习/深度学习 人工智能 自然语言处理
B站开源IndexTTS2,用极致表现力颠覆听觉体验
在语音合成技术不断演进的背景下,早期版本的IndexTTS虽然在多场景应用中展现出良好的表现,但在情感表达的细腻度与时长控制的精准性方面仍存在提升空间。为了解决这些问题,并进一步推动零样本语音合成在实际场景中的落地能力,B站语音团队对模型架构与训练策略进行了深度优化,推出了全新一代语音合成模型——IndexTTS2 。
607 21
|
12天前
|
人工智能 JavaScript 测试技术
Qwen3-Coder入门教程|10分钟搞定安装配置
Qwen3-Coder 挑战赛简介:无论你是编程小白还是办公达人,都能通过本教程快速上手 Qwen-Code CLI,利用 AI 轻松实现代码编写、文档处理等任务。内容涵盖 API 配置、CLI 安装及多种实用案例,助你提升效率,体验智能编码的乐趣。
970 110
|
6天前
|
人工智能 测试技术 API
智能体(AI Agent)搭建全攻略:从概念到实践的终极指南
在人工智能浪潮中,智能体(AI Agent)正成为变革性技术。它们具备自主决策、环境感知、任务执行等能力,广泛应用于日常任务与商业流程。本文详解智能体概念、架构及七步搭建指南,助你打造专属智能体,迎接智能自动化新时代。