D
D
DiaTMss2019-08-13 02:27:22
PHP
DiaTMss, 2019-08-13 02:27:22

Will this implementation be an Abstract Factory?

Good day, I started reading the book, there are some code fragments that are not on githab, I did not find the source code, moreover, in English. It says that this pattern can be implemented using both an abstract class and an interface. But because of the interface, all public methods are obtained when I try to raise it to a closed php starts to swear. Perhaps I wrote the homocode because the author did not bother to write in full in the book (
5d51f84232846217512119.png

class Client
  {
    private $merchant;

    public function __construct(GardenMerchant $merchant)
    {
      $this->merchant = $merchant;
    }

    public function run() : string
    {
      return 'Your merchant made $' . $this->merchant->makeMoney();
    }
  }

  abstract class GardenMerchant
  {
    private $makeMoney = 0;
    private $store;
    private $items;

    abstract protected function createStore();
    abstract protected function createGarden();

    public function makeMoney() : int
    {
      $this->store = $this->createStore();
      $this->items = $this->createGarden()->items();

      foreach ($this->items as $item) 
      {
        $this->makeMoney += $this->store->price($item);
      }

      return $this->makeMoney;
    }
  }

  class DrugDealer extends GardenMerchant
  {
    protected function createStore() : MarijuanaStore
    {
      return new MarijuanaStore;
    }

    protected function createGarden() : MarijuanaGarden
    {
      return new MarijuanaGarden;
    }
  }

  class RiceFarmer extends GardenMerchant
  {
    protected function createStore() : RiceStore
    {
      return new RiceStore;
    }

    protected function createGarden() : RiceGarden
    {
      return new RiceGarden;
    }
  }

  class MarijuanaStore
  {
    public function price(int $product) : int
    {
      return $product;
    }
  }

  class RiceStore
  {
    public function price(int $product) : int
    {
      return $product;
    }
  }

  class MarijuanaGarden
  {
    public function items() : array
    {
      return [300, 400, 500];
    }
  }

  class RiceGarden
  {
    public function items() : array
    {
      return [100, 200, 300];
    }
  }

  $drug_dealer = new DrugDealer();
  $rice_farmer = new RiceFarmer();

  $client1 = new Client($drug_dealer);
  $client2 = new Client($rice_farmer);

  echo $client1->run();
  echo '<hr>';
  echo $client2->run();

class Client
  {
    private $merchant;

    public function __construct(GardenMerchantInterface $merchant)
    {
      $this->merchant = $merchant;
    }

    public function run()
    {
      echo 'Your merchant made $' . $this->merchant->makeMoney();
    }
  }

  interface GardenMerchantInterface
  {
    public function createStore();
    public function createGarden();
    public function makeMoney();
  }

  trait MakeMoneyTrait
  {
    public function makeMoney()
    {
      $makeMoney = 0;

      $store = $this->createStore();
      $items = $this->createGarden()->items();

      foreach ($items as $item) 
      {
        $makeMoney += $store->price($item);
      }

      return $makeMoney;
    }
  }

  class DrugDealer implements GardenMerchantInterface
  {
    use MakeMoneyTrait;

    public function createStore()
    {
      return new MarijuanaStore;
    }

    public function createGarden()
    {
      return new MarijuanaGarden;
    }
  }

  class RiceFarmer implements GardenMerchantInterface
  {
    use MakeMoneyTrait;

    public function createStore()
    {
      return new RiceStore;
    }

    public function createGarden()
    {
      return new RiceGarden;
    }
  }

  class MarijuanaStore
  {
    public function price($product)
    {
      return $product;
    }
  }

  class RiceStore
  {
    public function price($product)
    {
      return $product;
    }
  }

  class MarijuanaGarden
  {
    public function items()
    {
      return [300, 400, 500];
    }
  }

  class RiceGarden
  {
    public function items()
    {
      return [100, 200, 300];
    }
  }

  $drug_dealer = new DrugDealer();
  $rice_farmer = new RiceFarmer();

  $client1 = new Client($drug_dealer);
  $client2 = new Client($rice_farmer);

  $client1->run();
  echo '<hr>';
  $client2->run();

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Lazy @BojackHorseman PHP, 2019-08-13
@DiaTMss

No

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question