デコレーターパターンについてA Philosophy Of Software degine

「The motivation for decorators is to separate special-purpose extensions of a class from a more generic core. However, decorator classes tend to be shallow: they introduce a large amount of boilerplate for a small amount of new functionality. Decorator classes often contain many pass-through methods. It’s easy to overuse the decorator pattern, creating a new class for every small new feature. This results in an explosion of shallow classes, such as the Java I/ O example.」

—『A Philosophy of Software Design, 2nd Edition』John K. Ousterhout著
https://a.co/73pR7SN

 

デコレーターパターンは汎用機能をもとに個別の特別な機能を持つクラス\関数に切り出すパターン

 

デコレーターパターンは複数の機能開発をしていく中でスルーメソッドを使われることがよくある。

浅いモジュールの出来上がりとなる

 

デコレーターパターンを著者は否定しないが利用する場合以下の点に気をつけるべきと記載している

注意:抜粋です

 

  • デコレーターとなるクラスを作らず元のベースクラスに機能を追加する
  • 特定のユースケースにのみ紐づく場合はユースケース側にこの機能を追加する
  • 新しいデコレーターの作成ではなく既存のデコレーターにマージ
  • デコレーターをする必要があるか問う デコレーターではなく別の基礎クラスとして切り分ける

 

 

背景などもう少し記述が違ったりするので詳細は本編に任せます