Design Patterns    |     Security    |     Testing    |     Distributed Computing    |     Contact

Builder Design Pattern, UML diagram, Java Example

             

Definition

Separates the construction of a complex object from its representation so that the same construction process can create different representations.

Class Diagram

Builder

Participants

  • Builder.
    • specifies an abstract interface for creating parts of a Product object
  • ConcreteBuilder.
    • constructs and assembles parts of the product by implementing the Builder interface
    • defines and keeps track of the representation it creates
    • provides an interface for retrieving the product
  • Director.
    • constructs an object using the Builder interface
  • Product.
    • represents the complex object under construction. ConcreteBuilder builds the product's internal representation and defines the process by which it's assembled
    • includes classes that define the constituent parts, including interfaces for assembling the parts into the final result

Example:

Example: Class Diagram

Builder Example

Example: Java sample code

    	package com.apwebco.patterns.gof.builder;
	// Builders
	public abstract class PromoKitBuilder {
		protected PromoKit promoKit = new PromoKit();
		public abstract void buildVideoPart();
		public abstract void buildGarmentPart();
		public abstract void buildBookPart();
		public abstract PromoKit getPromoKit();
	}
	public class MenPromoKitBuilder extends PromoKitBuilder {
		public void buildVideoPart() {
			// add videos to PromoKit based on men-specific preferences
		}
		public void buildGarmentPart() {
			// add men garments to PromoKit 
		}
		public void buildBookPart() {
			// add books to PromoKit based on men-specific preferences
		}
		public PromoKit getPromoKit() {
			return promoKit;
		}
	}
	public class WomenPromoKitBuilder extends PromoKitBuilder {
		public void buildVideoPart() {
			// add videos to PromoKit based on women-specific preferences
		}
		public void buildGarmentPart() {
			// add women garments to PromoKit
		}
		public void buildBookPart() {
			// add books to PromoKit based on women-specific preferences
		}
		public PromoKit getPromoKit() {
			return promoKit;
		}
	}
	// Director
	public class PromoKitDirector {
		public PromoKit createPromoKit(PromoKitBuilder builder) {
			builder.buildVideoPart();
			builder.buildGarmentPart();
			builder.buildBookPart();
			return builder.getPromoKit();
		}
	}
	// Integration with overal application
	public class Application {
		public static void main(String[] args) {
			String gendre = "M";
			PromoKitDirector director = new PromoKitDirector();
			PromoKitBuilder promoKitBuilder = null;

			if (gendre.equals("M")) {
				promoKitBuilder = new MenPromoKitBuilder();
			} else if (gendre.equals("F")) {
				promoKitBuilder = new WomenPromoKitBuilder();
			} else {
				// ....
			}
			PromoKit result = director.createPromoKit(promoKitBuilder);
		}
	}
    	

Benefits

  • When you want to vary product's internal representation.
  • Gives greater control over construction process.
  • Isolates code from construction and representation.

Usage

  • The algorithm for creating a complex object should be independent of both the parts that make up the object and how the parts are assembled.
  • The construction process must allow different representations of the constructed object.