Help us understand the problem. What is going on with this article?

タイプスクリプトでドメインオブジェクトやってみた

読んだ
現場で役立つシステム設計の原則 〜変更を楽で安全にするオブジェクト指向の実践技法 Kindle版

成果物1
class ShippingCost {
  readonly basePrice: number;
  readonly MINIMUM_FOR_FREE = 3000
  readonly COST = 500
  constructor(basePrice: number){
    this.basePrice = basePrice
  }

  amount(): number {
    if(this.basePrice < this.MINIMUM_FOR_FREE) return this.COST
    return 0
  }
}

const ABOVE_THRESHOLD: number = 5000
const BELOW_THRESHOLD: number = 300

const above_threshold_cost: ShippingCost = new ShippingCost(ABOVE_THRESHOLD)
console.log(above_threshold_cost.amount())
// -> 0
// 3000円以上の注文なので送料0円

const below_threshold_cost: ShippingCost = new ShippingCost(BELOW_THRESHOLD)
console.log(below_threshold_cost.amount())
// -> 500
// 3000円未満の注文なので送料500円

送料のみを求めるクラス
外側から送料を変更できない(readonly)


成果物2
class Quantity {
  readonly MIN: number = 1
  readonly MAX: number = 100
  readonly quantity: number

  constructor(quantity: number) {
    if(quantity < this.MIN) throw new RangeError("不正:" + this.MIN + "未満")
    if(quantity > this.MAX) throw new RangeError("不正:" + this.MAX + "超過")
    this.quantity = quantity
  }

  canAdd(quantity: Quantity) {
    const added: number = this.addValue(quantity)
    return added <= this.MAX
  }

  add(quantity: Quantity) {
    if(!this.canAdd(quantity)) throw new RangeError("不正:合計が" + this.MAX + "超過")
    const added: number = this.addValue(quantity)
    return new Quantity(added)
  }

  private addValue(quantity: Quantity){ // privateはちょっとprivateらしい
    return this.quantity + quantity.quantity
  }
}

const addedQuantity: Quantity = new Quantity(40)
console.log(addedQuantity.quantity)
let below: Quantity = new Quantity(50)
console.log(addedQuantity.canAdd(below))
// -> true
let above: Quantity = new Quantity(90)
console.log(addedQuantity.canAdd(above))
// -> false
addedQuantity.add(below)
// readonlyなので変更が行われない(エラーが出ないのは良くないと思った)
console.log(addedQuantity.quantity)
// -> 40

値が範囲内に収まるか?or値を追加するクラス
しきい値の変更ができない(readonly)
初期化時に範囲外ならエラー

readonly quantity: number

quantity: number

変更すれば、MAXまでは追加できるが、超過するとエラー


細かく区切れば、変更しやすいとか利点がある。
意識していきたい。
極端に言えば以下でも成り立つわけで、、、意識していきたい

class Quantity {
  quantity: number

  constructor(quantity: number) {
    this.quantity = quantity
  }
}
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした