Factory模式的学习

using System;
using System.Windows.Forms;
namespace Factory
{
 public abstract class Road
 {}
 public abstract class Builld
 {}
 public abstract class Tunnel
 {}
 public abstract class Jungle
 {}
 public abstract class FacilitiesFactory
 {
  public abstract Road CreateRoad();
  public abstract Builld CreateBuilld();
  public abstract Tunnel CreateTunnel();
  public abstract Jungle CreateJungle();
 }

 


 public class GameManager
 {
  Road road;
  Builld builld;
  FacilitiesFactory facilitiesFactory;
  public GameManager(FacilitiesFactory facilitiesFactory)//只有此处可变化,可通过配置文件变化
  {
   this.facilitiesFactory=facilitiesFactory;
  }
  public void BuildFacilitiles()
  {
   road=this.facilitiesFactory.CreateRoad();
   builld=this.facilitiesFactory.CreateBuilld();
  }
  public void Run()
  {
   MessageBox.Show(road.GetType().ToString());
   MessageBox.Show(builld.GetType().ToString());
  }
 }
 public class Test
 {
  public static void Main()
  {
   GameManager g=new GameManager(new MordenFacilitiesFactory());
   g.BuildFacilitiles();
  }
 }

 

 public class MordenRoad:Road
 {}
 public class MordenBuilld:Builld
 {}
 public class MordenTunnel:Tunnel
 {}
 public class MordenJungle:Jungle
 {}
 public class MordenFacilitiesFactory:FacilitiesFactory
 {
  public override Road CreateRoad()
  {
   return new MordenRoad();
  }
  public override Builld CreateBuilld()
  {
   return new MordenBuilld();
  }
  public override Tunnel CreateTunnel()
  {
   return new MordenTunnel();
  }
  public override Jungle CreateJungle()
  {
   return new MordenJungle();
  }
 }

 public class ClassicRoad:Road
 {}
 public class ClassicBuilld:Builld
 {}
 public class ClassicTunnel:Tunnel
 {}
 public class ClassicJungle:Jungle
 {}
 public class ClassicFacilitiesFactory:FacilitiesFactory
 {
  public override Road CreateRoad()
  {
   return new ClassicRoad();
  }
  public override Builld CreateBuilld()
  {
   return new ClassicBuilld();
  }
  public override Tunnel CreateTunnel()
  {
   return new ClassicTunnel();
  }
  public override Jungle CreateJungle()
  {
   return new ClassicJungle();
  }
 }
}

posted @ 2006-03-05 18:10  kuailewangzi1212  阅读(155)  评论(0编辑  收藏  举报