设计模式随笔-从“有病”说起(工厂模式前传)
--------------------------------------------------------------------------------
作者:吕震宇
Version:1.0 URL:http://www.cnblogs.com/zhenyulu/articles/41388.html
关键词:设计模式、工厂
using System;
public class Client
{
public static void Main(string[] argvs)
{
Car c;
if(argvs.Length < 1)
{
PrintHelp();
return;
}
// 司机将车开来
if(argvs[0] == "V")
c = new Volvo();
else if(argvs[0] == "B")
c = new Bench();
else if(argvs[0] == "M")
c = new Mazda();
else
{
PrintHelp();
return;
}
// C#先生发号施令“开车!”
c.Drive();
}
private static void PrintHelp()
{
Console.WriteLine("Please input a correct car type.(V, B, M)");
Console.WriteLine("For example: CarTest M");
}
}
public abstract class Car
{
public abstract void Drive();
}
public class Volvo : Car
{
public override void Drive()
{
Console.WriteLine("Driving Volvo ");
}
}
public class Bench : Car
{
public override void Drive()
{
Console.WriteLine("Driving Bench ");
}
}
public class Mazda : Car
{
public override void Drive()
{
Console.WriteLine("Driving Mazda ");
}
}
详细解释请参阅原文 http://www.cnblogs.com/zhenyulu/articles/41388.html