Coder Social home page Coder Social logo

Comments (2)

weburnit avatar weburnit commented on May 21, 2024

My propose

interface PackageBuilderInterface
{
    /**
     * Package
     *
     * @param PackageItemInterface $item
     *
     * @return bool
     */
    public function pack(PackageItemInterface $item);

    /**
     * @return array
     */
    public function getPackages():array;
}

interface PackageItemInterface
{
    /**
     * @return string
     */
    public function getWarehouse(): string;

    /**
     * @return string
     */
    public function get3PL(): string;

    /**
     * @return int
     */
    public function getOwnerUserId(): int;
}
abstract class AbstractBuilder implements PackageBuilderInterface
{
    /**
     * @var array
     */
    protected $packages;

    /**
     * @var PackageBuilderInterface
     */
    private $builder;

    /**
     * Builder constructor.
     *
     * @param PackageBuilderInterface $builder
     */
    public function __construct(PackageBuilderInterface $builder = null)
    {
        $this->packages = [];
        $this->builder  = $builder;
    }

    /**
     * {@inheritdoc}
     */
    final public function getPackages(): array
    {
        $packages = $this->packages;
        if ($this->builder) {
            $packages = [];
            foreach ($this->packages as $package) {
                foreach ($package as $item) {
                    $this->builder->pack($item);
                }
                $fromSeller = $this->builder->getPackages();
                $packages   = array_merge(array_values($fromSeller), $packages);
            }
        }
        $this->packages = [];

        return $packages;
    }
}
class SellerClassifier extends AbstractBuilder
{
    /**
     * {@inheritdoc}
     */
    public function pack(PackageItemInterface $item)
    {
        $key = sprintf('%s_%s', $item->get3PL(), $item->getOwnerUserId());
        if (!isset($this->packages[$key])) {
            $this->packages[$key] = [];
        }
        $this->packages[$key][] = $item;
    }
}
class ThreePLClassifier extends AbstractBuilder
{
    /**
     * {@inheritdoc}
     */
    public function pack(PackageItemInterface $item)
    {
        $key = sprintf('%s_%s', $item->getWarehouse(), $item->get3PL());
        if (!isset($this->packages[$key])) {
            $this->packages[$key] = [];
        }
        $this->packages[$key][] = $item;
    }
}
class WarehouseClassifier extends AbstractBuilder
{
    /**
     * {@inheritdoc}
     */
    public function pack(PackageItemInterface $item)
    {
        if (!isset($this->packages[$item->getWarehouse()])) {
            $this->packages[$item->getWarehouse()] = [];
        }
        $this->packages[$item->getWarehouse()][] = $item;
    }
}

from design-patterns-for-humans.

kamranahmedse avatar kamranahmedse commented on May 21, 2024

No it is fine. The example is not the exact implementation. To keep it simple, I did not implement the pay method in each of the classes and resorted to the one in the base class by just showing the class that was used for payment

echo sprintf('Paid %s using %s' . PHP_EOL, $amountToPay, get_called_class());

In real world, I could have overridden the pay method in each of the sub-classes and implemented it according to the payment provider.

from design-patterns-for-humans.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.