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

当たり前にリリースしていくために必要なこと / Release safely

当たり前にリリースしていくために必要なこと / Release safely

Avatar for soudai sone

soudai sone

April 21, 2021
Tweet

More Decks by soudai sone

Other Decks in Technology

Transcript

  1. 自己紹介
 曽根 壮大(36歳)
 Have Fun Tech LLC 代表社員
 
 そ 

    ね   たけ とも
 • 日本PostgreSQLユーザ会 勉強会分科会 担当
 • 3人の子供がいます(長女、次女、長男)
 • 技術的にはWeb/LL言語/RDBMSが好きです
 • コミュニティが好き
  2. <?php class PlainText { private $textString = null; public function

    getText() { return $this->textString; } public function setText($str) { $this->textString = $str; } } 文字列を扱うクラス。 これに大文字するメソッドを追 加したい場合は? Decoratorパターン
  3. <?php class PlainText { private $textString = null; public function

    getText() { return $this->textString; } public function setText($str) { $this->textString = $str; } // 現場で一番良く見る実装 public function upperCaseGetText($str) { return mb_strtoupper($this->getText()); } } 仕様追加のたびにPlainTextク ラスが大きくなる。 小文字にしたい場合など、どん どんこのクラスが大きくなって いくことが目に見えている。 getText()にifを追加するのは もっとダメ。 Decoratorパターン
  4. <?php /** * テキストを扱うインターフェースクラスです */ interface Text { public function

    getText(); public function setText($str); } Decoratorパターンの場合。 まずはインターフェイスを定義 する。 Decoratorパターン
  5. <?php class PlainText implements Text { private $textString = null;

    public function getText() { return $this->textString; } public function setText($str) { $this->textString = $str; } } 先程のクラスと一緒。 インターフェイスに対する 実装クラスになった。 Decoratorパターン
  6. <?php require_once('Text.class.php'); abstract class TextDecorator implements Text { private $text;

    public function __construct(Text $target) { $this->text = $target; } public function getText() { return $this->text->getText(); } public function setText($str) { $this->text->setText($str); } } Decoratorクラスを作る。 コードの遊び部分になる。 Decoratorパターン
  7. <?php require_once('TextDecorator.class.php'); class UpperCaseText extends TextDecorator { public function __construct(Text

    $target) { parent::__construct($target); } public function getText() { $str = parent::getText(); $str = mb_strtoupper($str); return $str; } } さぁ!Textをdecorateしてみ ましょう!! UpperCaseTextクラスを実装 するとこうなる。 元のPlainTextクラスに全く影 響を与えずに機能を追加する ことができた。 Decoratorパターン
  8. <?php require_once('TextDecorator.class.php'); class DoubleByteText extends TextDecorator { public function __construct(Text

    $target) { parent::__construct($target); } /** * テキストを全角文字に変換して返します * 半角英字、数字、スペース、カタカナを全角に、 * 濁点付きの文字を一文字に変換します */ public function getText() { $str = parent::getText(); $str = mb_convert_kana($str,"RANSKV"); return $str; } } さらに機能追加。 機能追加の単位がクラスに閉 じるので仕様追加の範囲が絞 ることができる。 1クラス、1責務。 シンプルでわかりやすい。 Decoratorパターン