Last active
August 4, 2021 21:02
-
-
Save elishaukpong/e7180bce112ae0b63bcbd2b5f8c27d4f to your computer and use it in GitHub Desktop.
After learning about the simple factory pattern, i made up a problem scope and used the pattern to solve it.
This file contains 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 | |
//a boy has two shows and can decide to wear anyone of it at any time, | |
//the below class shows the implementation without applying any design patterns | |
class ShoeWithoutFactory | |
{ | |
const CHURCH = 1; | |
const SCHOOL = 2; | |
public function putOn($flag) | |
{ | |
switch($flag) | |
{ | |
case self::CHURCH: | |
echo "polish shoes, put shoes on, and tie show lace \n"; | |
break; | |
default: | |
echo " clean the straps, wear socks and hit the ground to see it sits well on the sole \n"; | |
break; | |
} | |
} | |
public function pullOff($flag) | |
{ | |
switch($flag) | |
{ | |
case self::CHURCH: | |
echo "Kick the devil out of the shoes 😀, keep it aside \n"; | |
break; | |
default: | |
echo "put the show aside and be good \n"; | |
break; | |
} | |
} | |
} | |
//using the simple factory design pattern, we clean up the above code and make it clean and maintainable | |
//though it is worthy to mention that the simple factory pattern breaks the Open for extension and closed | |
//for Modification rule of the SOLID principles, but we can live with that. | |
abstract class Shoe | |
{ | |
const CHURCH = 1; | |
const SCHOOL = 2; | |
public static function make($flag) | |
{ | |
switch($flag) | |
{ | |
case self::CHURCH: | |
return new ChurchShoe(); | |
break; | |
default: | |
return new SchoolShoe(); | |
break; | |
} | |
} | |
public function putOn() | |
{ | |
echo 'putting on "' . __CLASS__ . '" shoes'; | |
} | |
public function pullOff() | |
{ | |
echo 'pulling off "' . __CLASS__ . '" shoes'; | |
} | |
} | |
class ChurchShoe extends Shoe | |
{ | |
public function putOn() | |
{ | |
echo "polish shoes, put shoes on, and tie show lace \n"; | |
} | |
public function pullOff() | |
{ | |
echo "Kick the devil out of the shoes 😀, keep it aside \n"; | |
} | |
} | |
class SchoolShoe extends Shoe | |
{ | |
public function putOn() | |
{ | |
echo " clean the straps, wear socks and hit the ground to see it sits well on the sole \n"; | |
} | |
public function pullOff() | |
{ | |
echo "put the show aside and be good \n"; | |
} | |
} | |
class Dressup | |
{ | |
private $shoe; | |
public function __construct(Shoe $shoe) | |
{ | |
$this->shoe = $shoe; | |
} | |
public function dripUp() | |
{ | |
//wear shirt | |
//wear pants | |
$this->shoe->putOn(); | |
} | |
public function dripDown() | |
{ | |
//remove shirt | |
//remove pants | |
$this->shoe->pullOff(); | |
} | |
} | |
$alfredToSchool = new Dressup(Shoe::make(Shoe::SCHOOL)); | |
$alfredToChurch = new Dressup(Shoe::make(Shoe::CHURCH)); | |
$alfredToSchool->dripUp(); | |
$alfredToSchool->dripDown(); | |
echo "\n \n ---BREAK--- \n\n"; | |
$alfredToChurch->dripUp(); | |
$alfredToChurch->dripDown(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment