我想知道下面的secnerio是否违反了得墨忒耳法。
CarFactory
和BusFactory
。CarFactory
将返回IVehicle
类型的对象。VehicleFactory
。VehicleFactory
将返回CarFactory
或'BusFactory'VehcileFactory
来返回CarFactory
或BusFactory
。CarFactory
或BusFactory
将返回Car(比如Maruthi)或Bus类型(比如Volvo)。以下是示例代码
分类:Maruthi
public class Maruthi:IVehicle
{
public void MakeVehicle()
{
Console.WriteLine("Maruthi..");
}
}
分类:CarFactory
public class CarFactory:IVehicleFactory
{
IVehicle car;
public IVehicle GetVehicle(string type)
{
switch (type)
{
case "Maruthi":
car= new Maruthi();
break;
}
return car;
}
}
分类:VehicleFactory
public class VehicleFactory
{
public static IVehicleFactory GetVehicleFactory (string Factorytype)
{
IVehicleFactory VehFact=null;
switch (Factorytype)
{
case "Car":
VehFact = new CarFactory();
break;
}
return VehFact;
}
}
主要
static void Main(string[] args)
{
IVehicleFactory vehFact = VehicleFactory.GetVehicleFactory("Car");
IVehicle veh = vehFact.GetVehicle("Maruthi");
veh.MakeVehicle();
Console.ReadLine();
}
iVehicle
namespace LOD_ViolationorNot
{
public interface IVehicle
{
void MakeVehicle();
}
}