W
W
WQP2017-07-16 10:15:43
PHP
WQP, 2017-07-16 10:15:43

What pattern is suitable for this?

Hello, I can not decide which pattern will be the most convenient for this.
How to bind multiple classes in one? We have several classes Car, Bus, Plane. All of these classes have one method getItems()(all from the same interface, code below). How to connect them in one class Transport?
For access to be like this:

$carItems = (new Transport)->setTransport('Car')->getItems();
$busItems = (new Transport)->setTransport('Bus')->getItems();

// а также 

$carItems = (new Car)->getItems();
$busItems = (new Bus)->getItems();

TransportInterface
interface TransportInterface
{
    public function getItems();
}

car
class Car implements TransportInterface
{
    public function getItems()
    {
        return [];
    }
}

Bus
class Bus implements TransportInterface
{
    public function getItems()
    {
        return [];
    }
}

plane
class Plane implements TransportInterface
{
    public function getItems()
    {
        return [];
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vlad, 2017-07-16
@d1skort

"Factory method" (setTransport) and it is desirable to carry away getItems using the "Strategy" pattern (the mutable must be separated from the immutable)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question