Java Reference
In-Depth Information
Another object that has access to the PlayerFactory class can use createPlay-
er to return new Player objects without knowing how the object is created. While
this does not hide much in the case of the createPlayer method, the Player-
Factory abstracts the details of which class is being instantiated so that the developer
only has to worry about obtaining a new Player object.
The factory pattern is an effective way to control how objects are created and
makes it easier to create objects of a certain type. Imagine if a constructor for an object
took more than just a handful of arguments; creating new objects that require more than
just a couple of arguments can become a hassle. Generating a factory to create those
objects so that you do not have to hard-code all the arguments with each instantiation
can make you much more productive!
5-5. Creating Reusable Objects
Problem
You would like to generate an object that could be used to represent something within
your application. For instance, suppose that you are creating an application that will be
used for generating statistics and league information for different sports teams.
Solution
Create a JavaBean that can be used to represent the object that you want to create.
JavaBean objects provide the capability for object fields to be declared as private ,
and they also allow the attributes to be read and updated so that an object can be passed
around and used within an application. This recipe demonstrates the creation of a
JavaBean named Team . The Team object contains a few different fields that can be
used to contain information:
public class Team implements TeamType {
private List<Player> players;
private String name = null;
private String city = null;
/**
Search WWH ::




Custom Search