PHP Abstract factory design pattern example
<?php
interface Payment {
public function PaymentPayTM(): PayTMInterface;
public function PaymentPayU() : PayUInterface;
}
interface PayTMInterface {
public function Paymentsetup();
}
interface PayUInterface {
public function Paymentsetup();
public function Paymentprocess();
}
class PaymentMethod1 implements Payment
{
public function PaymentPayTM(): PayTMInterface {
return new PayTM();
}
public function PaymentPayU() : PayUInterface{
return new PayU();
}
}
class PaymentMethod2 implements Payment
{
public function PaymentPayTM(): PayTMInterface{
return new PayTM2();
}
public function PaymentPayU() : PayUInterface{
return new PayU2();
}
}
class PayTM implements PayTMInterface {
Public function run(){
echo "payment by paytm";
}
Public function Paymentsetup(){
}
}
class PayU implements PayUInterface {
Public function run(){
echo "payment by PayU";
}Public function Paymentsetup(){
}
Public function Paymentprocess(){
}
}
class PayTM2 implements PayTMInterface {
Public function run(){
echo "payment by paytm amd method2";
}
Public function Paymentsetup(){
}
}
class PayU2 implements PayUInterface {
Public function run(){
echo "payment by PayU amd method2";
}
Public function Paymentsetup(){
}
Public function Paymentprocess(){
}
}
echo (new PaymentMethod1)->PaymentPayTM()->run(); echo "\n";
echo (new PaymentMethod1)->PaymentPayU()->run();echo "\n";
echo (new PaymentMethod2)->PaymentPayTM()->run();echo "\n";
echo (new PaymentMethod2)->PaymentPayU()->run();echo "\n";
PHP Builder design pattern example
<?php
Interface Builder {
public function title(): void;
public function body(): void;
public function header(): void;
public function footer(): void;
}
Class PageBuilder implements Builder {
private $page;
public function __construct()
{
$this->reset();
}
public function reset(): void
{
$this->page = new page();
}
public function title(): void
{
$this->page->parts[] = 'title';
}
public function body(): void // asd
{
$this->page->parts[] = 'body';
}
public function header(): void
{
$this->page->parts[] = 'header';
}
public function footer(): void
{
$this->page->parts[] = 'footer';
}
public function getPage()
{
$CurrentPage = $this->page;
$this->reset();
return $CurrentPage;
}
}
class page
{
public $parts = [];
public function listParts(): void
{
echo "page parts: " . implode(', ', $this->parts) . "\n\n";
}
}
class Editor {
public $builder;
public function setBuilder(Builder $builder)
{
$this->builder = $builder;
}
public function buildTitle()
{
$this->builder->title();
}
public function buildheader()
{
$this->builder->header();
}
public function buildbody()
{
$this->builder->body();
}
public function buildfooter()
{
$this->builder->footer();
}
public function buildFullpage()
{
$this->builder->title();
$this->builder->header();
$this->builder->body();
$this->builder->footer();
}
}
function clientCode(Editor $editor)
{
$builder = new PageBuilder();
$editor->setBuilder($builder);
echo "Standard basic product:\n";
$editor->buildheader();
$builder->getPage()->listParts();
echo "Standard full featured product:\n";
$editor->buildFullpage();
$builder->getPage()->listParts();
// Remember, the Builder pattern can be used without a Director class.
echo "Custom product:\n";
$builder->title();
$builder->header();
$builder->body();
echo $builder->getPage()->listParts();
}
clientCode(new Editor() );
Php Factory design pattern example
<?php
abstract class Producer {
abstract public function factoryMethod(): Entertainment;
public function someOperation(): string
{
// Call the factory method to create a Product object.
$product = $this->factoryMethod();
// Now, use the product.
$result = "The Producer are working on " .
$product->operation();
return $result;
}
}
class movies extends Producer
{
public $name;
public function __construct($name)
{
$this->name = $name;
}
public function factoryMethod(): Entertainment
{
return new movie($this->name);
}
}
class serials extends Producer
{
public $name;
public function __construct($name)
{
$this->name = $name;
}
public function factoryMethod(): Entertainment
{
return new serial( $this->name);
}
}
interface Entertainment
{
public function operation(): string;
}
class movie implements Entertainment {
public $name;
public function __construct($name)
{
$this->name = $name;
}
public function operation(): string{
return $this->name ." movie" ?? "uknown moview";
}
}
class serial implements Entertainment {
public $name;
public function __construct($name)
{
$this->name = $name;
}
public function operation(): string{
return $this->name ." serial" ?? "uknown serial";
}
}
echo (new movies('dabaang'))->someOperation();
echo (new serials('dosti'))->someOperation();
My name is Sam saifi. Last five years I am working as a web developer. Now these days I decided to share my knowledge with everyone. I have experience in much trending technology which I want to share with you guys.