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

値と属性の話

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
Avatar for Akira Suenami Akira Suenami
May 12, 2022
290

 値と属性の話

Avatar for Akira Suenami

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; } }