lundi 18 décembre 2017

PHP MVC factory pattern, what goes where?

If I were to split this up into MVC, which parts would be in the model, the controller, and the view?

<?php
  interface Description {

  public function describe();

}

class SmallPizza implements Description{

  public function describe(){
    echo "Small pizza is 10 inches" . '<br>';   
  }
}

class MediumPizza implements Description{

  public function describe(){
    echo "Medium pizza is 12 inches" . '<br>';  
  }
}

class LargePizza implements Description{

  public function describe(){
    echo "Large pizza is 14 inches" . '<br>';   
  }
}

class SmSoda implements Description{

  public function describe(){
    echo "Small Soda is 6oz." . '<br>'; 
  }
}

class MdSoda implements Description{

  public function describe(){
    echo "Medium Soda is 8oz." . '<br>';    
  }
}

class LgSoda implements Description{

  public function describe(){
    echo "Large soda is 12 oz." . '<br>';   
  }
}

class DescriptionFactory{

  public function create($type){

    if ($type == "SmallPizza"){
        return new SmallPizza;
    }
    if ($type == "MediumPizza"){
        return new MediumPizza;
    }
    if ($type == "LargePizza"){
        return new LargePizza;
    }
    if ($type == "SmSoda"){
        return new SmSoda;
    }
    if ($type == "MdSoda"){
        return new MdSoda;
    }
    if ($type == "LgSoda"){
        return new LgSoda;
    }
  } 
}

$factory = new DescriptionFactory();

$small = $factory->create("SmallPizza");
$medium = $factory->create("MediumPizza");
$large = $factory->create("LargePizza");
$sm = $factory->create("SmSoda");
$md = $factory->create("MdSoda");
$lg = $factory->create("LgSoda");

echo $small->describe();
echo $medium->describe();
echo $large->describe();
echo $sm->describe();
echo $md->describe();
echo $lg->describe();

?>

I'm assuming the interface is in it's own folder called interfaces? I'm also not sure where in MVC interfaces would go.

Would all of the classes between interface Description and class DescriptionFactory be my models?

Would the class DescriptionFactory be a Model?

Would everything after the class DescriptionFactory be in the view?

I'm not entirely sure.

Thank you for your time.

Aucun commentaire:

Enregistrer un commentaire