Builder pattern with java example



                Builder pattern is one of the Creational pattern that solves the complex object creation model.

 

Problem solved by builder pattern

         When we have too much of optional parameter to construct an object, the code become less readable.

             Building an object with different combination of data may confuse which one to use.


Problem in creating Object

Example:

                Create a Computer object based on different configuration. Assume we have three different set of combination to build a Computer object.

package com.candidjava;

public class Computer {
	String boardType;
	String processor;
	String ram;
	String graphicsCard;
	String hardDisk;
	String keyboard;
	String mouse;

	Computer(String boardType, String processor, String ram, String gCard,
			String hardDisk, String keyboard, String mouse) {
		// Assign to member variable
	}

	Computer(String boardType, String processor, String ram, String hardDisk,
			String keyboard, String mouse) {
		// Assign to member variable
	}

	Computer(String boardType, String processor, String ram, String hardDisk) {
		// Assign to member variable
	}

}

package com.candidjava;

public class Main {

	public static void main(String[] args) {
		// the below object creation looks less readable
		Computer c = new Computer("Intel", "i5", "2GB", "1TB");

		Computer c2 = new Computer("ASUS", "AMD", "4GB", "500GB", "Intex",
				"Dell");
	}
}
 

Solution to the above problem

                Give some importance to access specifiers also while building an Object using builder pattern.

 package com.candidjava.builder;

public class Computer {
	private String boardType;
	private String processor;
	private String ram;
	private String graphicsCard;
	private String hardDisk;
	private String keyboard;
	private String mouse;

	public static Builder getBuilder() {
		return new Builder();
	}

	public static class Builder {
		private String boardType;
		private String processor;
		private String ram;
		private String graphicsCard;
		private String hardDisk;
		private String keyboard;
		private String mouse;

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

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

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

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

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

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

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

		public Computer build() {
			if (vaildate(this) == false) {
				throw new IllegalArgumentException(
						"Invalid data input to create Computer Object");
			}

			return new Computer(this);
		}

		private boolean vaildate(Builder builder) {

			// add validation stuf here
			return true;
		}

	}

	private Computer(Builder builder) {
		this.boardType = builder.boardType;
		this.graphicsCard = builder.graphicsCard;
		this.hardDisk = builder.hardDisk;
		this.keyboard = builder.keyboard;
		this.mouse = builder.mouse;
		this.processor = builder.processor;
		this.ram = builder.ram;
	}

	public void print() {
		System.out.println(boardType);
		System.out.println(graphicsCard);
		System.out.println(hardDisk);
		System.out.println(keyboard);
		System.out.println(mouse);
		System.out.println(processor);
		System.out.println(ram);

	}
}
 

Building Computer instance using builder pattern

 package com.candidjava.builder;

public class Main {

	public static void main(String[] args) {
		
		Computer c1=Computer.getBuilder()
					.setBoardType("Intel")
					.setGraphicsCard("G3")
					.setHardDisk("200GB")
					.setKeyboard("Intex")
					.setMouse("Dell")
					.setProcessor("i3")
					.setRam("4GB")
					.build();
		c1.print();
	}
}
 

        If you are familiar with ORM framework you can find builder pattern for example in hibernate Criteria object will be created with different combination of queries.


 List c = s.createCriteria(Employee.class)

.add(Restrictions.between("age", 20, 50))

.add(Restrictions.ge("salary", new Integer(30000)))

.add(Restrictions.le("salary", new Integer(50000)))

.add(Restrictions.in("city", new String[] { "Chennai", "Erode" }))

        .add(Restrictions.eq("gender", "MALE"))

                        .list();


 

Drawback of Builder pattern

1.       Duplicating member variable again in nested builder class




Related Post

Comments


©candidjava.com