Created
June 29, 2022 17:49
-
-
Save atakde/95f0a6c1e40793f3f0953ea9fdfebceb to your computer and use it in GitHub Desktop.
Prototype Design Pattern PHP Example
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 | |
| abstract class MealPrototype | |
| { | |
| protected $mealName; | |
| protected $mealCategory; | |
| public function getMealName(): string | |
| { | |
| return $this->mealName; | |
| } | |
| public function setMealName(string $name): string | |
| { | |
| return $this->mealName = $name; | |
| } | |
| } | |
| class VegetableMealPrototype extends MealPrototype | |
| { | |
| protected $mealCategory = "vegetable"; | |
| /** | |
| * Clone.. | |
| */ | |
| public function __clone() | |
| { | |
| } | |
| } | |
| class MeatMealPrototype extends MealPrototype | |
| { | |
| protected $mealCategory = "meat"; | |
| /** | |
| * Clone.. | |
| */ | |
| public function __clone() | |
| { | |
| } | |
| } | |
| $meatMealPrototype = new MeatMealPrototype(); | |
| $vegetableMealPrototype = new VegetableMealPrototype(); | |
| for ($i = 0; $i < 5; $i++) { | |
| $object = clone $meatMealPrototype; | |
| $object->setMealName('Meal Menu #' . $i); | |
| echo $object->getMealName() . PHP_EOL; | |
| } | |
| for ($i = 0; $i < 5; $i++) { | |
| $object = clone $vegetableMealPrototype; | |
| $object->setMealName('Vegetable Menu #' . $i); | |
| echo $object->getMealName() . PHP_EOL; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment