Facadeパターンとは?

今日は、デザインパターンの1つ、Facade(ファサード)パターンについて調べてみました。

Facadeパターンは、Wikipediaでは、

異なるサブシステムを単純な操作だけを持ったFacadeクラスで結び、サブシステム間の独立性を高める事を目的とする。
Facade パターン - Wikipedia

と説明されています。

以下のクラス図が示されています(Wikipediaより)。

Facadeパターンのクラス図

『PHPによるデザインパターン入門』

Facadeパターンは、複雑な関連を持つクラス群を簡単に利用するための「窓口」を用意するパターンです。

Facadeパターンの目的は、GoF本では次のように定義されています。

サブシステム内に存在する複数のインターフェースに1つの統一インターフェースを与える。 Facadeパターンはサブシステムの利用を容易にするための高レベルインターフェースを定義する。

Facadeパターンは、複雑に関連しあうクラス群を隠蔽するようなクラスを用意し、そのクラスに統一されたAPIを実装します。利用側はそのAPIを通じてクラス群を利用します。
Facade - GoFデザインパターン~STEP2:少し慣れたら - PHPによるデザインパターン入門 - Do You PHP?

以下のクラス図が示されています(PHPによるデザインパターン入門より)。

Facadeパターンのクラス図

DesignPatternsPHP

The primary goal of a Facade Pattern is not to avoid you to read the manual of a complex API. It's only a side-effect. The first goal is to reduce coupling and follow the Law of Demeter.

A Facade is meant to decouple a client and a sub-system by embedding many (but sometimes just one) interface, and of course to reduce complexity.
https://github.com/domnikl/DesignPatternsPHP/tree/master/Facade

ちなみに、

デメテルの法則(Law of Demetre (LoD))または最小知識の原則とは、ソフトウェアの設計、特にオブジェクト指向プログラムの設計におけるガイドラインである。 このガイドラインは1987年の末にかけてノースイースタン大学で作成された。簡潔に言うと「直接の友達とだけ話すこと」と要約できる。基本的な考え方は、任意のオブジェクトが自分以外(サブコンポーネント含む)の構造やプロパティに対して持っている仮定を最小限にすべきであるという点にある。
デメテルの法則 - Wikipedia

そして、Facadeのサンプルは、

<?php

namespace DesignPatterns\Facade;

/**
 *
 * 
 */
class Facade
{
    /**
     * @var OsInterface
     */
    protected $os;

    /**
     * @var BiosInterface
     */
    protected $bios;

    /**
     * This is the perfect time to use a dependency injection container
     * to create an instance of this class
     *
     * @param BiosInterface $bios
     * @param OsInterface   $os
     */
    public function __construct(BiosInterface $bios, OsInterface $os)
    {
        $this->bios = $bios;
        $this->os = $os;
    }

    /**
     * turn on the system
     */
    public function turnOn()
    {
        $this->bios->execute();
        $this->bios->waitForKeyPress();
        $this->bios->launch($this->os);
    }

    /**
     * turn off the system
     */
    public function turnOff()
    {
        $this->os->halt();
        $this->bios->powerDown();
    }
}

/**
Copyright (c) 2013 Dominik Liebler

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

https://github.com/domnikl/DesignPatternsPHP/blob/master/Facade/Facade.php

このFacadeクラスを使えば、osやbiosの知識なしで、systemを起動したり停止できるというサンプルです。

その他の参考URL

Date: 2014/03/10

Tags: php, programming