Over a million developers have joined DZone.

NetBeans 7.2: Refactoring Constructor As Static Factory

· Java Zone

What every Java engineer should know about microservices: Reactive Microservices Architecture.  Brought to you in partnership with Lightbend.

In the post NetBeans 7.2: Refactoring Parameterized Constructor As Builder, I looked at how NetBeans 7.2 supports refactoring a constructor to a builder as described in Item 2 of the Second Edition of Effective Java. In this post, I look at how NetBeans 7.2 similarly supports refactoring constructors to factories as described in Item #1 of Effective Java.

I start with an Employee class that is largely the same as the one I used in my previous post on refactoring a constructor to a builder. There are some minor differences, the most significant being that the attributes of the version used here cannot be final as the factory initializer approach does not allow final class attributes because they are not set in the constructor with this approach.

Employee.java with Traditional Constructor

package dustin.examples;

/**
 * Simple employee class intended to illustrate NetBeans 7.2 and refactoring
 * constructor to use Factory initializer as discussed in Item #1 of Joshua
 * Bloch's <em>Effective Java</em>.
 * 
 * @author Dustin
 */
public class Employee 
{
   private String lastName;

   private String middleName;

   private String firstName;

   private long id;

   private int birthYear;

   private int birthMonth;

   private int birthDate;

   private int hireYear;

   private int hireMonth;

   private int hireDate;

   public Employee(
      final String newLastName,
      final String newMiddleName,
      final String newFirstName,
      final long newId,
      final int newBirthYear,
      final int newBirthMonth,
      final int newBirthDate,
      final int newHireYear,
      final int newHireMonth,
      final int newHireDate)
   {
      this.lastName = newLastName;
      this.middleName = newMiddleName;
      this.firstName = newFirstName;
      this.id = newId;
      this.birthYear = newBirthYear;
      this.birthMonth = newBirthMonth;
      this.birthDate = newBirthDate;
      this.hireYear = newHireYear;
      this.hireMonth = newHireMonth;
      this.hireDate = newHireDate;
   }

   @Override
   public String toString()
   {
      return  this.firstName + " " + this.middleName + " " + this.lastName
            + " with ID " + this.id;
   }
}

Right-clicking on the constructor of this class leads to the choices for refactoring including the highlighted choice to "Replace Constructor with Factory..."

When the "Refactor: Replace Constructor with Factory..." choice is selected, a window like that shown in the next screen snapshot appears. It has the name "create" for the factory method by default.

One of the advantages of these static initialization factories over constructors is the ability to name them as we like rather than always needing to use the class name. This can lead to them being more expressive and means potentially less reliance on overloading. The next screen snapshot shows my change of the name of the method to be generated.

When the above options are chosen, the class's source code is automatically changed to the code shown in the next code listing. Note that the constructor is changed from public to private and a new newInstance method is generated.

Refactored Employee.java (with White Space Adjustments Made Manually)

package dustin.examples;

/**
 * Simple employee class intended to illustrate NetBeans 7.2 and refactoring
 * constructor to use Factory initializer as discussed in Item #1 of Joshua
 * Bloch's <em>Effective Java</em>.
 * 
 * @author Dustin
 */
public class Employee 
{
   public static Employee newInstance(
      final String newLastName, final String newMiddleName,
      final String newFirstName, final long newId,
      final int newBirthYear, final int newBirthMonth,
      final int newBirthDate, final int newHireYear,
      final int newHireMonth, final int newHireDate)
   {
      return new Employee(newLastName, newMiddleName, newFirstName,
                          newId, newBirthYear, newBirthMonth, newBirthDate,
                          newHireYear, newHireMonth, newHireDate);
   }
   private final String lastName;

   private final String middleName;

   private final String firstName;

   private final long id;

   private final int birthYear;

   private final int birthMonth;

   private final int birthDate;

   private final int hireYear;

   private final int hireMonth;

   private final int hireDate;

   private Employee(
      final String newLastName,
      final String newMiddleName,
      final String newFirstName,
      final long newId,
      final int newBirthYear,
      final int newBirthMonth,
      final int newBirthDate,
      final int newHireYear,
      final int newHireMonth,
      final int newHireDate)
   {
      this.lastName = newLastName;
      this.middleName = newMiddleName;
      this.firstName = newFirstName;
      this.id = newId;
      this.birthYear = newBirthYear;
      this.birthMonth = newBirthMonth;
      this.birthDate = newBirthDate;
      this.hireYear = newHireYear;
      this.hireMonth = newHireMonth;
      this.hireDate = newHireDate;
   }

   @Override
   public String toString()
   {
      return  this.firstName + " " + this.middleName + " " + this.lastName
            + " with ID " + this.id;
   }
}

There are multiple advantages to using static initialization factory methods rather than constructors. These include the ability to provide different and more meaningful names that do not necessarily require overloading because they can be different. I like to name these methods in such a way that some of their parameters are implied, resulting in the need to provide fewer arguments to that method than would be necessary for a constructor.

Other advantages of the static factory approach of acquiring an instance and two disadvantages of this approach are discussed in Item #1 of Effective Java. That item concludes, "Often static factories are preferable, so avoid the reflex to provide public constructors without first considering static factories." NetBeans 7.2 makes it easy to see how a constructor can be refactored into a static factory and will even do the basic work for the developer.

 

 

Microservices for Java, explained. Revitalize your legacy systems (and your career) with Reactive Microservices Architecture, a free O'Reilly book. Brought to you in partnership with Lightbend.

Topics:

Published at DZone with permission of Dustin Marx, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}