using System;
namespace 设计模式_抽象工厂
{
class Program
{
public abstract class Road
{
}
public abstract class Building
{
}
public abstract class Tunnel
{
}
public abstract class Jungle
{
}
public abstract class FacilitiesFactory
{
public abstract Road CreateRoad();
public abstract Building CreateBuilding();
public abstract Tunnel CreateTunnel();
public abstract Jungle CreateJungle();
}
//客户程序-GameManager
class GameManager
{
FacilitiesFactory facilitiesFactory;
public GameManager(FacilitiesFactory facilitiesFactory)
{
this.facilitiesFactory = facilitiesFactory;
}
public void BuildGameFacilities()
{
Road road = facilitiesFactory.CreateRoad();
Building building = facilitiesFactory.CreateBuilding();
Tunnel tunnel = facilitiesFactory.CreateTunnel();
Jungle jungle = facilitiesFactory.CreateJungle();
}
}
static void Main(string[] args)
{
GameManager gameManager = new GameManager(new MDFacilitiesFactory ());
gameManager.BuildGameFacilities();
}
#region 一种工厂和抽象子类
public class MDFacilitiesFactory:FacilitiesFactory
{
public override Road CreateRoad()
{
return new MDRoad();
}
public override Building CreateBuilding()
{
return new MDBuilding();
}
public override Tunnel CreateTunnel()
{
return new MDTunnel();
}
public override Jungle CreateJungle()
{
return new MDJungle();
}
}
public class MDRoad:Road
{
}
public class MDBuilding:Building
{
}
public class MDTunnel:Tunnel
{
}
public class MDJungle:Jungle
{
}
#endregion
}
}