Pages

Search This Blog

Friday 12 June 2015

Implementation Of Factory Design Pattern in PHP (Practical Example)



<?php

/*
 *
 * @Author: Zuhair Mirza
 * @Implementation Of Factory Design Pattern in PHP (Practical Example)
 * @Date : 12-June-2015
 * @Defination  :  * One of the most commonly used design patterns is the factory pattern. In this pattern,
 * a class simply creates the object you want to use.
 *
 */

class Automobile
{
    private $vehicleMake;
    private $vehicleModel;

    public function __construct($make, $model)
    {
        $this->vehicleMake = $make;
        $this->vehicleModel = $model;
    }

    public function getMakeAndModel()
    {
        return $this->vehicleMake . ' ' . $this->vehicleModel;
    }
}

class AutomobileFactory
{
    public static function create($make, $model)
    {
        return new Automobile($make, $model);
    }
}

// Automobile Factory create the Automobile object

$veyron = AutomobileFactory::create('Mercedes ', 'Sedans');

print_r($veyron->getMakeAndModel()); // Output :  "Mercedes Sedans"

No comments:

Post a Comment