实体

设计模式 / 整洁架构模式
image

//实体
class OrderLine {
    //这一行针对什么商品,人人都能读到,所以它是普通字段。
    //price 和 count 承载着上面检查过的规则,
    //而带 # 的名称是真正的锁:
    //类体之外的任何代码连引用它都做不到
    #price;
    #count;

    constructor(sku, price, count) {
        if (count <= 0) {
            throw new Error("bad count " + sku);
        }
        this.sku = sku;
        this.#price = price;
        this.#count = count;
    }

    cost() {
        return this.#price * this.#count;
    }
}

//实体
class Order {
    //TypeScript 中的 private 只是写给编译器看的词,类型转换就能绕过它;
    //# 则是语言本身保证的锁
    #lines = [];
    #paid = false;

    constructor(id) {
        this.id = id;
    }

    add(sku, price, count) {
        if (this.#paid) {
            throw new Error("order is paid");
        }
        this.#lines.push(new OrderLine(sku, price, count));
        return this;
    }

    //商品以冻结副本的形式交出,
    //所以调用方向其中 push 也改变不了这里的数据
    goods() {
        return Object.freeze(this.#lines.slice());
    }

    size() {
        return this.#lines.length;
    }

    total() {
        let sum = 0;
        for (let line of this.#lines) {
            sum += line.cost();
        }
        return sum;
    }

    //业务规则:满 200 优惠 10%
    discount() {
        let total = this.total();
        return total >= 200 ? Math.trunc(total / 10) : 0;
    }

    payable() {
        return this.total() - this.discount();
    }

    //业务规则:空订单永远不能支付
    pay() {
        if (this.#lines.length === 0) {
            throw new Error("empty order");
        }
        this.#paid = true;
        return this;
    }

    paid() {
        return this.#paid;
    }
}

//客户端代码
//上方没有数据库,也没有界面
let order = new Order(1001);
order.add("pen", 30, 2);
order.add("lamp", 190, 1);
console.log("order " + order.id);
console.log("lines " + order.size());
console.log("total " + order.total());
console.log("discount " + order.discount());
console.log("payable " + order.payable());

order.pay();
console.log("paid " + order.paid());

//同一条规则,换用较小的订单
let small = new Order(1002);
small.add("pen", 30, 2);
console.log("small total " + small.total());
console.log("small discount " + small.discount());

//交出的副本会直接报错拒绝新增一行,
//而订单无论如何都保持原有的两行
let taken = order.goods();
console.log("copy holds " + taken[0].sku +
    " and " + taken[1].sku);
try {
    taken.push(new OrderLine("chair", 1, 1));
} catch (error) {
    console.log("refused: " + error.name + " on the copy");
}
console.log("still " + order.size());

//所有拒绝都来自规则本身,
//存储和界面都没有参与其中
try {
    order.add("pen", 30, 1);
} catch (error) {
    console.log("refused: " + error.message);
}

try {
    new Order(1003).pay();
} catch (error) {
    console.log("refused: " + error.message);
}

try {
    new OrderLine("pen", 30, 0);
} catch (error) {
    console.log("refused: " + error.message);
}
//printed: order 1001
//         lines 2
//         total 250
//         discount 25
//         payable 225
//         paid true
//         small total 60
//         small discount 0
//         copy holds pen and lamp
//         refused: TypeError on the copy
//         still 2
//         refused: order is paid
//         refused: empty order
//         refused: bad count pen


An entity holds a business rule of the shop and knows nothing about a database, a screen or the network, so it can be tested without them.
Read more

问题。商店对金额达到 200 的每笔订单减免 10%。如果这条规则写在网页里,又在数据库查询里写了一遍,两份副本很快就会不一致。要检查这条规则,就得启动网页或数据库。商店最重要的知识分散各处,难以测试。

思路。实体是一段数据,连同与它相关的业务规则:订单包含什么,总额如何计算,何时可以付款。它对数据库、界面或网络一无所知。整洁架构有一条主要规则,即依赖规则:外层代码可以了解内层代码,但内层代码从不了解外层代码。实体是最内层的代码,因此不依赖任何东西。

如何阅读示例。示例中有两个实体:订单行和订单本身。订单汇总各行,对 200 及以上的金额减免 10%,并计算应付金额。订单为空时,它拒绝付款。客户端代码构建含两个订单行的订单 1001,并打印其中的数字:总额 250,折扣 25,应付 225。它还尝试为一个空订单付款,实体予以拒绝。在某些语言中,实体是带有函数的普通值,而不是类;它的角色不变。

何时使用。业务中每个拥有自己规则的概念,例如订单或账户,都应做成一个实体。没有规则的数据不需要实体:简单的结构就够了。

常见错误。实体自己把自己保存到数据库,或者为界面格式化自己。这样它就依赖于外层代码,它的规则也无法再单独测试。