namespace DXWindowsApplication2
{
public class Main
{
public void Main()
{
IVehicle vehicle1 = VehicleFactory.GetVehicle(VehicleType.Car);
vehicle1.run();
IVehicle vehicle2 = VehicleFactory.GetVehicle(VehicleType.Bus);
vehicle2.run();
}
}
public interface IVehicle
{
void run();
}
public class Car : IVehicle
{
public void run()
{
MessageBox.Show("Car");
}
}
public class Bus : IVehicle
{
public void run()
{
MessageBox.Show("Bus");
}
}
public enum VehicleType
{
Car,
Bus
}
public class VehicleFactory
{
public static IVehicle GetVehicle(VehicleType vehicleType)
{
switch (vehicleType)
{
case VehicleType.Car:
return new Car();
case VehicleType.Bus:
return new Bus();
default:
return null;
}
}
}
}