Skip to content

Commit

Permalink
✨ 【第12章 設計とメタファー】 $5 + 5 CHF を実現するため plus メソッドは Expression(式)として返すようにする
Browse files Browse the repository at this point in the history
・Expression(式)がそれ以外の事柄をできるだけ知らいないような設計を続けることで、柔軟性が高く、かつテストや再利用、理解が簡単な状態を保てるようにするため採用する
・結果の Expression オブジェクトは為替レートによって1つの通貨に換算できることを想定している
  • Loading branch information
dodonki1223 committed Dec 19, 2021
1 parent 325811c commit da1c24c
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/__tests__/money.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// import { Dollar } from '../dollar';
// import { Franc } from '../franc';
import { Bank } from '../bank';
import { Money } from '../money';
import { Bank } from '../bank';

test('equals', () => {
expect(Money.dollar(5).equals(Money.dollar(5))).toBeTruthy();
Expand Down Expand Up @@ -34,7 +34,7 @@ test('times', () => {
});

test('simple addition', () => {
const sum :Money = Money.dollar(5).plus(Money.dollar(5));
const sum = Money.dollar(5).plus(Money.dollar(5));
expect(sum).toEqual(Money.dollar(10));

const bank = new Bank();
Expand Down
3 changes: 2 additions & 1 deletion src/bank.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Expression } from './expression';
import { Money } from './money';

export class Bank {
reduce(sum: Money, currency: string):Money {
reduce(sum: Expression, currency: string):Money {
return Money.dollar(10);
}
}
2 changes: 2 additions & 0 deletions src/expression.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export interface Expression{
}
6 changes: 4 additions & 2 deletions src/money.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export class Money {
import { Expression } from "./expression";

export class Money implements Expression {
constructor(protected readonly amount: number, public readonly currency: string) {
}

Expand Down Expand Up @@ -26,7 +28,7 @@ export class Money {
return `${this.amount} ${this.currency}`
}

plus(addend: Money):Money {
plus(addend: Money):Expression {
return new Money(this.amount + addend.amount, this.currency)
}
}

0 comments on commit da1c24c

Please sign in to comment.