Abstract Factory

This commit is contained in:
Yevhen Odynets 2025-07-03 21:15:26 +03:00
parent 0f4618edf5
commit 7fe2402ff3
19 changed files with 376 additions and 115 deletions

113
.gitignore vendored
View File

@ -28,19 +28,6 @@
.idea/**/gradle.xml
.idea/**/libraries
# Gradle and Maven with auto-import
# When using Gradle or Maven with auto-import, you should exclude module files,
# since they will be recreated, and may cause churn. Uncomment if using
# auto-import.
# .idea/artifacts
# .idea/compiler.xml
# .idea/jarRepositories.xml
# .idea/modules.xml
# .idea/*.iml
# .idea/modules
# *.iml
# *.ipr
# CMake
cmake-build-*/
@ -77,104 +64,8 @@ fabric.properties
# Android studio 3.1+ serialized cache file
.idea/caches/build_file_checksums.ser
### PhpStorm+iml template
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
# User-specific stuff
# AWS User-specific
# Generated files
# Sensitive or high-churn files
# Gradle
# Gradle and Maven with auto-import
# When using Gradle or Maven with auto-import, you should exclude module files,
# since they will be recreated, and may cause churn. Uncomment if using
# auto-import.
# .idea/artifacts
# .idea/compiler.xml
# .idea/jarRepositories.xml
# .idea/modules.xml
# .idea/*.iml
# .idea/modules
# *.iml
# *.ipr
# CMake
# Mongo Explorer plugin
# File-based project format
# IntelliJ
# mpeltonen/sbt-idea plugin
# JIRA plugin
# Cursive Clojure plugin
# SonarLint plugin
# Crashlytics plugin (for Android Studio and IntelliJ)
# Editor-based Rest Client
# Android studio 3.1+ serialized cache file
### PhpStorm template
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
# User-specific stuff
# AWS User-specific
# Generated files
# Sensitive or high-churn files
# Gradle
# Gradle and Maven with auto-import
# When using Gradle or Maven with auto-import, you should exclude module files,
# since they will be recreated, and may cause churn. Uncomment if using
# auto-import.
# .idea/artifacts
# .idea/compiler.xml
# .idea/jarRepositories.xml
# .idea/modules.xml
# .idea/*.iml
# .idea/modules
# *.iml
# *.ipr
# CMake
# Mongo Explorer plugin
# File-based project format
# IntelliJ
# mpeltonen/sbt-idea plugin
# JIRA plugin
# Cursive Clojure plugin
# SonarLint plugin
# Crashlytics plugin (for Android Studio and IntelliJ)
# Editor-based Rest Client
# Android studio 3.1+ serialized cache file
### This project
/vendor/
/storage/**/*.zip
/storage/logs/**/*.*
.php-cs-fixer.cache

File diff suppressed because one or more lines are too long

42
code/abstract_factory.php Normal file
View File

@ -0,0 +1,42 @@
<?php
/**
* @package: patterns
* @author: Yevhen Odynets
* @date: 2025-07-03
* @time: 20:20
*/
declare(strict_types = 1);
use Pattern\Creational\AbstractFactory\{NovapostDeliveryFactory, UkrpostDeliveryFactory};
use Pattern\Creational\AbstractFactory\{JustinDeliveryFactory, MeestDeliveryFactory};
/**
* @param array $factories
*
* @return void
*/
function delivery(array $factories): void
{
foreach ($factories as $factory) {
// getting the delivery service
$deliveryService = $factory->createDeliveryService();
// getting the parcel
$package = $factory->createPackage();
// checking the parcel
$package->getConsist();
// sending the parcel
$deliveryService->sendPackage($package);
echo '<hr/>' . PHP_EOL;
}
}
$factories = [
new MeestDeliveryFactory(),
new NovapostDeliveryFactory(),
new JustinDeliveryFactory(),
new UkrpostDeliveryFactory(),
];
delivery($factories);

View File

@ -48,12 +48,13 @@ require '../src/helpers.php';
</head>
<body>
<main>
<?php
<?php
/** Creational patterns **/
// require '../code/singleton.php';
require '../code/factory_method.php';
?>
// require '../code/factory_method.php';
require '../code/abstract_factory.php';
?>
</main>
<script>hljs.highlightAll()</script>
</body>

View File

@ -0,0 +1,25 @@
<?php
/**
* @package: patterns
* @author: Yevhen Odynets
* @date: 2025-07-03
* @time: 20:07
*/
declare(strict_types = 1);
namespace Pattern\Creational\AbstractFactory;
interface AbstractFactoryInterface
{
/**
* @return DeliveryServiceInterface
*/
public function createDeliveryService(): DeliveryServiceInterface;
/**
* @return PackageInterface
*/
public function createPackage(): PackageInterface;
}

View File

@ -0,0 +1,17 @@
<?php
/**
* @package: patterns
* @author: Yevhen Odynets
* @date: 2025-07-03
* @time: 20:02
*/
declare(strict_types = 1);
namespace Pattern\Creational\AbstractFactory;
interface DeliveryServiceInterface
{
public function sendPackage(PackageInterface $package): void;
}

View File

@ -0,0 +1,25 @@
<?php
/**
* @package: patterns
* @author: Yevhen Odynets
* @date: 2025-07-03
* @time: 20:15
*/
declare(strict_types = 1);
namespace Pattern\Creational\AbstractFactory;
class JustinDeliveryFactory implements AbstractFactoryInterface
{
public function createDeliveryService(): DeliveryServiceInterface
{
return new JustinDeliveryService();
}
public function createPackage(): PackageInterface
{
return new JustinPackage();
}
}

View File

@ -0,0 +1,21 @@
<?php
/**
* @package: patterns
* @author: Yevhen Odynets
* @date: 2025-07-03
* @time: 19:57
*/
declare(strict_types = 1);
namespace Pattern\Creational\AbstractFactory;
class JustinDeliveryService implements DeliveryServiceInterface
{
public function sendPackage(PackageInterface $package): void
{
/** @noinspection ForgottenDebugOutputInspection */
dump("Sending package via Justin...");
}
}

View File

@ -0,0 +1,21 @@
<?php
/**
* @package: patterns
* @author: Yevhen Odynets
* @date: 2025-07-03
* @time: 20:00
*/
declare(strict_types = 1);
namespace Pattern\Creational\AbstractFactory;
class JustinPackage implements PackageInterface
{
public function getConsist(): void
{
/** @noinspection ForgottenDebugOutputInspection */
dump('Checking package from Justin...');
}
}

View File

@ -0,0 +1,25 @@
<?php
/**
* @package: patterns
* @author: Yevhen Odynets
* @date: 2025-07-03
* @time: 20:15
*/
declare(strict_types = 1);
namespace Pattern\Creational\AbstractFactory;
class MeestDeliveryFactory implements AbstractFactoryInterface
{
public function createDeliveryService(): DeliveryServiceInterface
{
return new MeestDeliveryService();
}
public function createPackage(): PackageInterface
{
return new MeestPackage();
}
}

View File

@ -0,0 +1,22 @@
<?php
/**
* @package: patterns
* @author: Yevhen Odynets
* @date: 2025-07-03
* @time: 19:57
*/
//phpcs:ignore
declare(strict_types = 1);
namespace Pattern\Creational\AbstractFactory;
class MeestDeliveryService implements DeliveryServiceInterface
{
public function sendPackage(PackageInterface $package): void
{
/** @noinspection ForgottenDebugOutputInspection */
dump("Sending package via Meest...");
}
}

View File

@ -0,0 +1,21 @@
<?php
/**
* @package: patterns
* @author: Yevhen Odynets
* @date: 2025-07-03
* @time: 20:00
*/
declare(strict_types = 1);
namespace Pattern\Creational\AbstractFactory;
class MeestPackage implements PackageInterface
{
public function getConsist(): void
{
/** @noinspection ForgottenDebugOutputInspection */
dump('Checking package from Meest...');
}
}

View File

@ -0,0 +1,25 @@
<?php
/**
* @package: patterns
* @author: Yevhen Odynets
* @date: 2025-07-03
* @time: 20:15
*/
declare(strict_types = 1);
namespace Pattern\Creational\AbstractFactory;
class NovapostDeliveryFactory implements AbstractFactoryInterface
{
public function createDeliveryService(): DeliveryServiceInterface
{
return new NovapostDeliveryService();
}
public function createPackage(): PackageInterface
{
return new NovapostPackage();
}
}

View File

@ -0,0 +1,21 @@
<?php
/**
* @package: patterns
* @author: Yevhen Odynets
* @date: 2025-07-03
* @time: 19:57
*/
declare(strict_types = 1);
namespace Pattern\Creational\AbstractFactory;
class NovapostDeliveryService implements DeliveryServiceInterface
{
public function sendPackage(PackageInterface $package): void
{
/** @noinspection ForgottenDebugOutputInspection */
dump("Sending package via Novapost...");
}
}

View File

@ -0,0 +1,21 @@
<?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,17 @@
<?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,25 @@
<?php
/**
* @package: patterns
* @author: Yevhen Odynets
* @date: 2025-07-03
* @time: 20:15
*/
declare(strict_types = 1);
namespace Pattern\Creational\AbstractFactory;
class UkrpostDeliveryFactory implements AbstractFactoryInterface
{
public function createDeliveryService(): DeliveryServiceInterface
{
return new UkrpostDeliveryService();
}
public function createPackage(): PackageInterface
{
return new UkrpostPackage();
}
}

View File

@ -0,0 +1,21 @@
<?php
/**
* @package: patterns
* @author: Yevhen Odynets
* @date: 2025-07-03
* @time: 19:57
*/
declare(strict_types = 1);
namespace Pattern\Creational\AbstractFactory;
class UkrpostDeliveryService implements DeliveryServiceInterface
{
public function sendPackage(PackageInterface $package): void
{
/** @noinspection ForgottenDebugOutputInspection */
dump("Sending package via Ukrpost...");
}
}

View File

@ -0,0 +1,21 @@
<?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...');
}
}