您好,登錄后才能下訂單哦!
本文模擬賽車游戲,抽象場景工廠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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。