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 >>

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
What is fail-fast in java?
A fail-fast system is nothing but immediately report any failure that is likely to lead to failure. When a problem occurs, a fail-fast system fails immediately. In Java, we can find this behavior with iterators. Incase, you have called iterator on a collection object, and another thread tries to modify the collection object, then concurrent modification exception will be thrown. This is called fail-fast.
Famous Quotations
It’s not that I’m so smart, it’s just that I stay with problems longer.
-- Albert Einstein

About Author

I'm Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment.

If you come across any mistakes or bugs, please email me to [email protected] or you can comment on the page.

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.