View RSS Feed

Design Patterns

Construction for Your Java Classes with Many Parameters

Rate this Entry
by , 11-29-2011 at 01:10 AM (1753 Views)
Traditionally when programmers have to create a constructor that has lots of parameters, some that are required and other that are optional, they often will either build several constructors or use the telescoping constructor pattern. For those of you not aware, the telescoping constructor pattern is where you provide constructors for the class with the required parameters, and then others constructors that cover the other optional parameters. So depending on the number of parameters and whether there is any level of grouping, you can end up with an extra constructor for every optional parameter.

Now, this works but is rather cumbersome as there are so many parameters to handle as well as making it difficult to read and understand. Also when you have lots of typed parameters it is quite easy to have very subtle but difficult to locate errors.

Java Code:
public class User {
    protected Long id;
    protected String username;                    // required
    protected String password;                    // required
    protected String confirmPassword;
    protected String firstName;                   // required
    protected String lastName;                    // required
    protected String company;                    // required
    protected String phoneNumber;
    protected String email;                       // required; unique
    protected String passwordHint;
    protected Integer version;
    protected boolean enabled;
    protected boolean accountExpired;
    protected boolean accountLocked;
    protected boolean credentialsExpired; 

	public User(String username, String password, String firstName,
			String lastName, String company, String phoneNumber, String email) {
		this.username = username;
		this.password = password;
		this.firstName = firstName;
		this.lastName = lastName;
		this.company = company;
		this.phoneNumber = phoneNumber;
		this.email = email;
	}


	public User(String username, String password, String firstName,
			String lastName, String company, String email) {
		this.username = username;
		this.password = password;
		this.firstName = firstName;
		this.lastName = lastName;
		this.company = company;
		this.email = email;
	}
}

Iíve only showed two constructors here, but I could easily create several more and note that there are several other parameters that I could have added for a security profile as well as more information about the user.

Now there is another way to construct a class that has numerous parameters is to use the JavaBeans pattern. In that pattern, you use a constructor without parameters and then call the setter methods for each parameter. This is quite easy to do but the main problem is that while you are calling the setters for all parameters, the object is in an incomplete state. It would be dangerous to use a class in this situation. Also you have the problem of if you are creating an immutable class, it is difficult to ensure that it is thread safe.

Now there is another pattern the is a combination of both the Telescoping and JavaBeans patterns. It is a version of the Builder pattern. In this pattern the builder is a static member class of the class that it builds. The client calls a static factory with the requisite arguments to get a builder object. The client then calls methods on the builder object in order to set all the required and optional parameters. Once completed, it then calls the a build method (that has no parameters) to create the object, which can be immutable.

Note that I have not shown the full class in order to save space, but it will be relatively easy for you to extend what is shown in the figure below for all of your optional fields.

Java Code:
public class User {
	protected Long id;
	protected String username; // required
	protected String password; // required
	protected String confirmPassword;
	protected String firstName; // required
	protected String lastName; // required
	protected String company; // required
	protected String phoneNumber;
	protected String email; // required; unique
	protected String passwordHint;
	protected Integer version;
	protected boolean enabled;

	public static class Builder {
		protected Long id;
		protected String username; // required
		protected String password; // required
		protected String confirmPassword;
		protected String firstName; // required
		protected String lastName; // required
		protected String company; // required
		protected String email; // required; unique
		protected Integer version;
		protected boolean enabled;

		public Builder(String username, String password, String firstName,
				String lastName, String company, String email) {
			this.username = username;
			this.password = password;
			this.firstName = firstName;
			this.lastName = lastName;
			this.company = company;
			this.email = email;
		}

		public Builder id(Long id) {
			this.id = id;
			return this;
		}

		public Builder username(String username) {
			this.username = username;
			return this;
		}

		public Builder password(String password) {
			this.password = password;
			return this;
		}

		public Builder confirmPassword(String confirmPassword) {
			this.confirmPassword = confirmPassword;
			return this;
		}

		public Builder firstName(String firstName) {
			this.firstName = firstName;
			return this;
		}
	}
}

Submit "Construction for Your Java Classes with Many Parameters" to Facebook Submit "Construction for Your Java Classes with Many Parameters" to Digg Submit "Construction for Your Java Classes with Many Parameters" to del.icio.us Submit "Construction for Your Java Classes with Many Parameters" to StumbleUpon Submit "Construction for Your Java Classes with Many Parameters" to Google

Updated 11-30-2011 at 02:06 PM by Design Patterns

Categories
Builder Pattern

Comments