Builder Design Pattern
Unlocking Flexibility: Understanding the Builder Design Pattern

Builder Design Pattern

Alice and Bob find themselves at a construction site once again.

Alice : Hi Bob, what are you doing in this construction site?

Bob : This is our building, it is in the construction stage.

Alice: Oh! by the way, I newly learned about the Builder Design Pattern.

Bob : What is the purpose of this design pattern?

Alice: Let's take building construction as an example. If you want a 3Bhk with a 1600 Sqft area along with a garden, you can give your requirements to your contractor. The interesting part is that you can provide the requirements in any order, and the contractor is responsible for planning accordingly to fulfill them. Instead of ordering the parameters, we can create another class HouseBuilder. It will be responsible for assigning requirements to the House irrespective of the order.

class House {
    private int noOfRooms;
    private int areaSqft;
    private boolean haveGarden;

    private House(HouseBuilder builder) {
        this.noOfRooms = builder.noOfRooms;
        this.areaSqft = builder.areaSqft;
        this.haveGarden = builder.haveGarden;
    }

    public static class HouseBuilder {
        private int noOfRooms;
        private int areaSqft;
        private boolean haveGarden;

        public HouseBuilder rooms(int noOfRooms) {
            this.noOfRooms = noOfRooms;
            return this;
        }

        public HouseBuilder area(int areaSqft) {
            this.areaSqft = areaSqft;
            return this;
        }

        public HouseBuilder garden(boolean haveGarden) {
            this.haveGarden = haveGarden;
            return this;
        }

        public House build() {
            return new House(this);
        }
    }
}        
class Main {
    /**
     * Demonstrates the usage of the Builder pattern to create a House object with flexible parameter order.
     */
    public static void main(String[] args) {
        // Create a House object with Builder
        House house = new House.HouseBuilder()
                               .area(1600)
                               .rooms(3)
                               .garden(true)
                               .build();
    }
}        

In this example, inside House, there is a class HouseBuilder. The methods of HouseBuilder set values to its parameters, and the build() method will return a House object by initializing the values through the parameterized constructor House(HouseBuilder builder), which takes the HouseBuilder object as input. In the main() function, we can see that the requirements can be given in any order, and the Builder will assign them to their respective parameters.

Bob : It's a really useful pattern, so that it is not necessary to maintain parameters order while assigning values.

Alice : Not only for simple parameters, but we can also construct a complex object from simple objects along with encapsulating code for construction and representation.

Bob : I'll apply this concept in my project. Thanks for introducing me to a new design pattern.

Alice : You're welcome, Bob. Alright, it's high time; I am going home for Mortal Kombat. You can also join.

Bob : Yeah, I will be there in one hour.

Alice : Ok, let's fight in Mortal Kombat.

Alice and Bob left the construction site, looking forward to their Mortal Kombat match, and they will meet again at Alice's house...


Until then signing off with ? Ganesh

要查看或添加评论,请登录

Ganesh Manchi的更多文章

  • Facade Design Pattern

    Facade Design Pattern

    Alice and Bob are watching a movie at Bob's place, enjoying the latest action flick on Bob's impressive home theater…

  • Factory Design Pattern

    Factory Design Pattern

    One sunny morning, Alice and Bob met at a bustling factory. Alice: Hi Bob, what brings you to this factory today? Bob:…

  • Decorator Design Pattern

    Decorator Design Pattern

    One sunny afternoon, Alice and Bob found themselves at their favorite coffee shop. Alice: Hi Bob, how's your day going?…

    2 条评论
  • Observer Design Pattern

    Observer Design Pattern

    One sunny afternoon, Alice and Bob decided to meet at their favorite park. Alice: Hi Bob, how's everything going? Bob:…

  • Strategy Design Pattern

    Strategy Design Pattern

    Bob came to Alice's house, and they have been playing Mortal Kombat for two hours. Alice: Hey Bob, we've played a lot…

  • Singleton Design Pattern

    Singleton Design Pattern

    One day, Alice and Bob meet at a coffee shop. Alice : Hi Bob, how are you doing? Bob : I'm good.

    2 条评论
  • Coding culture in colleges :(

    Coding culture in colleges :(

    Hello, World! Note: If your college has a good coding culture, then skip this article. I want to share what I've…

    5 条评论
  • DSA vs CP

    DSA vs CP

    Hello World! One of the most confusing topics that often crosses our minds is the choice between Data Structures and…

社区洞察

其他会员也浏览了