Last active
September 10, 2023 21:31
-
-
Save firefoxrebo/f78ab3c352b77279c94e208570eaf187 to your computer and use it in GitHub Desktop.
Revisions
-
firefoxrebo revised this gist
Mar 29, 2017 . 1 changed file with 158 additions and 84 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,175 +1,249 @@ <?php // Builder Design Pattern // Product class SmartPhone { public $cpu; public $gpu; public $ram; public $sensors; public $camera; public $case; } // Concrete Products class SamsungPhone extends SmartPhone { public function __toString() { return '<h1>Your Samsung Phone is Ready with the following Specs.</h1>' . '<pre>' . var_export($this, true) . '</pre>'; } } class HTCPhone extends SmartPhone { public function __toString() { return '<h1>Your HTC Phone is Ready with the following Specs.</h1>' . '<pre>' . var_export($this, true) . '</pre>'; } } class ApplePhone extends SmartPhone { public function __toString() { return '<h1>Your Apple Phone is Ready with the following Specs.</h1>' . '<pre>' . var_export($this, true) . '</pre>'; } } // Builder Interface interface SmartPhoneBuilder { public function addCPU(); public function addGPU(); public function addRAM(); public function addSensors(); public function addCamera(); public function addCase(); public function getProduct(); } // Concrete Builders class SamsungPhoneBuilder implements SmartPhoneBuilder { private $product; private $options; public function __construct(array $options) { $this->options = $options; $this->product = new SamsungPhone(); } public function addCPU() { $this->product->cpu = $this->options['cpu']; } public function addGPU() { $this->product->gpu = $this->options['gpu']; } public function addRAM() { $this->product->ram = $this->options['ram']; } public function addSensors() { $this->product->sensors = $this->options['sensors']; } public function addCamera() { $this->product->camera = $this->options['camera']; } public function addCase() { $this->product->case = $this->options['case']; } public function getProduct() { return $this->product; } } class HTCPhoneBuilder implements SmartPhoneBuilder { private $product; private $options; public function __construct(array $options) { $this->options = $options; $this->product = new HTCPhone(); } public function addCPU() { $this->product->cpu = $this->options['cpu']; } public function addGPU() { $this->product->gpu = $this->options['gpu']; } public function addRAM() { $this->product->ram = $this->options['ram']; } public function addSensors() { $this->product->sensors = $this->options['sensors']; } public function addCamera() { $this->product->camera = $this->options['camera']; } public function addCase() { $this->product->case = $this->options['case']; } public function getProduct() { return $this->product; } } class ApplePhoneBuilder implements SmartPhoneBuilder { private $product; private $options; public function __construct(array $options) { $this->options = $options; $this->product = new ApplePhone(); } public function addCPU() { $this->product->cpu = $this->options['cpu']; } public function addGPU() { $this->product->gpu = $this->options['gpu']; } public function addRAM() { $this->product->ram = $this->options['ram']; } public function addSensors() { $this->product->sensors = $this->options['sensors']; } public function addCamera() { $this->product->camera = $this->options['camera']; } public function addCase() { $this->product->case = $this->options['case']; } public function getProduct() { return $this->product; } } // Director class SmartPhoneCreator { public function buildPhone(SmartPhoneBuilder $builder) { $builder->addCPU(); $builder->addGPU(); $builder->addRAM(); $builder->addSensors(); $builder->addCamera(); $builder->addCase(); return $builder->getProduct(); } } $creator = new SmartPhoneCreator(); $samsungBuilder = new SamsungPhoneBuilder([ 'cpu' => 'Snapdragon 820', 'gpu' => 'Adreno 530', 'ram' => 4, 'sensors' => ['UV', 'Barometer', 'Altimeter', 'GPS', 'GLONASS'], 'camera' => 12, 'case' => 'Silver Metalic Case' ]); $appleBuilder = new ApplePhoneBuilder([ 'cpu' => 'A10', 'gpu' => 'Apple', 'ram' => 3, 'sensors' => ['UV', 'Barometer', 'Altimeter', 'GPS', 'GLONASS'], 'camera' => 12, 'case' => 'Black Matt Case' ]); $htcBuilder = new HTCPhoneBuilder([ 'cpu' => 'Snapdragon 810', 'gpu' => 'Adreno 520', 'ram' => 4, 'sensors' => ['UV', 'Barometer', 'Altimeter', 'GPS'], 'camera' => 8, 'case' => 'Blue Matt Case' ]); $note7 = $creator->buildPhone($samsungBuilder); $iphone7Plus = $creator->buildPhone($appleBuilder); $htc = $creator->buildPhone($htcBuilder); echo $note7; echo $iphone7Plus; echo $htc; -
firefoxrebo renamed this gist
Mar 29, 2017 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
firefoxrebo created this gist
Mar 29, 2017 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,175 @@ <?php // Explaining the Builder Design Pattern // The Director class SmartPhoneDirector { public function buildMobile(MobileBuilder $builder, array $options) { $builder->addCPU($options['cpu']); $builder->addRam($options['ram']); $builder->addWrapper($options['wrapper']); $builder->addGPU($options['gpu']); $builder->addSensors($options['sensors']); $builder->addCamera($options['camera']); return $builder->getPhone(); } } // The Builder Interface interface MobileBuilder { public function addCPU($cpu); public function addRam($ram); public function addWrapper($wrapper); public function addGPU($gpu); public function addSensors($sensors); public function addCamera($camera); public function getPhone(); } // Concrete Builder class SamsungPhoneBuilder implements MobileBuilder { public $product; public function __construct() { $this->product = new SamsungPhone(); } public function addCPU($cpu) { $this->product->cpu = $cpu; } public function addRam($ram) { $this->product->ram = $ram; } public function addWrapper($wrapper) { $this->product->wrapper = $wrapper; } public function addGPU($gpu) { $this->product->gpu = $gpu; } public function addCamera($camera) { $this->product->camera = $camera; } public function addSensors($sensors) { $this->product->sensors = $sensors; } public function getPhone() { return $this->product; } } // Concrete Builder class ApplePhoneBuilder implements MobileBuilder { public $product; public function __construct() { $this->product = new iPhonePhone(); } public function addCPU($cpu) { $this->product->cpu = $cpu; } public function addRam($ram) { $this->product->ram = $ram; } public function addWrapper($wrapper) { $this->product->wrapper = $wrapper; } public function addGPU($gpu) { $this->product->gpu = $gpu; } public function addCamera($camera) { $this->product->camera = $camera; } public function addSensors($sensors) { $this->product->sensors = $sensors; } public function getPhone() { return $this->product; } } class SmartPhone { public $cpu; public $ram; public $sensors; public $gpu; public $wrapper; public $camera; } class SamsungPhone extends SmartPhone { public function __toString() { return '<p>Your Samsung Mobile is Ready: </p><pre>' . var_export($this, true) . '</pre>'; } } class iPhonePhone extends SmartPhone { public function __toString() { return '<p>Your iPhone Mobile is Ready: </p><pre>' . var_export($this, true) . '</pre>'; } } $samsungBuilder = new SamsungPhoneBuilder(); $iphoneBuilder = new ApplePhoneBuilder(); $director = new SmartPhoneDirector(); $note3 = $director->buildMobile($samsungBuilder, [ 'cpu' => 'Snapdragon 820', 'gpu' => 'Adreno 530', 'sensors' => ['UV', 'GPS', 'Barometer', 'Altimeter', 'GLONASS'], 'ram' => 4, 'camera' => 12, 'wrapper' => 'Silver Case' ]); $iPhone = $director->buildMobile($iphoneBuilder, [ 'cpu' => 'A10', 'gpu' => 'Apple GPU', 'sensors' => ['UV', 'GPS', 'Barometer', 'Altimeter', 'GLONASS'], 'ram' => 3, 'camera' => 12, 'wrapper' => 'Black Case' ]); echo $note3; echo $iPhone;