In an RPG game, use a simple factory mode to create game characters. The game can create different roles according to the parameters selected by the user . For example, when the parameter is " angel", an angel role is created, and when the parameter is " hero " A hero character that creates a witch character when the parameter is "witch". Draw class diagrams and simulate implementation using Java language programming.
Class Diagram:
Role class:
public abstract class Role {
public abstract void display();
}
Angle class:
public class Angle extends Role {
public Angle(){}
public void display(){
System.out.println("angle");
}
}
Hero class:
public class Hero extends Role {
public Hero(){}
public void display(){
System.out.println("hero");
}
}
Witch class:
public class Witch extends Role {
public Witch(){}
public void display(){
System.out.println("witch");
}
}
RoleFactory class:
public class RoleFactory {
public static Role getRole(String type){
Role role = null;
if (type.equalsIgnoreCase("angle")){
role = new Angle();
}
if (type.equalsIgnoreCase("witch")){
role = new Witch();
}
if (type.equalsIgnoreCase("Hero")){
role = new Hero();
}
return role;
}
}
Main class (client):
public class Main {
public static void main(String[] args) {
Role role;
role = RoleFactory.getRole ("hero");
if (role==null){
System.out.println("Failed to create role... Please check parameters");
}else{
role.display();
}
// System.out.println("Hello World!");
}
}