Comments (2)
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.
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)
- JobPostings to Employment Agency HOT 3
- Translation request HOT 1
- Where has this been all my life! 👌😲👏 HOT 1
- Missing Interface Observer and Observable HOT 1
- Does someone make the java version? HOT 3
- Proxy HOT 2
- Should not use 'Default' as a class name in state pattern, 'class Default implements WritingState' HOT 1
- Didn't see any implementation difference between state and strategy pattern HOT 4
- Add MVC (model-view-controller) pattern HOT 3
- Why not provide a place to give feedback? HOT 1
- adding more coding language support - DISCUSSION
- Pii0327
- Permission for portuguese translation HOT 1
- Russian translate HOT 3
- Traditional Chinese (zh-TW) translation HOT 1
- Observer error HOT 1
- Problem with the adapter example HOT 2
- Shouldn't the strategy pattern have condition checking in the example? HOT 1
- Missing Specification Design Pattern HOT 1
- Adding Architectural patterns - Data Transfer Object Design Pattern HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from design-patterns-for-humans.