## Factory method pattern

### Introduction

Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.

### Participants:

#### Product

- defines the interface of objects the factory method creates.

#### Concrete product

- implements the Product interface.
- this participant can not be read-only.

#### Creator

- declares the factory method, which returns an object of type Product. Creator may also define a default implementation of the factory method that returns a default Concrete Product object
- may call the factory method to create a Product object.
- this participant can not be read-only.

In the "Factory method name" field you can specify the name of a Creator's method which returns an object of type Product.

In the "Factory method parameters" field you can specify parameters when calling the factory method (for example, `int i, boolean b`. The pattern will create a constructor in the Concrete product with these parameters, so the factory method (in Concrete creator) will return an instance of Concrete product initialized with the specified parameters.

#### Concrete creator

- overrides the factory method to return an instance of a Concrete Product.
- this participant can not be read-only.
- this participant can not be an interface.

The "Copy documentation" option controls whether to copy JavaDoc comments from methods in interfaces participating in the pattern to the stubs of these methods made by the pattern in classes implementing such interfaces.

The "Create pattern links" option controls whether to generate additional links which can be used by this pattern later to determine classes and interfaces participating in the pattern. That means that if you selected this option and used the pattern to create a set of classes and interfaces, the pattern invoked for some participant later (via the popup menu item "Choose Pattern...") will automatically find (if it is possible) all other participants and fill in participant fields with their names.

Also, if you applied the pattern with this option on and invoked the pattern via the popup menu item "Choose Pattern..." for some participant later, the pattern will have additional field called "Use selected class as", containing possible roles for the selected element.

This option is very useful when you are planning to change something in the classes/interfaces participating in the pattern. For example, if this option is on and after creation of the classes and interfaces you added several methods to certain interface-participant (and this change must be reflected somehow in other participants), all you have to do is to select this changed interface, invoke the "Choose Pattern..." dialog for this element and select the original pattern. After that the pattern will determine other participants and you will only have to press "Finish". The pattern will modify all other classes and interfaces accordingly to your changes.

### Applicability

Use the Factory Method pattern when

- a class can't anticipate the class of objects it must create
- a class wants its subclasses to specify the objects it creates.
- classes delegate responsibility to one of several helper subclasses, and you want to localize the knowledge of which helper subclass is the delegate.