抽象工厂模式---游戏等级

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

 

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();
	}

}

 

运行截图:

  • 2
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
当然,我非常了解抽象工厂模式,以下是Java代码演示: 首先,我们需要定义两个抽象产品接口:Game和Operation。 ```java public interface Game { void play(); } public interface Operation { void operate(); } ``` 然后,我们定义两个具体产品类:ComputerGame和HumanGame,以及其对应的简易操作复杂操作类。 ```java public class ComputerGame implements Game { @Override public void play() { System.out.println("与电脑对战游戏"); } } public class HumanGame implements Game { @Override public void play() { System.out.println("与人对战游戏"); } } public class SimpleOperation implements Operation { @Override public void operate() { System.out.println("简易操作"); } } public class ComplexOperation implements Operation { @Override public void operate() { System.out.println("复杂操作"); } } ``` 接着,我们定义抽象工厂接口:GameFactory。 ```java public interface GameFactory { Game createGame(); Operation createOperation(); } ``` 最后,我们定义两个具体工厂类:ComputerGameFactory和HumanGameFactory,别用于创建与电脑对战和与人对战游戏对象。 ```java public class ComputerGameFactory implements GameFactory { @Override public Game createGame() { return new ComputerGame(); } @Override public Operation createOperation() { return new ComplexOperation(); } } public class HumanGameFactory implements GameFactory { @Override public Game createGame() { return new HumanGame(); } @Override public Operation createOperation() { return new SimpleOperation(); } } ``` 使用抽象工厂模式,我们可以轻松地根据选择的情况创建具体的游戏对象,比如: ```java // 选择与电脑对战复杂操作游戏 GameFactory factory = new ComputerGameFactory(); Game game = factory.createGame(); Operation operation = factory.createOperation(); game.play(); // 输出:与电脑对战游戏 operation.operate(); // 输出:复杂操作 ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值