有一款游戏软件,分为与电脑对战和与人对战两种玩法,而每种玩法又分简易操作和复杂操作两种,假设现在需要你根据选择的情况创建具体的游戏对象,你会采用什么样的工厂模式进行设计,请给出设计方案。

 

Game.java

package Gaming;

public interface Game {
	public Computer creatComputer();
	public People creatPeople();
}

easyGame.java

package Gaming;


public class easyGame implements Game {

	@Override
	public Computer creatComputer() {
		// TODO Auto-generated method stub
		return new easyComputer();
	}

	@Override
	public People creatPeople() {
		// TODO Auto-generated method stub
		return new easyPeople();
	}

}

hardGame.java

package Gaming;

public class hardGame implements Game {

	@Override
	public Computer creatComputer() {
		// TODO Auto-generated method stub
		return new hardComputer();
	}

	@Override
	public People creatPeople() {
		// TODO Auto-generated method stub
		return new hardPeople();
	}

}

Computer.java

package Gaming;

public interface Computer {
	public void Creat();
}

easyComputer.java

package Gaming;

public class easyComputer implements Computer {

	@Override
	public void Creat() {
		// TODO Auto-generated method stub
		System.err.println("这是简单电脑模式");
	}
	
}

hardComputer.java

package Gaming;

public class hardComputer implements Computer {

	@Override
	public void Creat() {
		// TODO Auto-generated method stub
		System.err.println("这是困难电脑模式");
	}

}

 

People.java

package Gaming;

public interface People {

	public void creatPeple();

}

easyPeople.java

package Gaming;


public class easyPeople implements People {

	@Override
	public void creatPeple() {
		// TODO Auto-generated method stub
		System.err.println("这是简单玩家对战模式");
	}

}

hardPeople.java

​
package Gaming;

public class hardPeople implements People {

	@Override
	public void creatPeple() {
		// TODO Auto-generated method stub
		System.err.println("这是困难玩家对战模式");
	}

}

​

 

Test.java

package Gaming;


public class Test {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Game game;
		Computer com;
		game=new easyGame();
		com=game.creatComputer();
		com.Creat();
		System.err.println("---------------------");
		People peo;
		peo=game.creatPeople();
		peo.creatPeple();
	}

}

 

运行截图:

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐