Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Pizza {
- private $tamaño;
- private $masa;
- private $orillaConQueso;
- private $ingredientes;
- private function __construct($builder) {
- $this->tamaño = $builder->tamaño;
- $this->masa = $builder->masa;
- $this->orillaConQueso = $builder->orillaConQueso;
- $this->ingredientes = $builder->ingredientes;
- }
- public static function builder() {
- return new PizzaBuilder();
- }
- public function mostrar() {
- echo "Pizza: $this->tamaño, $this->masa, orilla con queso: " . ($this->orillaConQueso ? "sí" : "no") . ", ingredientes: $this->ingredientes\n";
- }
- // Clase Builder
- class PizzaBuilder {
- public $tamaño;
- public $masa;
- public $orillaConQueso = false;
- public $ingredientes;
- public function tamaño($tamaño) {
- $this->tamaño = $tamaño;
- return $this;
- }
- public function masa($masa) {
- $this->masa = $masa;
- return $this;
- }
- public function orillaConQueso($orillaConQueso) {
- $this->orillaConQueso = $orillaConQueso;
- return $this;
- }
- public function ingredientes($ingredientes) {
- $this->ingredientes = $ingredientes;
- return $this;
- }
- public function build() {
- return new Pizza($this);
- }
- }
- }
- // USO:
- $pizza = Pizza::builder()
- ->tamaño("Mediana")
- ->masa("Gruesa")
- ->orillaConQueso(true)
- ->ingredientes("Jamón, Aceitunas")
- ->build();
- $pizza->mostrar();
- ?>
Advertisement
Add Comment
Please, Sign In to add comment