领域驱动设计(DDD)前夜:三种编码方式

三种编码方式

一个开灯设置颜色的示例,使用三种编码方式来实现。分别是:脚本化编程、具有函数思维的过程化编程以及面向对象编程。

脚本化编程:

class Light {
    status: string
    color: string
}

function main() {
    const light = new Light()
    if (light.status === "on") {
        throw new Error("The light is on")
    }
    light.status = "on"
    light.color = "red"
}

具有函数思维的过程化编程:

class Light {
    status: string
    color: string
}

function open(light: Light) {
    if (light.status === "on") {
        throw new Error("The light is on")
    }
    light.status = "on"
}

function setColor(light: Light, color: string) {
    if (light.status !== "on") {
        throw new Error("The light is not on")
    }
    light.color = color
}

function main() {
    const light = new Light()
    open(light)
    setColor(light, "red")
}

面向对象编程:

class Light {
    #status: string
    #color: string

    public on() {
        if (this.#status === "on") {
            throw new Error("The light is on")
        }
        this.#status = "on"
    }

    public set color(color: string) {
        if (this.#status !== "on") {
            throw new Error("The light is not on")
        }
        this.#color = color
    }
}

function main() {
    const light = new Light()
    light.on()
    light.color = "red"
}

都是 Light 咋有那么多实现,你慢慢品…,你平常用那一种方式呢?

开源电商

Mallfoundry 是一个完全开源的使用 Spring Boot 开发的多商户电商平台。它可以嵌入到已有的 Java 程序中,或者作为服务器、集群、云中的服务运行。

  • 领域模型采用领域驱动设计(DDD)、接口化以及面向对象设计。

项目地址:https://gitee.com/mallfoundry/mall

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值