本文模拟赛车游戏,抽象场景工厂FacilitiesFactory ,引用到抽象道路,抽象建筑物等类,实例中 场景继承抽象场景,道路继承抽象道路,实现多类交叉的方法。
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();
}
}
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。