抽象工厂模式

格斗类游戏软件

Fighter: SlowFighter, MedFighter, SuperFighter

Monster: SlowMonster, MedMonster, SuperMonster

注意: 用户有三种选择:初级,中级,高级

初级: SlowFighter, SlowMonster

中级: MedFighter, MedMonster

高级: SuperFighter, SuperMonster

 抽象工厂EnemyFactory

package game;

public abstract class EnemyFactory {
	public abstract Fighter getFighter();
	public abstract Monster getMonster();
	public static EnemyFactory getFactory(String type) {
		if(type.equals("初级")) {
			return new SlowEnemyFactory();
		}
		else if(type.equals("中级")) {
			return new MedEnemyFactory();
		}
		else if(type.equals("高级")) {
			return new SuperEnemyFactory();
		}
		return null;
	}
}

初级SlowEnemyFactory

package game;

public class SlowEnemyFactory extends EnemyFactory{
	public Fighter getFighter() {
		return new SlowFighter();
	}
	public Monster getMonster() {
		return new SlowMonster();
	}
}

中级MedEnemyFactory

package game;

public class MedEnemyFactory extends EnemyFactory{
	public Fighter getFighter() {
		return new MedFighter();
	}
	public Monster getMonster() {
		return new MedMonster();
	}
}

高级SuperEnemyFactory

package game;

public class SuperEnemyFactory extends EnemyFactory{
	public Fighter getFighter() {
		return new SuperFighter();
	}
	public Monster getMonster() {
		return new SuperMonster();
	}
}

Fighter接口

package game;

public interface Fighter {
	public void getSpeed();
	public void getWeapon();
}

SlowFighter实现类

package game;

public class SlowFighter implements Fighter{
	int speed = 10;
	String weapon = "匕首";
	public void getSpeed() {
		System.out.println("我是初级敌人我的速度是"+speed+"m/s");
	}
	public void getWeapon() {
		System.out.println("我是初级敌人我的武器"+weapon);
	}
}

MedFighter实现类

package game;

public class MedFighter implements Fighter{
	int speed = 50;
	String weapon = "蝴蝶剑";
	public void getSpeed() {
		System.out.println("我是中级敌人我的速度是"+speed+"m/s");
	}
	public void getWeapon() {
		System.out.println("我是中级敌人我的武器是"+weapon);
	}
}

SuperFactory实现类

package game;

public class SuperFighter implements Fighter{
	int speed = 100;
	String weapon = "雷神之锤";
	public void getSpeed() {
		System.out.println("我是高级敌人我的是"+speed+"m/s");
	}
	public void getWeapon() {
		System.out.println("我是高级敌人我的武器是"+weapon);
	}
}

Monster接口

package game;

public interface Monster {
	public void getVitality();
	public void getIntelligence();
}

SlowMonster实现类

package game;

public class SlowMonster implements Monster{
	String vitality = "初极怪兽";
	String intelligence = "不太聪明(●'◡'●)";
	public void getVitality() {
		System.out.println("我是"+vitality);
	}
	public void getIntelligence() {
		System.out.println("我"+intelligence);
	}
}

MedMonster实现类

package game;

public class MedMonster implements Monster{
	String vitality = "中极怪兽";
	String intelligence = "比较聪明(*^-^*)";
	public void getVitality() {
		System.out.println("我是"+vitality);
	}
	public void getIntelligence() {
		System.out.println("我"+intelligence);
	}
}

SuperMonster实现类

package game;

public class SuperMonster implements Monster{
	String vitality = "高极怪兽";
	String intelligence = "最聪明(*/ω\*)";
	public void getVitality() {
		System.out.println("我是"+vitality);
	}
	public void getIntelligence() {
		System.out.println("我"+intelligence);
	}
}

Client客户端进行选择

package game;
import java.util.Scanner;
public class Client {
	public static void main(String args[]) {
		while(true) {
			String menu = "请输入您想格斗的等级:\n"+
		                  "1.初级\n"+
					      "2.中级\n"+
		                  "3.高级\n";
			System.out.println(menu);
			Scanner sc = new Scanner(System.in);
			String type = sc.next();
			EnemyFactory619 ef = EnemyFactory619.getFactory(type);
			Fighter f = ef.getFighter();
			Monster m = ef.getMonster();
			f.getSpeed();
			f.getWeapon();
			m.getVitality();
			m.getIntelligence();
	}
}
}

你可能感兴趣的:(抽象工厂模式)