public abstract class User {
protected String name;
public abstract boolean isNull();
public abstract String getName();
}
public class Player extends User{
public Player() {}
public Player(String _name) {
name = _name;
}
@Override
public boolean isNull() {
return false;
}
@Override
public String getName() {
return name;
}
}
public class NullPlayer extends User{
public NullPlayer() {}
public NullPlayer(String _name) {
name = _name;
}
@Override
public boolean isNull() {
return true;
}
@Override
public String getName() {
return "Null Player";
}
}
public class PlayerFactory {
public PlayerFactory(){}
public User create(String name) {
if(name.equals("PlayerName"))
return new Player(name);
return new NullPlayer(name);
}
}
public class Main {
public static void main(String[] args) {
PlayerFactory playerFactory = new PlayerFactory();
User user1 = playerFactory.create("PlayerName");
User user2 = playerFactory.create("Unkown");
System.out.println("user1:"+user1.getName());
System.out.println("user2:"+user2.getName());
}
}