Help the world stop coronavirus! Stay home!

Prev Next

Java / Design Patterns

Explain builder pattern in Java.

Builder pattern combines the safety of the telescoping constructor pattern with the readability of the JavaBeans pattern.

To create the desired object, the client first calls a constructor (or static factory) with all of the required parameters and gets a builder object. Then the client calls setter-like methods on the builder object to set each optional parameter of interest. Finally, the client calls a parameter-less build method to generate the object, which is immutable.

The builder is a static member class of the class it builds.

public class BuilderPatternEx {

	static class Person {
		private String fName;
		private String lName;
		private short age;
		private String employerName;

		public static class PersonBuilder {
			private String fName;
			private String lName;
			private short age;
			private String employerName;

			public PersonBuilder(String fName, String lName) {
				this.fName = fName;
				this.lName = lName;
			}

			public PersonBuilder age(short age) {
				this.age = age;
				return this;
			}

			// The builder's setter methods return the builder itself so that
			// invocations can be chained.
			public PersonBuilder employerName(String employerName) {
				this.employerName = employerName;
				return this;
			}

			public Person build() {
				return new Person(this);
			}
		}

		Person(PersonBuilder p) {
			this.fName = p.fName;
			this.lName = p.lName;
			this.age = p.age;
			this.employerName = p.employerName;

		}

		@Override
		public String toString() {
			StringBuilder temp = new StringBuilder("Peron details: ");
			temp.append(this.fName).append(" ").append(this.lName).append(" of age ").append(this.age);

			return temp.toString();
		}

	}

	public static void main(String[] args) {

		Person p = new Person.PersonBuilder("John", "Jacob").age((short) 45).employerName("Apple").build();

		System.out.println(p);
	}
}

❤Cash Back At Stores you Love !!!❤

Earn your $10 reward when you make your first purchase through Ebates by signing up with clicking below button.

Ebates Coupons and Cash Back

More Related questions...

What is design pattern in Java? What is Observer design pattern in Java? Name few design patterns used in standard JDK library in Java. Explain strategy design pattern in Java. Difference between Strategy and State design Pattern in Java. Singleton pattern. How to implement a singleton pattern? Why do we need Singleton class? (or) what are the advantages of using singleton pattern? What is Decorator pattern in Java? Give an example of Decorator pattern in Java. Difference between Decorator and Proxy pattern in Java. When to use Setter and Constructor Injection in Dependency Injection pattern? Explain Composite design Pattern in Java. Explain Front controller design pattern in Java/J2ee. Explain MVC design pattern in J2EE. How do you implement a thread-safe Singleton in Java? What is the difference between factory and abstract factory pattern in Java? Is Decorator an EJB design pattern in Java? Explain Facade design pattern. Types of Java Design patterns. Name few Creational Patterns in Java. Name some of the structural design patterns in Java. Name some of the Behavioral design patterns in Java. Explain Builder Design Pattern. Explain Factory pattern in Java. Difference between Service Provider Interface (SPI) and Application Programming Interface (API). Explain Service Provider framework pattern. Explain telescopic constructor pattern in Java. Explain Java beans pattern. Disadvantages of Java beans pattern. Explain builder pattern in Java. Disadvantage of Builder pattern. When to use Builder design pattern? In Java singleton, how do you prevent creating instance using reflection? How do we ensure Singleton Design Pattern in a clustered Environment? Difference between Singleton Pattern and static Class in Java. Give example of singleton class from JDK. When to use Static Class in place of Singleton in Java? Explain Bridge design pattern in Java. Explain prototype pattern in Java. Explain Template method design Pattern in Java. What is filter pattern in Java? Explain Proxy design pattern in Java. What is Adapter pattern in Java? Difference between the Bridge and Adapter pattern in Java. What is Chain of Responsibility Pattern in Java? Explain Intercepting Filter pattern in Java. Disadvantage of using factory pattern for object creation. Which pattern provides greater abstraction than factory pattern? Disadvantages of the singleton pattern. Which classes are candidates of Singleton? What is double checked locking in Singleton? How to prevent creating another instance of Singleton using clone() method? How to prevent creating another instance of Singleton during serialization? Explain mediator pattern in Java. Give few examples of Mediator Pattern in JDK. Explain Command design pattern in Java. What is State Design Pattern in Java? When cloning an object is preferred over creating a new object in Java? Name some of the design patterns that makes use of singleton pattern. Explain Memento pattern in Java. When do you use memento design pattern ? Explain visitor pattern in Java. What is value object design pattern? What is Transfer object design pattern in Java? Explain Service Locator design pattern in Java. Mention some of the core J2EE design patterns. Difference between Front Controller and View Helper in J2EE. Advantages of Factory method design pattern. What is a monolithic architecture? Is template method pattern an example of Inversion of control? Give an example of Template method pattern from JDK. Disadvantages of Immutable class. Types of messaging patterns. Difference between filter and interceptor patterns in Spring MVC.
Show more question and Answers...

Generics

Comments & Discussions