vendredi 12 juillet 2019

Handling similar classes without conditional statements?

So I got an assignment to make a website that displays a list of various items. They share some properties like names, but each has unique properties like dimensions or weight as well. Since this is an exercise in OOP this could be split into a parent class that contains common properties and each item would have a class that extends that parent class.

Part of the assignment is to make a dynamic form for adding an item which should change depending on what type of item is selected. The problem is that the assignment specifically states not to use conditional statements to decide what form to choose.

I made the form but using a simple switch statement to decide what dynamic form to load.

switch ($_POST['thing']) {
    case 'thing1':
        require '../views/thing1.php';
        break;
    case 'thing2':
        require '../views/thing2.php';
        break;
}

I'm aware of various OOP design patters but none of them really seem to fit this problem in my mind. So how would one go about this without making a giant conditional for 100 different items?

Aucun commentaire:

Enregistrer un commentaire