48
Trait とは? その使い道を考えてみる 藤村 拓也 @tlync 1289日木曜日

Trait とは? その使い道を考えてみる

Embed Size (px)

DESCRIPTION

使い道を考える時間があまりありませんでした。教えてください。

Citation preview

Page 1: Trait とは? その使い道を考えてみる

Trait とは?その使い道を考えてみる

藤村 拓也 @tlync

12年8月9日木曜日

Page 2: Trait とは? その使い道を考えてみる

自己紹介

12年8月9日木曜日

Page 3: Trait とは? その使い道を考えてみる

自己紹介

• 藤村 拓也@tlync

• アプリケーション開発第二• 最近の関心事

• Scala, Agile(Project Mgt.)

• PHP は生きる為にやっています

12年8月9日木曜日

Page 4: Trait とは? その使い道を考えてみる

※ ちなみに残念ながら? PHP の Trait のお話しです

12年8月9日木曜日

Page 5: Trait とは? その使い道を考えてみる

Trait を知ってる人?

12年8月9日木曜日

Page 6: Trait とは? その使い道を考えてみる

Trait を説明できる人?

12年8月9日木曜日

Page 7: Trait とは? その使い道を考えてみる

trait /tréit | tréi/

名 詞⦅形式的⦆1 (性格習慣の)特徴, 特色, 特質admirable traits like honesty and courage|誠実さや勇気といったすばらしい特質American traits|米国国民性.2⦅希用語⦆(ペン鉛筆などの)一筆, 筆使い;(性質などの)気味, 気配⦅of ...⦆a trait of sadness|一まつの悲哀.

12年8月9日木曜日

Page 8: Trait とは? その使い道を考えてみる

特徴,特色,特質

12年8月9日木曜日

Page 9: Trait とは? その使い道を考えてみる

Trait とは?

• メソッドとプロパティの集合• 単一継承の制約を軽減する

• 継承関係とは関係なく、共通の特性、振舞いを再利用(水平展開)できる

• それ自体ではインスタンス化できない

12年8月9日木曜日

Page 10: Trait とは? その使い道を考えてみる

継承関係と関係なく実装 を 再利用 できる仕組み

12年8月9日木曜日

Page 11: Trait とは? その使い道を考えてみる

PHP 5.4 <=

12年8月9日木曜日

Page 12: Trait とは? その使い道を考えてみる

他言語での類似の機構• Scala … trait

• Perl 6 … role

• Ruby … Mix-in

※同様の概念だが仕様は微妙に異なる。型情報を持つか(is_aをパスするか)とか。

12年8月9日木曜日

Page 13: Trait とは? その使い道を考えてみる

簡単なサンプル

12年8月9日木曜日

Page 14: Trait とは? その使い道を考えてみる

trait Engine{ public function startEngine() { echo 'BRRRM!'; }}

class Car{ use Engine;}

(new Car)->startEngine(); // BRRRM!

12年8月9日木曜日

Page 15: Trait とは? その使い道を考えてみる

これは継承でもできなくはない

12年8月9日木曜日

Page 16: Trait とは? その使い道を考えてみる

では、これは?

12年8月9日木曜日

Page 17: Trait とは? その使い道を考えてみる

class Vehicle{ protected function startEngine() { echo '⚡'; }}

class Plane extends Vehicle{ public function fly() { echo '✈'; }}

class Boat extends Vehicle{ public function sail() { echo '⚓'; }}

12年8月9日木曜日

Page 18: Trait とは? その使い道を考えてみる

class SeaPlane extends ???{ // ボートして走らせたいし、飛行機として飛ばしたい。どうすれば…}

12年8月9日木曜日

Page 19: Trait とは? その使い道を考えてみる

startEngine()

Vehicle

sail()

Boat

fly()

Plane

sail()fly()

SeaPlane

???

12年8月9日木曜日

Page 20: Trait とは? その使い道を考えてみる

多重継承はできない

12年8月9日木曜日

Page 21: Trait とは? その使い道を考えてみる

委譲(Composition) ?

12年8月9日木曜日

Page 22: Trait とは? その使い道を考えてみる

interface Plane{ public function fly();}

interface Boat{ public function sail();}

12年8月9日木曜日

Page 23: Trait とは? その使い道を考えてみる

class SimplePlane extends Vehicle implements Plane{ public function fly() { echo '✈'; }}

class SimpleBoat extends Vehicle implements Boat{ public function sail() { echo '⚓'; }}

12年8月9日木曜日

Page 24: Trait とは? その使い道を考えてみる

class SeaPlane extends Vehicle implements Plane, Boat{ private $boat; private $plane;

public function __constract() { $this->boat = new SimpleBoat(); $this->plane = new SimplePlane(); }

public function sail() { return $this->boat->sail(); }

public function fly() { return $this->plane->fly(); }}

$seaPlane = new SeaPlane();$seaPlane->sail(); // !$seaPlane->fly(); // ✈

12年8月9日木曜日

Page 25: Trait とは? その使い道を考えてみる

形式的な記述が多いやや面倒

12年8月9日木曜日

Page 26: Trait とは? その使い道を考えてみる

Trait で実装の共有をしてみる

12年8月9日木曜日

Page 27: Trait とは? その使い道を考えてみる

// 命名はちょっと微妙trait FlyEngine{ public function fly() { echo '✈'; }}

trait FloatEngine{ public function sail() { echo '⚓'; }}

class SeaPlane implements Plane, Boat // 型の継承{ use FloatEngine, FlyEngine; // 実装の継承}

$seaPlane = new SeaPlane();$seaPlane->sail(); // !$seaPlane->fly(); // ✈

12年8月9日木曜日

Page 28: Trait とは? その使い道を考えてみる

記述はすっきり実装の共有ができた

12年8月9日木曜日

Page 29: Trait とは? その使い道を考えてみる

startEngine()

Vehicle

sail()

Boat

fly()

Plane

sail()fly()

SeaPlane

sail()

BoatEngine

fly()

FlyEngine

sail()

BoatEngine

fly()

FlyEngine

12年8月9日木曜日

Page 30: Trait とは? その使い道を考えてみる

型の保証 … Interface実装の再利用 … trait

※ 注: Scala, Ruby などではまたちょっと違うよ!

12年8月9日木曜日

Page 31: Trait とは? その使い道を考えてみる

Trait は継承ツリーに関係なく実装の再利用を行うだけ

12年8月9日木曜日

Page 32: Trait とは? その使い道を考えてみる

で、実際どう使うと便利なのか?

12年8月9日木曜日

Page 33: Trait とは? その使い道を考えてみる

1. 抽象クラスを作る前に Trait の検討

12年8月9日木曜日

Page 34: Trait とは? その使い道を考えてみる

抽象クラスの主な理由はコードの再利用(多分)

12年8月9日木曜日

Page 35: Trait とは? その使い道を考えてみる

Trait が利用できないか検討する

12年8月9日木曜日

Page 36: Trait とは? その使い道を考えてみる

※Trait 中2病には注意しましょう

12年8月9日木曜日

Page 37: Trait とは? その使い道を考えてみる

2. Entity の振舞いの共有

12年8月9日木曜日

Page 38: Trait とは? その使い道を考えてみる

Entity の振舞い

• タイムスタンプ管理• 変更のトレース• ソフトデリート可能か• など…

12年8月9日木曜日

Page 39: Trait とは? その使い道を考えてみる

trait Timestampable{

private $createdAt; private $updatedAt;

... getter, setter

/** * Updates createdAt and updatedAt timestamps. */ public function updateTimestamps() { if (null === $this->createdAt) { $this->createdAt = new \DateTime('now'); }

$this->updatedAt = new \DateTime('now'); }}

12年8月9日木曜日

Page 40: Trait とは? その使い道を考えてみる

/** * 商品エンティティ */class Product{ use Timestampable, // タイムスタンプを自動で SoftDeletable, // ソフトデリート可能に Activatable; // 活性、非活性を可能に

private $name;

// 本質的なロジック}

12年8月9日木曜日

Page 41: Trait とは? その使い道を考えてみる

3

12年8月9日木曜日

Page 42: Trait とは? その使い道を考えてみる

時間なかった\(^o^)/

12年8月9日木曜日

Page 43: Trait とは? その使い道を考えてみる

いいアイディアあったら教えてください

12年8月9日木曜日

Page 44: Trait とは? その使い道を考えてみる

まとめ

12年8月9日木曜日

Page 45: Trait とは? その使い道を考えてみる

Trait を知り、PHP 5.4 時代の新たな設計を探ろう

12年8月9日木曜日

Page 46: Trait とは? その使い道を考えてみる

それか

12年8月9日木曜日

Page 47: Trait とは? その使い道を考えてみる

この会社から PHP を撲滅しよう

12年8月9日木曜日

Page 48: Trait とは? その使い道を考えてみる

おわり

12年8月9日木曜日