在 Javascript 中,每一个类形成一个单独的文件,然后相互引用
1、参考:在 重构:改善既有代码的设计 (第2版) 中,构筑测试体系 – 待测试的示例代码的编写:https://www.shuijingwanwq.com/2023/01/28/7368/ 。现在存在的问题是,多个类存在于同一个文件中。决定拆分开,每一个类形成一个单独的文件。
2、新建类文件 /modules/producer.js,将类定义迁移至文件中
class Producer { constructor(aProvince, data) { this._province = aProvince; this._cost = data.cost; this._name = data.name; this._production = data.production || 0; } get name() { return this._name; } get cost() { return this._cost; } set cost(arg) { this._cost = parseInt(arg); } get production() { return this._production; } set production(amountStr) { const amount = parseInt(amountStr); const newProduction = Number.isNaN(amount) ? 0 : amount; this._province.totalProduction += newProduction - this._production; this._production = newProduction; } } export {Producer};
3、新建类文件 /modules/province.js,将类定义迁移至文件中。导入类 Producer
import {Producer} from './producer.js'; class Province { constructor(doc) { this._name = doc.name; this._producers = []; this._totalProduction = 0; this._demand = doc.demand; this._price = doc.price; doc.producers.forEach(d => this.addProducer(new Producer(this, d))); } addProducer(arg) { this._producers.push(arg); this._totalProduction += arg.production; } get name() { return this._name; } get producers() { return this._producers.slice(); } get totalProduction() { return this._totalProduction; } set totalProduction(arg) { this._totalProduction = arg; } get demand() { return this._demand; } set demand(arg) { this._demand = parseInt(arg); } get price() { return this._price; } set price(arg) { this._price = parseInt(arg); } get shortfall() { return this._demand - this.totalProduction; } get profit() { return this.demandValue - this.demandCost; } get demandCost() { let remainingDemand = this.demand; let result = 0; this.producers .sort((a, b) => a.cost - b.cost) .forEach(p => { const contribution = Math.min(remainingDemand, p.production); remainingDemand -= contribution; result += contribution * p.cost; }); return result; } get demandValue() { return this.satisfiedDemand * this.price; } get satisfiedDemand() { return Math.min(this._demand, this.totalProduction); } } export {Province};
4、编辑 /productionPlan.js,删除两个类相关的代码,然后导入类 Province
import {Province} from './modules/province.js'; function sampleProvinceData() { return { name: "Asia", producers: [{name: "Byzantium", cost: 10, production: 9}, { name: "Attalia", cost: 12, production: 10 }, {name: "Sinope", cost: 10, production: 6},], demand: 30, price: 20 }; } const asia = new Province(sampleProvinceData()); console.log(asia.shortfall);
5、再次运行代码(建议强制刷新浏览器),在控制台的输出无变化,符合预期。如图1
6、最终的代码结构如下所示。如图2
近期评论