• User.java
public abstract class User {
       protected String name;
       public abstract boolean isNull();
       public abstract String getName();
}
  • Player.java
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;
       }
}
  • NullPlayer.java
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";
       }
}
  • PlayerFactory
public class PlayerFactory {
       public PlayerFactory(){}
       public User create(String name) {
              if(name.equals("PlayerName"))
                     return new Player(name);
              
              return new NullPlayer(name);
       }
}
  • Main.java
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());                 
       }
}