10 Tips how to lose weight fast in 2023

10 Tips how to lose weight fast in 2023. Cut out sugary drinks and juices. These beverages are often high in calories and added sugars, which can contribute to weight gain….

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Builder Pattern in Java

It is a great pattern for handling the construction of objects that may conteains of lot of parameters,and we want to make object immutable once we constructing it.
When considering a builder you wanna focus on whether or not the construction of object are complex, complex means lots of parameters.

The concept of builder pattern
Handles complex constructor
Large number of parameters
Immutiblity
Examples:-
StringBuilder
DocumentBuilder
Locale.Builder

Design
Flexiblity over telescoping constructor
static inner class
the getters

Every day example-StringBuilder

public class BuilderDemo {
public static void main(String[] args){
StringBuilder builder= new StringBuilder();
builder.append(“This an example “);
builder.append(“of the builder pattern. “);
builder.append(“It has methods to append “);
builder.append(“almost anything we want to a string, “);
builder.append(“42 “)
System.out.println(bulder.toString());
}
}

Output “This is an example of builder pattern. It has methods to append almost anything we want to a string,42”

Creating Custom Builder Patten:=

package com.creational.pattern;

public class LunchOrder {
private final String bread;
private final String condiments;
private final String dressing;
private final String meat;

private LunchOrder(Builder builder) {
this.bread = builder.bread;
this.condiments = builder.condiments;
this.dressing = builder.dressing;
this.meat = builder.meat;
}

public static class Builder{
private String bread;
private String condiments;
private String dressing;
private String meat;

public Builder(){
}

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

public Builder bread(String bread) {
this.bread=bread;
return this;
}

public Builder condiments(String condiments) {
this.condiments=condiments;
return this;
}

public Builder dressing(String dressing) {
this.dressing=dressing;
return this;
}

public Builder meat(String meat) {
this.meat=meat;
return this;
}
}

public String getBread() {
return bread;
}

public String getCondiments() {
return condiments;
}

public String getDressing() {
return dressing;
}

public String getMeat() {
return meat;
}
}

Client Code

package com.creational.pattern;

public class BuilderLunchOrderDemo {
public static void main(String args[]) {
LunchOrder.Builder builder = new LunchOrder.Builder();
builder.bread(“wheat”).condiments(“Lettuce”).dressing(“mayo”).meat(“Turkey”);
LunchOrder lunchOrder=builder.build();

System.out.println(lunchOrder.getBread());
System.out.println(lunchOrder.getCondiments());
System.out.println(lunchOrder.getDressing());
System.out.println(lunchOrder.getMeat());
}
}

Add a comment

Related posts:

Porn!

Pornography. An evil and wretched device that is simultaneously one of the most popular forms of media we have ever created. It caters to every fantasy a person could have, no matter how obscure or…

Rick Sanchez and the Tragedy of Shy Poopers

Tucked away in the latest season of Rick and Morty is a profound question: To what lengths will you go to poop in peace? If you’re Rick Sanchez, the answer is setting up your own porcelain throne on…

What are third party risks in a Chinese context you should know about?

Third party vendors play a critical role is the ecosystem, however there are certain risk that the organizations need to manage when they engage with vendors. Third-party risks emanate from replying…