Design Patterns in OOP PHP Abstract Factory
Abstract Factory design pattern
Abstract Factory
create series of related dependent objects without specifying its blueprint classes usually all the created classes implements the same interface
Example
Imagine we want to calculate price of different types of cars each car has it's own price formula we will use Abstract Factory Pattern this will be include as following:
- interface car which contains the shared methods among all cars type.
- carType1 class that implements the car interface and have its own private data ,constructor and implements the shared function
- carType2 class that implements the car interface and have its own private data ,constructor and implements the shared function
- abstract Factory Class in this class we have private members that exist in the two classes carType1 , carType2 and if there is members that don't exist in all other classes we but a default value for it so we don't get an error
in terminal run
$ composer init
enter add needed info
then
$ composer require phpunit/phpunit 7.3
composer .json file
{
"name": "lovel/desgin-pattern",
"description": "code example for design pattern in php",
"authors": [
{
"name": "yourNAme",
"email": "youeEmail@gmail.com"
}
],
"require": {
"phpunit/phpunit": "^7.3"
},
"autoload":{
"psr-4": {
"Creational\\":"Creational"
}
}
}
create namespaces as follow design pattern category / design pattern type
interface car
<?php
namespace Creational\AbstractFactory;
interface CarInterface
{
public function calculatePrice();
}
?>
CarType1 Class
<?php
namespace Creational\AbstractFactory;
class BmWCar implements CarInterface
{
private $price;
public function __construct($price){
$this->price =$price;
}
public function calculatePrice(){
return $this->price + 120000;
}
}
?>
CarType2 Class
<?php
namespace Creational\AbstractFactory;
class BenzCar implements CarInterface
{
//same price as in BmWCar
private $price;
//additional member tax
private $tax;
public function __construct($price,$tax){
$this->price =$price;
$this->tax =$tax;
}
public function calculatePrice(){
return $this->price + $this->tax +200000;
}
}
?>
abstract Factory Class
namespace Creational\AbstractFactory;
class CarAbstractFactory
{
//defualt Value for additional nember
private $tax=10000;
private $price;
public function __construct($price){
$this->price =$price;
}
public function CreateBmWCar() : BmWCar{
return new BmWCar($this->price);
}
public function CreateBenzCar() :BenzCar{
return new BenzCar($this->price,$this->tax);
}
}
?>
Testing our design pattern using phpunit
test case functions must start with test
create two test each test function use CarAbstractFactory()
and pass parameter to it to create instance of the CarAbstractFactory
and then check the if we can call the appropriate Class using CarAbstractFactory
object
<?php
namespace Tests;
use PHPUnit\Framework\TestCase;
use Creational\AbstractFactory\CarAbstractFactory;
use Creational\AbstractFactory\BmWCar;
use Creational\AbstractFactory\BenzCar;
class AbstractClassTests extends TestCase
{
public function testCanCreateBmWCar(){
//here we create abstract class instance and pass the price
$carFactory = new CarAbstractFactory(205555);
//here we call the create bmw car function
$BmWCarToBe = $carFactory->createBmWCar();
$this->assertInstanceOf(BmWCar::Class,$BmWCarToBe);
}
public function testCanCreateBenzCar(){
//here we create abstract class instance and pass the price
$carFactory = new CarAbstractFactory(205555,2580);
//here we call the create bmw car function
$BmWCarToBe = $carFactory->CreateBenzCar();
$this->assertInstanceOf(BenzCar::Class,$BmWCarToBe);
}
}
?>
in terminal run
$./vendor/bin/phpunit tests/AbstractClassTests
result
PHPUnit 7.5.20 by Sebastian Bergmann and contributors.Configuration: C:\xampp7\htdocs\desginPattern\phpunit.xml
.. 2 / 2 (100%)
OK (2 tests, 2 assertions)
Runtime: PHP 7.4.28 Time: 54 ms, Memory: 4.00 MB
Source
https://www.youtube.com/watch?v=Z7U9RJyEBR4&list=PLdYYj2XLw5BnpInmR103TyVwFd_CLI6IS&index=2
Comments
Post a Comment