/// <summary>
/// 生成家用门的工厂
/// </summary>
interface IDoorFactory
{
IDoor CreateDoor();
}
interface IDoor
{
void Open();
}
/// <summary>
/// 生产木门的工厂
/// </summary>
class WoodenDoorFactory : IDoorFactory
{
IDoor _door;
public WoodenDoorFactory()
{
_door = new WoodenDoor();
}
public IDoor CreateDoor()
{
return _door;
}
}
/// <summary>
/// 生产铁门的工厂
/// </summary>
class IronDoorFactory: IDoorFactory
{
IDoor _door;
public IronDoorFactory()
{
_door = new IronDoor();
}
public IDoor CreateDoor()
{
return _door;
}
}
/// <summary>
/// 铁门
/// </summary>
class IronDoor : IDoor
{
public void Open()
{
Console.WriteLine("铁门打开");
}
}
/// <summary>
/// 木门
/// </summary>
class WoodenDoor : IDoor
{
public void Open()
{
Console.WriteLine("木头门打开");
}
}
//调用方式
static IDoorFactory factory ;
static IDoor door ;
static void Main(string[] args)
{
factory = new WoodenDoorFactory();
door = factory.CreateDoor();
door.Open();
factory = new IronDoorFactory();
door = factory.CreateDoor();
door.Open();
Console.ReadKey();
}