←Older revision | Revision as of 08:32, 18 April 2009 | ||
Line 2: | Line 2: | ||
Example: | Example: | ||
+ | <source lang="java"> | ||
public enum BankAccountEnum | public enum BankAccountEnum | ||
{ | { | ||
Line 19: | Line 20: | ||
public abstract BankAccount createBankAccount(); | public abstract BankAccount createBankAccount(); | ||
} | } | ||
+ | </source> | ||
Each result instance carries with it a reference to the factory instance that created it. I can directly get the correct factory instance to create a clone. | Each result instance carries with it a reference to the factory instance that created it. I can directly get the correct factory instance to create a clone. | ||
Line 24: | Line 26: | ||
The BankAccount base class can be polymorphically subclassed into SavingsAccount and CheckingAccount, with corresponding subclasses for SavingsAccountFactory and CheckingAccountFactory. | The BankAccount base class can be polymorphically subclassed into SavingsAccount and CheckingAccount, with corresponding subclasses for SavingsAccountFactory and CheckingAccountFactory. | ||
+ | <source lang="java"> | ||
public abstract class CheckingAccount | public abstract class CheckingAccount | ||
extends BankAccount | extends BankAccount | ||
Line 50: | Line 53: | ||
} | } | ||
} | } | ||
+ | </source> | ||
and a similar pair of classes for SavingsAccount. I can keep the factory instances in an array that is indexed by the BankAccountEnum to select the factory that matches the kind of account that I want to create. Or I can wrap the array within a "Factories" class that has methods for obtaining the factory instance or directly creating an instance from a specified BankAccountEnum. | and a similar pair of classes for SavingsAccount. I can keep the factory instances in an array that is indexed by the BankAccountEnum to select the factory that matches the kind of account that I want to create. Or I can wrap the array within a "Factories" class that has methods for obtaining the factory instance or directly creating an instance from a specified BankAccountEnum. | ||
+ | <source lang="java"> | ||
public abstract class BankAccountFactories | public abstract class BankAccountFactories | ||
{ | { | ||
Line 66: | Line 71: | ||
} | } | ||
} | } | ||
+ | </source> | ||
The "Factories" instance can be implemented elsewhere and saved in a global repository for any component to use without knowing anything about the underlying implementation. The use of enum indeces allows for easily extending the polymorphism to future subclasses (e.g., MoneyMarketAccount) without affecting existing clients. | The "Factories" instance can be implemented elsewhere and saved in a global repository for any component to use without knowing anything about the underlying implementation. The use of enum indeces allows for easily extending the polymorphism to future subclasses (e.g., MoneyMarketAccount) without affecting existing clients. |