Alexey Zvolinskiy

About Alexey Zvolinskiy

Alexey is a test developer with solid experience in automation of web-applications using Java, TestNG and Selenium. He is so much into QA that even after work he provides training courses for junior QA engineers.

Design Patterns: Builder

Sometimes there is a need to create a complex object in application. The one solution for this is a Factory pattern, the another one is a Builder design pattern. In some situation you even can combine these two patterns. But in this article I want to examine the Builder design pattern. The first thing which I need to say that it is a creational pattern.java-design-patterns In what situations you should use the Builder design pattern? Definitely when creation of the object requires a plenty of another independent objects. When you want to hide creation process from the user. When you can have different representation of the object in the end of the construction process.

Let’s proceed with a code example. UML scheme of the pattern:

builder-design-pattern-java

As I mentioned the Builder pattern is the creational pattern. This circumstance implies creation of some object (product) in the end of the process. The product is created with the help of a concrete builder, in its turn the builder has some parent builder class or interface. The final point of the pattern is a director class, it is responsible for creation of the concrete builder for the appropriate product.

The example will be based on famous and epic computer game – StarCraft. A role of the product will play a Zealot it is a simple battle Protoss unit. The director’s role will play a Gateway. And the concrete builder is a ZealotBuilder. All code I will provide below:

Abstract class for the game units:

public abstract class Unit {

	protected int hitPoints;
	protected int armor;
	protected int damage;

	public int getHitPoints() {
		return hitPoints;
	}

	public void setHitPoints(int hitPoints) {
		this.hitPoints = hitPoints;
	}

	public int getArmor() {
		return armor;
	}

	public void setArmor(int armor) {
		this.armor = armor;
	}

	public int getDamage() {
		return damage;
	}

	public void setDamage(int damage) {
		this.damage = damage;
	}

}

Class of the Zealot (product):

public class Zealot extends Unit {

	public String toString() {
		return "Zealot is ready!"+
				"\nHitPoints: "+getHitPoints()+
				"\nArmor: "+getArmor()+
				"\nDamage: "+getDamage();
	}

}

Interface of the builder:

public interface UnitBuilder {

	public void buildHitPoints();
	public void buildArmor();
	public void buildDamage();
	public Unit getUnit();

}

Implementation of the builder interface:

public class ZealotBuilder implements UnitBuilder {

	private Unit unit;

	public ZealotBuilder() {
		unit = new Zealot();
	}

	@Override
	public void buildHitPoints() {
		unit.setHitPoints(100);
	}

	@Override
	public void buildArmor() {
		unit.setArmor(50);
	}

	@Override
	public void buildDamage() {
		unit.setDamage(8);
	}

	@Override
	public Unit getUnit() {
		return unit;
	}

}

The Gateway (Director) class:

public class Gateway {

	public Unit constructUnit(UnitBuilder builder) {
		builder.buildHitPoints();
		builder.buildArmor();
		builder.buildDamage();
		return builder.getUnit();
	}

}

And now let’s see how it works all together:

...
	public static void main(String[] args) {

		UnitBuilder builder = new ZealotBuilder();
		Gateway director = new Gateway();
		Unit product = director.constructUnit(builder);
		System.out.println(product);

	}
...

The result of the last code snippet is:

Zealot is ready!
HitPoints: 100
Armor: 50
Damage: 8

So as you can see the Builder design pattern is really helpful in situation when you need to create complex objects. The example in the tutorial wasn’t really hard, but now you can imagine in what situation you can apply this approach. More articles about the design patterns you can find here.
 

Reference: Design Patterns: Builder from our JCG partner Alexey Zvolinskiy at the Fruzenshtein’s notes blog.
Related Whitepaper:

Bulletproof Java Code: A Practical Strategy for Developing Functional, Reliable, and Secure Java Code

Use Java? If you do, you know that Java software can be used to drive application logic of Web services or Web applications. Perhaps you use it for desktop applications? Or, embedded devices? Whatever your use of Java code, functional errors are the enemy!

To combat this enemy, your team might already perform functional testing. Even so, you're taking significant risks if you have not yet implemented a comprehensive team-wide quality management strategy. Such a strategy alleviates reliability, security, and performance problems to ensure that your code is free of functionality errors.Read this article to learn about this simple four-step strategy that is proven to make Java code more reliable, more secure, and easier to maintain.

Get it Now!  

Leave a Reply


− six = 3



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books