I
I
indigo_style2019-02-19 13:34:09
PHP
indigo_style, 2019-02-19 13:34:09

Should objects be created inside the Facade (pattern)?

I looked at the examples, and did not understand whether objects should be created inside the facade.
PHP examples taken from articles.

class Facade
{
    public function transfer($amount)
    {
        $Bank = new Bank();
        $Client = new Client();
        $Log = new Log();

        $Bank->OpenTransaction();
        $Client->OpenTransaction();
        $Log->logTransaction('Transaction open');

        $Bank->transferMoney(-$amount);
        $Log->logTransaction('Transfer money from bank');

        $Client->transferMoney($amount);
        $Log->logTransaction('Transfer money to client');

        $Bank->CloseTransaction();
        $Client->CloseTransaction();
        $Log->logTransaction('Transaction close');
    }
}

or do it right
class Facade
{
    private $os;
    private $bios;

    public function __construct(BiosInterface $bios, OsInterface $os)
    {
        $this->bios = $bios;
        $this->os = $os;
    }

    public function turnOn()
    {
        $this->bios->execute();
        $this->bios->waitForKeyPress();
        $this->bios->launch($this->os);
    }

    public function turnOff()
    {
        $this->os->halt();
        $this->bios->powerDown();
    }
}

Thank you in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
illuzor, 2019-02-19
@indigo_style

Second. It's better to provide dependencies externally, and not just for the facade, but in general.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question