抽象类的工厂模式
本文模拟赛车游戏,抽象场景工厂FacilitiesFactory ,引用到抽象道路,抽象建筑物等类,实例中 场景继承抽象场景,道路继承抽象道路,实现多类交叉的方法。
博罗网站建设公司创新互联建站,博罗网站设计制作,有大型网站制作公司丰富经验。已为博罗上1000+提供企业网站建设服务。企业网站搭建\成都外贸网站建设公司要多少钱,请找那个售后服务好的博罗做网站的公司定做!
package test;
public class AbstractFactory {
//抽象 道路
public abstract class Road
{
int road=12;
//沙路
public void aa(){road=1;}
//水泥路
public void ab(){road=2;}
//山路
public void ac(){road=3;}
}
//抽象 建筑物
public abstract class Building
{
String gg="建筑物";
public void bb() {}
}
//抽象 地道
public abstract class Tunnel{
String gg="地道";
}
//抽象 丛林
public abstract class Jungle
{
String gg="建筑物";
}
//抽象一个设施工厂,包含所有赛车游戏的所有设施
public abstract class FacilitiesFactory
{
public abstract Road CreateRoad();
public abstract Building CreateBuilding();
public abstract Tunnel CreateTunnel();
public abstract Jungle CreateJungle();
}
//道路
public class ModernRoad extends Road
{
}
//建筑物
public class ModernBuilding extends Building
{
}
//地道
public class ModernTunnel extends Tunnel
{
String gg="地道";
}
//丛林
public class ModernJungle extends Jungle
{
String gg="丛林";
}
//新建一个设施工厂,包含所有赛车游戏的所有设施
public class ModernFacilitiesFactory extends FacilitiesFactory
{
public Road CreateRoad() {
return new ModernRoad();
}
public Building CreateBuilding(){
return new ModernBuilding();
}
public Tunnel CreateTunnel(){
return new ModernTunnel();
}
public Jungle CreateJungle(){
return new ModernJungle();
}
}
//客户程序
public class GameManger{
Road road;
Building buliding;
FacilitiesFactory fac;
public GameManger(FacilitiesFactory fac){
this.fac=fac;
}
//实际创建 赛道,建筑物,丛林等等
public void BuildGameFacilities(){
road = fac.CreateRoad();
System.out.println(road.road);
buliding=fac.CreateBuilding();
System.out.println(buliding.gg);
}
public void run(){
BuildGameFacilities();
System.out.println(road.road);
//road.aa();
buliding.bb();
}
}
public void play(){
GameManger ga = new GameManger(new ModernFacilitiesFactory());
ga.run();
}
public static void main(String[] msg){
AbstractFactory ab =new AbstractFactory();
ab.play();
}
}
网站名称:抽象类的工厂模式
文章出自:http://myzitong.com/article/ghcgje.html