made some refactoring
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package: patterns
|
||||
* @parcel: patterns
|
||||
* @author: Yevhen Odynets
|
||||
* @date: 2025-07-03
|
||||
* @time: 20:07
|
||||
@@ -19,7 +19,7 @@ interface AbstractFactoryInterface
|
||||
public function createDeliveryService(): DeliveryServiceInterface;
|
||||
|
||||
/**
|
||||
* @return PackageInterface
|
||||
* @return ParcelInterface
|
||||
*/
|
||||
public function createPackage(): PackageInterface;
|
||||
public function createParcel(): ParcelInterface;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @parcel: patterns
|
||||
* @author: Yevhen Odynets
|
||||
* @date: 2025-07-08
|
||||
* @time: 10:59
|
||||
*/
|
||||
|
||||
declare(strict_types = 1);
|
||||
|
||||
namespace Pattern\Creational\AbstractFactory;
|
||||
|
||||
class DeliveryServiceFactory
|
||||
{
|
||||
public static function create(ProvidersEnum $provider): AbstractFactoryInterface
|
||||
{
|
||||
return $provider->getService();
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package: patterns
|
||||
* @parcel: patterns
|
||||
* @author: Yevhen Odynets
|
||||
* @date: 2025-07-03
|
||||
* @time: 20:02
|
||||
@@ -13,5 +13,5 @@ namespace Pattern\Creational\AbstractFactory;
|
||||
|
||||
interface DeliveryServiceInterface
|
||||
{
|
||||
public function sendPackage(PackageInterface $package): void;
|
||||
public function sendParcel(ParcelInterface $parcel): bool;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package: patterns
|
||||
* @parcel: patterns
|
||||
* @author: Yevhen Odynets
|
||||
* @date: 2025-07-03
|
||||
* @time: 20:15
|
||||
@@ -18,8 +18,8 @@ class JustinDeliveryFactory implements AbstractFactoryInterface
|
||||
return new JustinDeliveryService();
|
||||
}
|
||||
|
||||
public function createPackage(): PackageInterface
|
||||
public function createParcel(): ParcelInterface
|
||||
{
|
||||
return new JustinPackage();
|
||||
return new JustinParcel();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package: patterns
|
||||
* @parcel: patterns
|
||||
* @author: Yevhen Odynets
|
||||
* @date: 2025-07-03
|
||||
* @time: 19:57
|
||||
@@ -13,9 +13,11 @@ namespace Pattern\Creational\AbstractFactory;
|
||||
|
||||
class JustinDeliveryService implements DeliveryServiceInterface
|
||||
{
|
||||
public function sendPackage(PackageInterface $package): void
|
||||
public function sendParcel(ParcelInterface $parcel): bool
|
||||
{
|
||||
/** @noinspection ForgottenDebugOutputInspection */
|
||||
dump("Sending package via Justin...");
|
||||
dump("Sending parcel via Justin...");
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package: patterns
|
||||
* @parcel: patterns
|
||||
* @author: Yevhen Odynets
|
||||
* @date: 2025-07-03
|
||||
* @time: 20:00
|
||||
@@ -11,11 +11,18 @@ declare(strict_types = 1);
|
||||
|
||||
namespace Pattern\Creational\AbstractFactory;
|
||||
|
||||
class MeestPackage implements PackageInterface
|
||||
class JustinParcel implements ParcelInterface
|
||||
{
|
||||
public function getConsist(): void
|
||||
{
|
||||
/** @noinspection ForgottenDebugOutputInspection */
|
||||
dump('Checking package from Meest...');
|
||||
dump('Checking parcel from Justin...');
|
||||
}
|
||||
|
||||
public function setDestination(string $destination): static
|
||||
{
|
||||
echo "<pre>$destination</pre></<br>>";
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package: patterns
|
||||
* @parcel: patterns
|
||||
* @author: Yevhen Odynets
|
||||
* @date: 2025-07-03
|
||||
* @time: 20:15
|
||||
@@ -18,8 +18,8 @@ class MeestDeliveryFactory implements AbstractFactoryInterface
|
||||
return new MeestDeliveryService();
|
||||
}
|
||||
|
||||
public function createPackage(): PackageInterface
|
||||
public function createParcel(): ParcelInterface
|
||||
{
|
||||
return new MeestPackage();
|
||||
return new MeestParcel();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package: patterns
|
||||
* @parcel: patterns
|
||||
* @author: Yevhen Odynets
|
||||
* @date: 2025-07-03
|
||||
* @time: 19:57
|
||||
@@ -14,9 +14,11 @@ namespace Pattern\Creational\AbstractFactory;
|
||||
|
||||
class MeestDeliveryService implements DeliveryServiceInterface
|
||||
{
|
||||
public function sendPackage(PackageInterface $package): void
|
||||
public function sendParcel(ParcelInterface $parcel): bool
|
||||
{
|
||||
/** @noinspection ForgottenDebugOutputInspection */
|
||||
dump("Sending package via Meest...");
|
||||
dump("Sending parcel via Meest...");
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package: patterns
|
||||
* @parcel: patterns
|
||||
* @author: Yevhen Odynets
|
||||
* @date: 2025-07-03
|
||||
* @time: 20:00
|
||||
@@ -11,11 +11,18 @@ declare(strict_types = 1);
|
||||
|
||||
namespace Pattern\Creational\AbstractFactory;
|
||||
|
||||
class JustinPackage implements PackageInterface
|
||||
class MeestParcel implements ParcelInterface
|
||||
{
|
||||
public function getConsist(): void
|
||||
{
|
||||
/** @noinspection ForgottenDebugOutputInspection */
|
||||
dump('Checking package from Justin...');
|
||||
dump('Checking parcel from Meest...');
|
||||
}
|
||||
|
||||
public function setDestination(string $destination): static
|
||||
{
|
||||
echo "<pre>$destination</pre></<br>";
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package: patterns
|
||||
* @parcel: patterns
|
||||
* @author: Yevhen Odynets
|
||||
* @date: 2025-07-03
|
||||
* @time: 20:15
|
||||
@@ -15,11 +15,11 @@ class NovapostDeliveryFactory implements AbstractFactoryInterface
|
||||
{
|
||||
public function createDeliveryService(): DeliveryServiceInterface
|
||||
{
|
||||
return new NovapostDeliveryService();
|
||||
return new NovapostDeliveryService;
|
||||
}
|
||||
|
||||
public function createPackage(): PackageInterface
|
||||
public function createParcel(): ParcelInterface
|
||||
{
|
||||
return new NovapostPackage();
|
||||
return new NovapostParcel;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package: patterns
|
||||
* @parcel: patterns
|
||||
* @author: Yevhen Odynets
|
||||
* @date: 2025-07-03
|
||||
* @time: 19:57
|
||||
@@ -13,9 +13,11 @@ namespace Pattern\Creational\AbstractFactory;
|
||||
|
||||
class NovapostDeliveryService implements DeliveryServiceInterface
|
||||
{
|
||||
public function sendPackage(PackageInterface $package): void
|
||||
public function sendParcel(ParcelInterface $parcel): true
|
||||
{
|
||||
/** @noinspection ForgottenDebugOutputInspection */
|
||||
dump("Sending package via Novapost...");
|
||||
dump("Sending parcel via Novapost...");
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,21 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package: patterns
|
||||
* @author: Yevhen Odynets
|
||||
* @date: 2025-07-03
|
||||
* @time: 20:00
|
||||
*/
|
||||
|
||||
declare(strict_types = 1);
|
||||
|
||||
namespace Pattern\Creational\AbstractFactory;
|
||||
|
||||
class NovapostPackage implements PackageInterface
|
||||
{
|
||||
public function getConsist(): void
|
||||
{
|
||||
/** @noinspection ForgottenDebugOutputInspection */
|
||||
dump('Checking package from Novapost...');
|
||||
}
|
||||
}
|
||||
33
src/Pattern/Creational/AbstractFactory/NovapostParcel.php
Normal file
33
src/Pattern/Creational/AbstractFactory/NovapostParcel.php
Normal file
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @parcel: patterns
|
||||
* @author: Yevhen Odynets
|
||||
* @date: 2025-07-03
|
||||
* @time: 20:00
|
||||
*/
|
||||
|
||||
declare(strict_types = 1);
|
||||
|
||||
namespace Pattern\Creational\AbstractFactory;
|
||||
|
||||
class NovapostParcel implements ParcelInterface
|
||||
{
|
||||
public function getConsist(): void
|
||||
{
|
||||
/** @noinspection ForgottenDebugOutputInspection */
|
||||
dump('Checking parcel from Novapost...');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $destination
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setDestination(string $destination): static
|
||||
{
|
||||
echo "<pre>$destination</pre></<br>";
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package: patterns
|
||||
* @author: Yevhen Odynets
|
||||
* @date: 2025-07-03
|
||||
* @time: 19:59
|
||||
*/
|
||||
|
||||
declare(strict_types = 1);
|
||||
|
||||
namespace Pattern\Creational\AbstractFactory;
|
||||
|
||||
interface PackageInterface
|
||||
{
|
||||
public function getConsist(): void;
|
||||
}
|
||||
27
src/Pattern/Creational/AbstractFactory/ParcelInterface.php
Normal file
27
src/Pattern/Creational/AbstractFactory/ParcelInterface.php
Normal file
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @parcel: patterns
|
||||
* @author: Yevhen Odynets
|
||||
* @date: 2025-07-03
|
||||
* @time: 19:59
|
||||
*/
|
||||
|
||||
declare(strict_types = 1);
|
||||
|
||||
namespace Pattern\Creational\AbstractFactory;
|
||||
|
||||
interface ParcelInterface
|
||||
{
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function getConsist(): void;
|
||||
|
||||
/**
|
||||
* @param string $destination
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setDestination(string $destination): static;
|
||||
}
|
||||
41
src/Pattern/Creational/AbstractFactory/ParcelSender.php
Normal file
41
src/Pattern/Creational/AbstractFactory/ParcelSender.php
Normal file
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package: patterns
|
||||
* @author: Yevhen Odynets
|
||||
* @date: 2025-07-08
|
||||
* @time: 11:14
|
||||
*/
|
||||
|
||||
declare(strict_types = 1);
|
||||
|
||||
namespace Pattern\Creational\AbstractFactory;
|
||||
|
||||
readonly class ParcelSender
|
||||
{
|
||||
private AbstractFactoryInterface $service;
|
||||
|
||||
public function __construct(
|
||||
private ProvidersEnum $provider,
|
||||
private string $destinationAddress,
|
||||
) {
|
||||
$this->service = DeliveryServiceFactory::create($this->provider);
|
||||
}
|
||||
|
||||
public function send(): bool
|
||||
{
|
||||
// getting the delivery service
|
||||
$deliveryService = $this->service->createDeliveryService();
|
||||
|
||||
// getting the parcel
|
||||
$parcel = $this->service->createParcel();
|
||||
|
||||
// setting up address & checking the parcel
|
||||
$parcel->setDestination($this->destinationAddress)->getConsist();
|
||||
|
||||
// sending the parcel
|
||||
$deliveryService->sendParcel($parcel);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
30
src/Pattern/Creational/AbstractFactory/ProvidersEnum.php
Normal file
30
src/Pattern/Creational/AbstractFactory/ProvidersEnum.php
Normal file
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @parcel: patterns
|
||||
* @author: Yevhen Odynets
|
||||
* @date: 2025-07-08
|
||||
* @time: 10:20
|
||||
*/
|
||||
|
||||
declare(strict_types = 1);
|
||||
|
||||
namespace Pattern\Creational\AbstractFactory;
|
||||
|
||||
enum ProvidersEnum
|
||||
{
|
||||
case NovaPost;
|
||||
case UkrPost;
|
||||
case Meest;
|
||||
case Justin;
|
||||
|
||||
public function getService(): AbstractFactoryInterface
|
||||
{
|
||||
return match ($this) {
|
||||
self::NovaPost => new NovapostDeliveryFactory,
|
||||
self::UkrPost => new UkrpostDeliveryFactory,
|
||||
self::Meest => new MeestDeliveryFactory,
|
||||
self::Justin => new JustinDeliveryFactory,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package: patterns
|
||||
* @parcel: patterns
|
||||
* @author: Yevhen Odynets
|
||||
* @date: 2025-07-03
|
||||
* @time: 20:15
|
||||
@@ -18,8 +18,8 @@ class UkrpostDeliveryFactory implements AbstractFactoryInterface
|
||||
return new UkrpostDeliveryService();
|
||||
}
|
||||
|
||||
public function createPackage(): PackageInterface
|
||||
public function createParcel(): ParcelInterface
|
||||
{
|
||||
return new UkrpostPackage();
|
||||
return new UkrpostParcel();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package: patterns
|
||||
* @parcel: patterns
|
||||
* @author: Yevhen Odynets
|
||||
* @date: 2025-07-03
|
||||
* @time: 19:57
|
||||
@@ -13,9 +13,11 @@ namespace Pattern\Creational\AbstractFactory;
|
||||
|
||||
class UkrpostDeliveryService implements DeliveryServiceInterface
|
||||
{
|
||||
public function sendPackage(PackageInterface $package): void
|
||||
public function sendParcel(ParcelInterface $parcel): bool
|
||||
{
|
||||
/** @noinspection ForgottenDebugOutputInspection */
|
||||
dump("Sending package via Ukrpost...");
|
||||
dump("Sending parcel via Ukrpost...");
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,21 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package: patterns
|
||||
* @author: Yevhen Odynets
|
||||
* @date: 2025-07-03
|
||||
* @time: 20:00
|
||||
*/
|
||||
|
||||
declare(strict_types = 1);
|
||||
|
||||
namespace Pattern\Creational\AbstractFactory;
|
||||
|
||||
class UkrpostPackage implements PackageInterface
|
||||
{
|
||||
public function getConsist(): void
|
||||
{
|
||||
/** @noinspection ForgottenDebugOutputInspection */
|
||||
dump('Checking package from Ukrpost...');
|
||||
}
|
||||
}
|
||||
34
src/Pattern/Creational/AbstractFactory/UkrpostParcel.php
Normal file
34
src/Pattern/Creational/AbstractFactory/UkrpostParcel.php
Normal file
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @parcel: patterns
|
||||
* @author: Yevhen Odynets
|
||||
* @date: 2025-07-03
|
||||
* @time: 20:00
|
||||
*/
|
||||
|
||||
declare(strict_types = 1);
|
||||
|
||||
namespace Pattern\Creational\AbstractFactory;
|
||||
|
||||
class UkrpostParcel implements ParcelInterface
|
||||
{
|
||||
public function getConsist(): void
|
||||
{
|
||||
/** @noinspection ForgottenDebugOutputInspection */
|
||||
dump('Checking parcel from Ukrpost...');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $destination
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setDestination(string $destination): static
|
||||
{
|
||||
echo "<pre>$destination</pre></<br>";
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
22
src/Pattern/Creational/AbstractFactory/info.md
Normal file
22
src/Pattern/Creational/AbstractFactory/info.md
Normal file
@@ -0,0 +1,22 @@
|
||||
🧠 **Переваги цієї архітектури**
|
||||
|
||||
✅ Легко додати нову службу доставки
|
||||
✅ Не потрібно змінювати існуючий код (принцип Open/Closed)
|
||||
✅ Клієнтський код не залежить від реалізацій
|
||||
✅ Можна легко замінювати служби в тестах (Mock)
|
||||
|
||||
|
||||
🧩 **Структура оновленого коду:**
|
||||
DeliveryServiceInterface — інтерфейс
|
||||
|
||||
{Justin|Meest|NovaPost|UkrPost}DeliveryService — реалізація
|
||||
|
||||
|
||||
DeliveryServiceFactory — фабрика
|
||||
|
||||
ParcelSender — клієнтська логіка
|
||||
|
||||
index.php — приклад використання
|
||||
|
||||
|
||||
Надай приклад реалізації патерну Singleton на мові програмування PHP
|
||||
@@ -53,7 +53,7 @@ class Singleton
|
||||
dump(trim($message . ': ' . $cls, ' :'));
|
||||
|
||||
if (!isset(self::$instances[$cls])) {
|
||||
self::$instances[$cls] = new static();
|
||||
self::$instances[$cls] = new static;
|
||||
}
|
||||
|
||||
return self::$instances[$cls];
|
||||
|
||||
Reference in New Issue
Block a user