Java Reference
In-Depth Information
public class PlayerFactory {
public static Player createPlayer(String playerType){
Player returnType;
switch(playerType){
case "GOALIE":
returnType = new Goalie();
break;
case "LEFT":
returnType = new LeftWing();
break;
case "RIGHT":
returnType = new RightWing();
break;
case "CENTER":
returnType = new Center();
break;
case "DEFENSE":
returnType = new Defense();
break;
default:
returnType = new AllPlayer();
}
return returnType;
}
}
If a class wants to use the factory, it simply calls the static createPlayer meth-
od, passing a string value representing a new instance of Player . The following code
represents one of the Player subclasses; the others could be very similar:
public class Goalie extends Player implements PlayerType {
private int totalSaves;
public Goalie(){
this.setPosition("GOALIE");
}
Search WWH ::




Custom Search