vendredi 19 juin 2015

Using a class to store values

Is there a better approach to storing values required as per the following example. Im trying to avoid any possible errors when passing the string to factory method

abstract class Types
{
    const Car = 'car';
    const Boat = 'boat';
    const Bike = 'bike';
}

class VehicleFactory {

    public function make($type) 
    {
        if ($type === Types::Car) {
            // create new car
        }
        if ($type === Types::Boat) {
            // create new Boat
        }
        if ($type === Types::Bike) {
            // create new Bike
        }
    }
}

class Client
{
    public function createACar()
    {
        $vehicleFactory = new VehicleFactory();
        $car = $vehicleFactory->create(Types::Car)
    }
}

Aucun commentaire:

Enregistrer un commentaire