JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Publish Your Article Here

Builder design pattern in java


The builder pattern is an object creation design pattern.

The builder pattern is a design pattern that allows for the step-by-step creation of complex objects using the correct sequence of actions. The construction is controlled by a director object that only needs to know the type of object it is to create. The diagram shows the structure of the builder pattern

Below example shows each component from the above diagram as a seperate class. This sample example trying to create a camara object based on the customer needs.

Here is the code for product:

package com.java2novice.dp.builder.pattern;

public class Camara {

	private String opticalZoom;
	private String digitalZoom;
	private String displaySize;
	private String inMemorySize;
	
	public void setOpticalZoom(String opticalZoom) {
		this.opticalZoom = opticalZoom;
	}
	public void setDigitalZoom(String digitalZoom) {
		this.digitalZoom = digitalZoom;
	}
	public void setDisplaySize(String displaySize) {
		this.displaySize = displaySize;
	}
	public void setInMemorySize(String inMemorySize) {
		this.inMemorySize = inMemorySize;
	}
}

Code for abstract builder:

package com.java2novice.dp.builder.pattern;

public abstract class CamaraBuilder {

	protected Camara cam;
	
	public Camara getCamara(){
		return this.cam;
	}
	
	public void createNewCamara(){
		this.cam = new Camara();
	}
	
	public abstract void buildOpticalZome();
	public abstract void buildDigitalZome();
	public abstract void buildDisplay();
	public abstract void buildMemory();
}

Code for concrete builder:

package com.java2novice.dp.builder.pattern;

public class OpticalZoomCamaraBuilder extends CamaraBuilder {

	@Override
	public void buildOpticalZome() {
		this.cam.setOpticalZoom("50X Optical Zoom");
	}

	@Override
	public void buildDigitalZome() {
		this.cam.setDigitalZoom("None");
	}

	@Override
	public void buildDisplay() {
		this.cam.setDisplaySize("6 inch LED screen with touch functionality");
	}

	@Override
	public void buildMemory() {
		this.cam.setInMemorySize("32GB inbuilt memory");
	}
}

Code for concrete builder:

package com.java2novice.dp.builder.pattern;

public class DigitalZoomCamaraBuilder extends CamaraBuilder {

	@Override
	public void buildOpticalZome() {
		this.cam.setOpticalZoom("None");
	}

	@Override
	public void buildDigitalZome() {
		this.cam.setDigitalZoom("10X Optical Zoom");
	}

	@Override
	public void buildDisplay() {
		this.cam.setDisplaySize("4 inch LED screen");
	}

	@Override
	public void buildMemory() {
		this.cam.setInMemorySize("16GB inbuilt memory");
	}
}

Code for Director:

package com.java2novice.dp.builder.pattern;

public class CamaraMaker {

	private CamaraBuilder camBuilder = null;
	
	public void setCamaraBuilder(CamaraBuilder camBuilder){
		this.camBuilder = camBuilder;
	}
	
	public Camara getCamara(){
		return this.camBuilder.getCamara();
	}
	
	public void constructCamara(){
		this.camBuilder.createNewCamara();
		this.camBuilder.buildDigitalZome();
		this.camBuilder.buildOpticalZome();
		this.camBuilder.buildDisplay();
		this.camBuilder.buildMemory();
	}
}

Code for ordering camara:

package com.java2novice.dp.builder.pattern;

public class CamaraOnlineSeller {

	public static void main(String a[]){
		CamaraMaker cm = new CamaraMaker();
		System.out.println("Creating camara with optical zoom...");
		CamaraBuilder ozcb = new OpticalZoomCamaraBuilder();
		cm.setCamaraBuilder(ozcb);
		cm.constructCamara();
		Camara cam = cm.getCamara();
	}
}

Output:
Creating camara with optical zoom...
<< Previous Program | Next Program >>
blog comments powered by Disqus

Java design pattern examples

  1. Factory Pattern
  2. Abstract Factory Pattern
  3. Builder Design Pattern
  4. Prototype Pattern
  5. Adapter Pattern
  6. Composite Pattern
  7. Proxy Pattern
Knowledge Centre
Prevent a method from being overridden
By specifying final keyword to the method you can avoid overriding in a subcalss. Similarlly one can use final at class level to prevent creating subclasses.
Famous Quotations
The pessimist complains about the wind; the optimist expects it to change; the realist adjusts the sails.
-- William Arthur Ward

About Author

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2017 by Nataraja Gootooru. All Rights Reserved.