3.2. Using an Object Factory

All Java classes have constructors. If you write code for a class without explicitly supplying one, then a default constructor taking no arguments will be provided for you by the compiler. Invoking the constructor of a class is a straightforward operation when you know the type of object you want to create, but frequently there is the need to write code that has the flexibility to create several different classes of objects. Sometimes the set of possible object types is known in advance, but often software needs to be designed to provide extensibility to handle types that may be created later.

An Object Factory is a common design pattern for handling the need to create objects of types that aren't specified at compile ...

Get J2EE™ and JAX™: Developing Web Applications and Web Services now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.