【发布时间】:2015-06-19 21:33:53
【问题描述】:
是否有更好的方法来存储以下示例所需的值。将字符串传递给工厂方法时,我试图避免任何可能的错误。
但是,这有什么问题吗?它是否易于测试,是否有更好的方法来实现相同的目标等。
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)
}
}
【问题讨论】:
-
枚举是这里的正确选择。
标签: design-patterns constants factory