Upgrade to Pro — share decks privately, control downloads, hide ads and more …

値と属性の話

Akira Suenami
May 12, 2022
140

 値と属性の話

Akira Suenami

May 12, 2022
Tweet

More Decks by Akira Suenami

Transcript

  1. 自己紹介 • 末並 晃 @a_suenami • 生息している界隈: DDDとか、TDDとか、RDBとか • お仕事で使ってる技術スタック:

    Rails, React, Java ◦ 最近は terraform おじさんです • 好きな RDBMS: PostgreSQL • 好きな制約: チェック制約 • 好きな焼肉の部位: ハラミ • 好きな(ry
  2. 値オブジェクト enum Currency { JPY, USD } class Money {

    private Currency currency; private int amount; Money(final Currency currency, final int amount) { this.currency = currency; this.amount = amount; } Currency getCurrency() { return this.currency; } int getAmount() { return this.amount; } Money add(Money another) { if (this.currency != another.getCurrency()) { throw new IllegalArgumentException("通貨が異なります"); } return new Money(this.currency, this.amount + another.getAmount()); } }
  3. 値オブジェクト? class ProductUnitPrice { private Currency currency; private int amount;

    ProductUnitPrice(final Currency currency, final int amount) { this.currency = currency; this.amount = amount; } Currency getCurrency() { return this.currency; } int getAmount() { return this.amount; } }