made some refactoring

This commit is contained in:
2025-07-09 05:28:39 +03:00
parent f76eb08fe0
commit 38676bc9fd
69 changed files with 3075 additions and 355 deletions

View File

@@ -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;
}

View File

@@ -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();
}
}

View File

@@ -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;
}

View File

@@ -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();
}
}

View File

@@ -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;
}
}

View File

@@ -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;
}
}

View File

@@ -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();
}
}

View File

@@ -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;
}
}

View File

@@ -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;
}
}

View File

@@ -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;
}
}

View File

@@ -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;
}
}

View File

@@ -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...');
}
}

View 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;
}
}

View File

@@ -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;
}

View 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;
}

View 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;
}
}

View 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,
};
}
}

View File

@@ -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();
}
}

View File

@@ -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;
}
}

View File

@@ -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...');
}
}

View 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;
}
}

View File

@@ -0,0 +1,22 @@
🧠 **Переваги цієї архітектури**
✅ Легко додати нову службу доставки
Не потрібно змінювати існуючий код (принцип Open/Closed)
✅ Клієнтський код не залежить від реалізацій
✅ Можна легко замінювати служби в тестах (Mock)
🧩 **Структура оновленого коду:**
DeliveryServiceInterface — інтерфейс
{Justin|Meest|NovaPost|UkrPost}DeliveryService — реалізація
DeliveryServiceFactory — фабрика
ParcelSender — клієнтська логіка
index.php — приклад використання
Надай приклад реалізації патерну Singleton на мові програмування PHP