Java Reference
In-Depth Information
Factory Method
Also known as Virtual Constructor
Pattern Properties
Type: Creational
Level: Class
Purpose
To define a standard method to create an object, apart from a constructor, but the decision of what kind of an
object to create is left to subclasses.
Introduction
Imagine that you're working on a Personal Information Manager (PIM) application. It will contain many pieces
of information essential to your daily life: addresses, appointments, dates, books read, and so on. This information
is not static; for instance, you want to be able to change an address when a contact moves, or change the details of
an appointment if your lunch date needs to meet an hour later.
The PIM is responsible for changing each field. It therefore has to worry about editing (and therefore the User
Interface) and validation for each field. The big disadvantage, however, is that the PIM has to be aware of all the
different types of appointments and tasks that can be performed on them. Each item has different fields and the
user needs to see an input screen appropriate to those fields. It will be very difficult to introduce new types of task
information, because you will have to add a new editing capability to the PIM every time, suitable to update the
new item type. Furthermore, every change in a specific type of task, such as adding a new field to an appointment,
means you also have to update the PIM so that it is aware of this new field. You end up with a very bloated PIM
that is difficult to maintain.
The solution is to let items, like appointments, be responsible for providing their own editors to manage additions
and changes. The PIM only needs to know how to request an editor using the method getEditor , which is in
every editable item. The method returns an object that implements the ItemEditor interface, and the PIM uses
that object to request a JComponent as the GUI editor. Users can modify information for the item they want to
edit, and the editor ensures that the changes are properly applied.
All the information on how to edit a specific item is contained in the editor, which is provided by the item itself.
The graphical representation of the editor is also created by the editor itself. Now you can introduce new types of
items without having to change PIM.
Applicability
Use Factory Method pattern when:
You want to create an extensible framework. This means allowing flexibility by leaving some decisions, like the
specific kind of object to create, until later.
You want a subclass, rather than its superclass, to decide what kind of an object to create.
You know when to create an object, but not what kind of an object.
You need several overloaded constructors with the same parameter list, which is not allowed in Java. Instead, use
several Factory Methods with different names.
Description
This pattern is called Factory Method because it creates (manufactures) objects when you want it to.
When you start writing an application, it's often not clear yet what kind of components you will be using.
Normally you will have a general idea of the operations certain components should have, but the implementation
is done at some other time and will not be of consequence at that moment.
 
Search WWH ::




Custom Search