Java 17 Recipes


-4. Generating Instances of a Class


Download 3.2 Mb.
Pdf ko'rish
bet95/245
Sana02.06.2024
Hajmi3.2 Mb.
#1839910
1   ...   91   92   93   94   95   96   97   98   ...   245
Bog'liq
Java 17 Recipes

5-4. Generating Instances of a Class
 Problem
In one of your applications, you want to provide the ability to generate instances of an 
object on the fly. Each instance of the object should be ready to use, and the object 
creator should not need to know about the details of the object creation.
 Solution
Use the factory method pattern to instantiate instances of the class while abstracting 
the creation process from the object creator. Creating a factory enables new instances 
of a class to be returned on invocation. The following class represents a simple factory 
that returns a new instance of a Player subclass each time its createPlayer(String) 
method is called. The subclass of Player that is returned depends on what string value is 
passed to the createPlayer method.
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;
Chapter 5 ObjeCt-Oriented java


160
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 method, 
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 {
private int totalSaves;
public Goalie(){
this.setPosition("GOALIE");
}
/**
* @return the totalSaves
*/
public int getTotalSaves() {
return totalSaves;
}
/**
* @param totalSaves the totalSaves to set
*/
Chapter 5 ObjeCt-Oriented java


161
public void setTotalSaves(int totalSaves) {
this.totalSaves = totalSaves;
}
}
The Player class is:
package org.java17recipes.chapter05.recipe05_04;
public abstract class Player implements PlayerType {
private String firstName;
private String lastName;
private String position;
private int status = -1;
public Player(){
}
public Player (String position, int status){
this.position = position;
this.status = status;
}
protected String playerStatus(){
String returnValue = null;
switch(getStatus()){
case 0:
returnValue = "ACTIVE";
case 1:
returnValue = "INACTIVE";
case 2:
returnValue = "INJURY";
default:
returnValue = "ON_BENCH";
}
return returnValue;
}
Chapter 5 ObjeCt-Oriented java


162
public String playerString(){
return getFirstName() + " " + getLastName() + " -
" + getPosition();
}
/**
* @return the firstName
*/
public String getFirstName() {
return firstName;
}
/**
* @param firstName the firstName to set
*/
public void setFirstName(String firstName) {
if (firstName.length() > 30){
this.firstName = firstName.substring(0, 29);
} else {
this.firstName = firstName;
}
}
/**
* @return the lastName
*/
public String getLastName() {
return lastName;
}
/**
* @param lastName the lastName to set
*/
public void setLastName(String lastName) {
this.lastName = lastName;
}
Chapter 5 ObjeCt-Oriented java


163
/**
* @return the position
*/
@Override
public String getPosition() {
return position;
}
/**
* @param position the position to set
*/
public void setPosition(String position) {
this.position = position;
}
/**
* @return the status
*/
public int getStatus() {
return status;
}
/**
* @param status the status to set
*/
public void setStatus(int status) {
this.status = status;
}
}
The PlayerType interface is:
public interface PlayerType {
public String position=””;
public String getPosition();
}
Chapter 5 ObjeCt-Oriented java


164
Each of the other Player subclasses is very similar to the Goalie class. The most 
important code to note is the factory method, createPlayer, which can create new 
instances of the Player class.
Note to take this example one step further, you can limit the methods that can 
be accessed. You do this by returning objects of type PlayerType, and only 
declaring the accessible methods within that interface.

Download 3.2 Mb.

Do'stlaringiz bilan baham:
1   ...   91   92   93   94   95   96   97   98   ...   245




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling