Created
November 22, 2017 06:43
-
-
Save lastday154/949f68dfdf4fd731505a7536ed0f571b to your computer and use it in GitHub Desktop.
Factory Pattern
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| 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); | |
| } | |
| } | |
| // have the factory create the Automobile object | |
| $veyron = AutomobileFactory::create('Bugatti', 'Veyron'); | |
| print_r($veyron->getMakeAndModel()); // outputs "Bugatti Veyron" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment