Java Builder Pattern: Want to Avoid Getters and Setters to Create an Object?

Have you ever get bored of setting lots of properties to create an object? Have you ever considered an alternate way to create an object? Well, I got one. It’s called Builder Pattern. It’s another way to create an object. I’m trying to make this as easier as possible but I will need to be technical as well. So, here is a simple of builder diagram.

Builder Pattern

Builder Pattern Diagram

Start from Builder
Builder is the abstract class where you form a skeleton of a object you want to create; in other words, a base class for every concreate builder.

Concrete Builder
Concrete Builder classes are extended from the Builder.

Director
The Director class is to control everything and responsible to control the sequence of object creation.

Product
Product can be seen as the model class.

However, it will be easier if you look at the example below.
First let’s create a Product class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
public class Car
{
	private String name;
	private String description;
	private String model;

	/**
	 * Get the name
	 *
	 * @return the name
	 */
	public String getName()
	{
		return name;
	}

	/**
	 * Set the name
	 *
	 * @param name the name to set
	 */
	public void setName(String name)
	{
		this.name = name;
	}

	/**
	 * Get the description
	 *
	 * @return the description
	 */
	public String getDescription()
	{
		return description;
	}

	/**
	 * Set the description
	 *
	 * @param description the description to set
	 */
	public void setDescription(String description)
	{
		this.description = description;
	}

	/**
	 * Get the model
	 *
	 * @return the model
	 */
	public String getModel()
	{
		return model;
	}

	/**
	 * Set the model
	 *
	 * @param model the model to set
	 */
	public void setModel(String model)
	{
		this.model = model;
	}
}

Then we create an abstract class to be a base class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public abstract class FactoryAbstract
{
	protected Car car;

	public void createACar()
	{
		this.car = new Car();
	}

	public Car getCar()
	{
		return this.car;
	}

	public abstract void buildName();
	public abstract void buildDescription();
	public abstract void buildModel();
}

Then, create a concrete class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class ToyotaCreator extends FactoryAbstract
{

	@Override
	public void buildDescription()
	{
		car.setDescription("Toyota hybrid-power");
	}

	@Override
	public void buildModel()
	{
		car.setModel("Prius");
	}

	@Override
	public void buildName()
	{
		car.setName("Toyota");
	}
}

The last one is the Director

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class Director
{
	private FactoryAbstract factory;

	public void setConcreteClass( FactoryAbstract factoryAbstract )
	{
		this.factory = factoryAbstract;
	}

	public void constructACar()
	{
		factory.createACar();
		factory.buildName();
		factory.buildDescription();
		factory.buildModel();
	}

	public Car getCar()
	{
		return factory.getCar();
	}
}

And a client to test the pattern

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class Client
{

	/**
	 * @param args
	 */
	public static void main(String[] args)
	{
		FactoryAbstract factory = new ToyotaCreator();
		Director director = new Director();
		director.setConcreteClass( factory );
		director.constructACar();

		Car aCar = director.getCar();

		System.out.println( "Name: " + aCar.getName() );
		System.out.println( "Description: " + aCar.getDescription() );
		System.out.println( "Model: " + aCar.getModel() );
	}

}
Feb 6th, 2011

Comments