Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8683133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:57:40+00:00 2026-06-12T21:57:40+00:00

I am learning about the builder pattern, and so far I understood that, it

  • 0

I am learning about the builder pattern, and so far I understood that, it is a great alternative to the commonly patterns used for initialization:

  • Telescoping Constructor Pattern

  • JavaBean Pattern

The thing is, I don’t really like to remove the getters and setters from the objects in my domain model. I always like to keep them as POJOs. One of the reasons I don’t like it is:
If i don’t use POJOs, then it is not easy to annotate the variables when using ORM frameworks…

So here are my doubts:
-Is it possible to implement the builder pattern without using static inner classes?
-If I have to use the builder pattern by using the inner class, do you think it is correct to keep the getters and the setters?
-I did a little example for practice where I tried to avoid the inner class.
Could you let me what do you think about it?

Product

    public class Product
{
    private String color;
    private int price;

    public Product() {
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public String toString() {
        return getColor() + "\n" + getPrice();
    }    
}

Builder

public class Builder
{
    private Product product;

    public Builder() {
        product = new Product();
    }
    public Builder withColor(String color) {        
        product.setColor(color);
        return this;
    }

     public Builder withPrice(int price) {        
        product.setPrice(price);
        return this;
    }
    public Product build() {
        return product;
    }
}**

Client

public class Client
{

    public static void main(String[] args) {
        System.out.println(new Builder().withColor("Black").withPrice(11).build());
        System.out.println("-----------------------------------------------------");
        System.out.println(new Builder().withColor("Blue").withPrice(12).build());
    }
}

enter image description here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T21:57:41+00:00Added an answer on June 12, 2026 at 9:57 pm

    The Builder pattern is useful to create immutable objects and avoid several constructors with optional parameters.

    IMO using Builder pattern to build a POJO which can be updated using setters is useless. You only create an additional class.

    Depending on the ORM framework used, it might not need the presence of setter method. But only assigning members values through reflection.

    Product class:

    public final class Product {
        private final String color;
        private final int price;
    
        public Product(Builder builder) {
            this.color = builder.getColor();
            this.price = builder.getPrice();
        }
    
        public String getColor() {
            return color;
        }
    
        public int getPrice() {
            return price;
        }
    
        public String toString() {
            return getColor() + "\n" + getPrice();
        }    
    }
    

    Builder class:

    public final class Builder {
    
        private String color;
        private int price;
    
        public Builder() {
            // Assign any default values
        }
    
        public Builder color(String color) {        
            this.color = color;
            return this;
        }
    
        public Builder price(int price) {        
            this.price = price;
            return this;
        }
    
        protected String getColor() {
            return color;
        }
    
        protected int getPrice() {
            return price;
        }
    
        public Product build() {
            return new Product(this);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm learning about Design Patterns and found the Builder design pattern. What are the
Just learning about mercurial's --style and --template options that can be used on hg
I am learning about raw sockets. I heard that ping utility is using raw
I am learning about JS Prototype. If I set a prototype of a constructor(A)
In learning about Core Data, I've noticed how (in Xcode's templates) Apple directly used
In learning about Web Deploy I came across some netsh.exe commands that talk about
I am about to start learning coding the GUI. Now I know that its
Learning about ARM NEON intrinsics, I was timing a function that I wrote to
I'm learning about fractal tree indices such as that found in TokuDB . I
I've been learning about C++ AMP, and everything I've seen stresses that AMP works

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.