0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

メディエーターパターンは神

Posted at

API上のコードとjs上の列挙体の値とラベル文字列の値の3要素がある。

enum MessageStringCode {
  PowerOn = '01',
  PowerOff = '02'  
}

enum MessageType {
  PowerOn,
  PowerOff,
}

enum MessageLabel {
  PowerOn = 'パワーオン',
  PowerOff = 'パワーオフ'
}

これらを相互変換するために以下の関数を定義する。

function stringCodeToType(a, b) { ... }
function stringCodeToLabel(a, b) { ... }
function typeToStringCode(a, b) { ... }
function typeToLabel(a, b) { ... }
function labelToStringCode(a, b) { ... }
function labelToType(a, b) { ... }

FUCK!!!

もう一度言おう

FUCK!!!

メディエーターパターンを使おう

詳しくはこれ見てちょ

メディエーターパターンを使えば定義する関数の数が圧倒的に減る

function stringCodeToLabel(a, b) { ... }
function typeToLabel(a, b) { ... }
function labelToStringCode(a, b) { ... }
function labelToType(a, b) { ... }

変換関数をLabel経由に集約してstringCode -> Type へ変換したい場合は stringCode -> Label -> Type のように変換する

何が言いたいかというと、全てを一つのものに集約する形で実装しようってことです。

おわり

デザインパターンは絶対に役に立つ日が来るので勉強しよう

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?