创建型模式(Simple Factory)
转自:
http://www.cnblogs.com/zhenyulu/articles/36462.html
代码为
演化为
代码为
http://www.cnblogs.com/zhenyulu/articles/36462.html
代码为
using System;
public abstract class Light
{
public abstract void TurnOn();
public abstract void TurnOff();
}
public class BulbLight : Light
{
public override void TurnOn()
{
Console.WriteLine("Bulb Light is Turned on");
}
public override void TurnOff()
{
Console.WriteLine("Bulb Light is Turned off");
}
}
public class TubeLight : Light
{
public override void TurnOn()
{
Console.WriteLine("Tube Light is Turned on");
}
public override void TurnOff()
{
Console.WriteLine("Tube Light is Turned off");
}
}
public class LightSimpleFactory
{
public Light Create(string LightType)
{
if(LightType == "Bulb")
return new BulbLight();
else if(LightType == "Tube")
return new TubeLight();
else
return null;
}
}
public class Client
{
public static void Main()
{
LightSimpleFactory lsf = new LightSimpleFactory();
Light l = lsf.Create("Bulb");
l.TurnOn();
l.TurnOff();
Console.WriteLine("-----------------");
l = lsf.Create("Tube");
l.TurnOn();
l.TurnOff();
}
}
public abstract class Light
{
public abstract void TurnOn();
public abstract void TurnOff();
}
public class BulbLight : Light
{
public override void TurnOn()
{
Console.WriteLine("Bulb Light is Turned on");
}
public override void TurnOff()
{
Console.WriteLine("Bulb Light is Turned off");
}
}
public class TubeLight : Light
{
public override void TurnOn()
{
Console.WriteLine("Tube Light is Turned on");
}
public override void TurnOff()
{
Console.WriteLine("Tube Light is Turned off");
}
}
public class LightSimpleFactory
{
public Light Create(string LightType)
{
if(LightType == "Bulb")
return new BulbLight();
else if(LightType == "Tube")
return new TubeLight();
else
return null;
}
}
public class Client
{
public static void Main()
{
LightSimpleFactory lsf = new LightSimpleFactory();
Light l = lsf.Create("Bulb");
l.TurnOn();
l.TurnOff();
Console.WriteLine("-----------------");
l = lsf.Create("Tube");
l.TurnOn();
l.TurnOff();
}
}
演化为
代码为
using System;
public class Light
{
public virtual void TurnOn()
{
}
public virtual void TurnOff()
{
}
public static Light Create(string LightType)
{
if(LightType == "Bulb")
return new BulbLight();
else if(LightType == "Tube")
return new TubeLight();
else
return null;
}
}
public class BulbLight : Light
{
public override void TurnOn()
{
Console.WriteLine("Bulb Light is Turned on");
}
public override void TurnOff()
{
Console.WriteLine("Bulb Light is Turned off");
}
}
public class TubeLight : Light
{
public override void TurnOn()
{
Console.WriteLine("Tube Light is Turned on");
}
public override void TurnOff()
{
Console.WriteLine("Tube Light is Turned off");
}
}
public class Client
{
public static void Main()
{
Light l = Light.Create("Bulb");
l.TurnOn();
l.TurnOff();
Console.WriteLine("-----------------");
l = Light.Create("Tube");
l.TurnOn();
l.TurnOff();
}
}
public class Light
{
public virtual void TurnOn()
{
}
public virtual void TurnOff()
{
}
public static Light Create(string LightType)
{
if(LightType == "Bulb")
return new BulbLight();
else if(LightType == "Tube")
return new TubeLight();
else
return null;
}
}
public class BulbLight : Light
{
public override void TurnOn()
{
Console.WriteLine("Bulb Light is Turned on");
}
public override void TurnOff()
{
Console.WriteLine("Bulb Light is Turned off");
}
}
public class TubeLight : Light
{
public override void TurnOn()
{
Console.WriteLine("Tube Light is Turned on");
}
public override void TurnOff()
{
Console.WriteLine("Tube Light is Turned off");
}
}
public class Client
{
public static void Main()
{
Light l = Light.Create("Bulb");
l.TurnOn();
l.TurnOff();
Console.WriteLine("-----------------");
l = Light.Create("Tube");
l.TurnOn();
l.TurnOff();
}
}