2.7. Facade

2.7.1. Amaç

The primary goal of a Facade Pattern is not to avoid you having 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.

Bir önyüz, bir veya birden fazla arayüzü gömerek (interface injection) bir istemci ve bir alt sistemi ayrıştırmak ve tabii ki karmaşıklığı azaltmak içindir.

  • Bir önyüz alt sisteme erişimi yasaklamaz.

  • Bir alt sistem için birden fazla önyüze sahip olabilirsiniz (olmalısınız).

İyi tasarlamış bir önyüzde new diye bir şey olmamasının nedeni budur. Her yöntem için birden fazla oluşturum varsa (multiple creation), bu artık bir Önyüz değil, bir Yapıcı (Builder) ya da [Soyut (Abstract)|Statik (Static)|Basit (Simple)] Fabrika (Factory) [Yöntem (Method)] olur.

En iyi önyüz new içermeyen ve kurucusu (constructor) arayüz türü ipuçlanarak (interface-type-hinted) parametrize edilmiş olandır. Şayet yeni örnekler (instance) oluşturmanız gerekiyorsa, argüman olarak bir Fabrika (Factory) kullanın.

2.7.2. UML Diyagramı

Alt Facade UML Diyagramı

2.7.3. Kod

Bu kodu Github üzerinde de bulabilirsiniz.

Facade.php

 1<?php
 2
 3declare(strict_types=1);
 4
 5namespace DesignPatterns\Structural\Facade;
 6
 7class Facade
 8{
 9    public function __construct(private Bios $bios, private OperatingSystem $os)
10    {
11    }
12
13    public function turnOn()
14    {
15        $this->bios->execute();
16        $this->bios->waitForKeyPress();
17        $this->bios->launch($this->os);
18    }
19
20    public function turnOff()
21    {
22        $this->os->halt();
23        $this->bios->powerDown();
24    }
25}

OperatingSystem.php

 1<?php
 2
 3declare(strict_types=1);
 4
 5namespace DesignPatterns\Structural\Facade;
 6
 7interface OperatingSystem
 8{
 9    public function halt();
10
11    public function getName(): string;
12}

Bios.php

 1<?php
 2
 3declare(strict_types=1);
 4
 5namespace DesignPatterns\Structural\Facade;
 6
 7interface Bios
 8{
 9    public function execute();
10
11    public function waitForKeyPress();
12
13    public function launch(OperatingSystem $os);
14
15    public function powerDown();
16}

2.7.4. Test

Tests/FacadeTest.php

 1<?php
 2
 3declare(strict_types=1);
 4
 5namespace DesignPatterns\Structural\Facade\Tests;
 6
 7use DesignPatterns\Structural\Facade\Bios;
 8use DesignPatterns\Structural\Facade\Facade;
 9use DesignPatterns\Structural\Facade\OperatingSystem;
10use PHPUnit\Framework\TestCase;
11
12class FacadeTest extends TestCase
13{
14    public function testComputerOn()
15    {
16        $os = $this->createMock(OperatingSystem::class);
17
18        $os->method('getName')
19            ->will($this->returnValue('Linux'));
20
21        $bios = $this->createMock(Bios::class);
22
23        $bios->method('launch')
24            ->with($os);
25
26        /** @noinspection PhpParamsInspection */
27        $facade = new Facade($bios, $os);
28        $facade->turnOn();
29
30        $this->assertSame('Linux', $os->getName());
31    }
32}