Skip to content
Naoya Tech
Go back

Inheritance vs. Composition, Explained with an Image Library

9:50 Programming & CS

Overview

“Prefer composition over inheritance” is familiar advice, but it is a little vague. This video tests it on the code of an image library. Inheritance and composition are tools for the same goal: reusing code you have already written.

The video shows where inheritance breaks down, what the same design looks like with composition, and when inheritance is still worth using.

Key Points

Chapters

概要

「継承よりコンポジションを選べ」はよく聞く格言ですが、少し曖昧な助言でもあります。この動画では、画像ライブラリのコードを使ってこの助言を確かめます。継承もコンポジションも、書いたコードを再利用するという同じ目的のための道具です。

継承がどこで破綻するのか、同じ設計をコンポジションで書くとどうなるのか、そして継承が今でも役に立つのはどんなときかを解説します。

ポイント

  • リサイズと反転を持ち、保存と読み込みを抽象メソッドにして JpgImage・PngImage・BmpImage に実装させる Image クラスは、ファイルに由来しない、ユーザーが絵を描ける画像が必要になるまではうまく動きます。DrawableImage は意味のない save と load の実装を強いられ、新しい親 FileImage を間に挟むと既存のコードが壊れます。
  • コンポジションでは、Image はメモリ上の画像だけを表し、ファイル形式のクラスは画像を引数として受け取ります。新しい ImageDraw クラスも、他のクラスに一切手を加えずに追加できます。
  • 継承は、コードを再利用する能力と抽象化を作る能力の2つをまとめたものです。ImageFile インターフェースは抽象化を契約だけとして提供し、そのインターフェースを ImageApp の外から渡すのが依存性の注入です。
  • コンポジションにも、定型コードやラッパーメソッドが増えるといった欠点があります。それでもオブジェクト同士の接触面が小さくなるため、変更したときの痛みが減ります。
  • 継承は、重複だらけの巨大なシステムをすぐに1か所だけ直したいときには有効です。使うなら継承される前提で設計し、変数を安易に protected にせず、上書き用には明示的な protected の API だけを用意して、それ以外は private、final、sealed にします。

チャプター

Watch on YouTube

Share this post on:

Previous Video
How Networks Work: Ethernet, TCP/IP, TLS, and DNS
Next Video
The Brian Kernighan Story: Creator of AWK and 50 Years with Unix and C