- Forward


Designing with Constructors and Factories
An Introduction with Examples in Java


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Review of Constructors
Back SMYC Forward
  • Purpose:
    • Initialize the attributes of an instance of a class
      Expand
  • Comparison to Other Methods:
    • Difference -
      No return type (because they do not return anything)
      Expand
    • Similarity -
      Can throw exceptions
      Expand
  • Calling:
    • Indirectly (i.e., the new operator calls the constructor)
      Expand
Design Issues
Back SMYC Forward
  • A Weakness of Constructors:
    • Because they do not return anything, we are limited in what we can do with them
  • An Alternative/Supplement to Constructors:
    • Factory Methods (i.e., static methods that construct and return objects)
Review of an Earlier Example
Back SMYC Forward
  • An Abstract Class:
    • SecurityQuotation
  • Two Concrete Specializations:
    • StockQuotation
    • FutureQuotation
Review of an Earlier Example (cont.)
Back SMYC Forward
javaexamples/oopbasics/finance/SecurityQuotation.java
 
Review of an Earlier Example (cont.)
Back SMYC Forward
javaexamples/oopbasics/finance/StockQuotation.java
 
Review of an Earlier Example (cont.)
Back SMYC Forward
javaexamples/oopbasics/finance/FutureQuotation.java
 
Our Current Objectives
Back SMYC Forward
  • Add Functionality:
    • Provide the ability to construct an instance of the concrete specializations from a String representation (e.g., lines read from a file)
  • Consider Alternative Designs:
    • There are several ways to add this functionality and it is a useful exercise to consider their advantages and disadvantages
Design 1
Back SMYC Forward
  • General Approach:
    • Avoid code duplication
    • Put as much functionality in the base class as possible
  • Specifics:
    • Use constructors
Approach 1 (cont.)
Back SMYC Forward

The constructor in SecurityQuotation is straightforward.

javaexamples/oopbasics/betterfinance/SecurityQuotation.java (Fragment: constructor)
 
Approach 1 (cont.)
Back SMYC Forward

The constructor in StockQuotation is elegant.

javaexamples/oopbasics/betterfinance/StockQuotation.java (Fragment: constructor)
 
Approach 1 (cont.)
Back SMYC Forward

The constructor in FutureQuotation is awful!

javaexamples/oopbasics/betterfinance/FutureQuotation.java (Fragment: constructor)
 

It has to do unnecessary work and it has to know what work was done in the base class.

Design 2
Back SMYC Forward
  • General Approach:
    • Avoid code duplication
    • Put as much functionality in the base class as possible
  • Specifics:
    • Use a fromString() method that sets the attributes of an empty object from a String representation
Approach 2 (cont.)
Back SMYC Forward

The SecurityQuotation class now needs a default constructor (because the derived classes will), but it can be protected.

javaexamples/oopbasics/stillbetterfinance/SecurityQuotation.java (Fragment: constructor)
 
Approach 2 (cont.)
Back SMYC Forward

The code that was in the constructor can now be moved to the fromString() method (which can also be protected), and this method can return the StringTokenizer it used in case a derived class needs to use it.

javaexamples/oopbasics/stillbetterfinance/SecurityQuotation.java (Fragment: fromString)
 
Approach 2 (cont.)
Back SMYC Forward

The StockQuotation class now needs a public default constructor (because someone that wants to create a StockQuotation from a String will first need to construct an empty object and then call its fromString()).

javaexamples/oopbasics/stillbetterfinance/StockQuotation.java (Fragment: constructor)
 
Approach 2 (cont.)
Back SMYC Forward

The fromString() method is now elegant because it can use the same StringTokenizer that the fromString() method in the SecurityQuotation class used.

javaexamples/oopbasics/stillbetterfinance/StockQuotation.java (Fragment: fromString)
 
Approach 2 (cont.)
Back SMYC Forward

The FutureQuotation class also needs a public default constructor.

javaexamples/oopbasics/stillbetterfinance/FutureQuotation.java (Fragment: constructor)
 
Approach 2 (cont.)
Back SMYC Forward

The fromString() method is now elegant because it can use the same StringTokenizer that the fromString() method in the SecurityQuotation class used.

javaexamples/oopbasics/stillbetterfinance/FutureQuotation.java (Fragment: fromString)
 
Approach 2 (cont.)
Back SMYC Forward
  • Advantages:
    • The fromString() methods are both elegant
    • These classes can be further specialized if needed
  • Disadvantages:
    • Someone that wants to create an object from a String first has to use the default constructor and then call the fromString() method
Design 3
Back SMYC Forward
  • General Approach:
    • Improve on design 2
  • Specifics:
    • Add a factory method
Approach 3 (cont.)
Back SMYC Forward

The default constructor in the StockQuotation class is now protected.

javaexamples/oopbasics/bestfinance/StockQuotation.java (Fragment: constructor)
 
Approach 3 (cont.)
Back SMYC Forward

The fromString() method remains the same (though it could be made protected if desired) and a static factory method is added.

javaexamples/oopbasics/bestfinance/StockQuotation.java (Fragment: createInstance)
 
Approach 3 (cont.)
Back SMYC Forward

The default constructor in the FutureQuotation class is now protected.

javaexamples/oopbasics/bestfinance/FutureQuotation.java (Fragment: constructor)
 
Approach 3 (cont.)
Back SMYC Forward

The fromString() method remains the same (though it could be made protected if desired) and a static factory method is added.

javaexamples/oopbasics/bestfinance/FutureQuotation.java (Fragment: createInstance)
 
There's Always More to Learn
Back -