Builder DesignPattern

Builder Design pattern is used when there is a requirement for creating complex objects.
if it is a simple object you can make use of factory design pattern.



Car.java
~~~~~~~~~
package com.anil.builder.design.
pattern;

public class Car {

        private String engine;
        private String tyre;
        private String paint;

        public String getEngine() {
                return engine;
        }

        public void setEngine(String engine) {
                this.engine = engine;
        }

        public String getTyre() {
                return tyre;
        }

        public void setTyre(String tyre) {
                this.tyre = tyre;
        }

        public String getPaint() {
                return paint;
        }

        public void setPaint(String paint) {
                this.paint = paint;
        }

}
----------------------------------------------------

CarBuilder.java
~~~~~~~~~~~~~~~

package com.anil.builder.design.pattern;

public interface CarBuilder {
        public String makeEngine();
        public String makeTyre();
        public String makePaint();
        public void makeCar();
        public Car getCar();
}
----------------------------------------------------------

USCarBuilder.java
~~~~~~~~~~~~~~~~~~

package com.anil.builder.design.pattern;

public class USCarBuilder implements CarBuilder {

        private Car car;

        @Override
        public String makeEngine() {
                // TODO Auto-generated method stub
                return "US Engine added ";
        }

        @Override
        public String makeTyre() {
                return "APPOLO tyres added";
        }

        @Override
        public String makePaint() {
                return "Red colour added";
        }

        @Override
        public void makeCar() {
                car=new Car();
                car.setEngine(makeEngine());
                car.setTyre(makeTyre());
                car.setPaint(makePaint());
        }

        @Override
        public Car getCar() {
                // TODO Auto-generated method stub
                return car;
        }

}
--------------------------------------------------------

GermanCarBuilder.java
~~~~~~~~~~~~~~~~~~~~~~

package com.anil.builder.design.pattern;

public class GermanCarBuilder implements CarBuilder {

private Car car;

        @Override
        public String makeEngine() {
                // TODO Auto-generated method stub
                return "German Engine";
        }

        @Override
        public String makeTyre() {
                return "German tyres";
        }

        @Override
        public String makePaint() {
                return "Blue";
        }

        @Override
        public void makeCar() {
                car=new Car();
                car.setEngine(makeEngine());
                car.setTyre(makeTyre());
                car.setPaint(makePaint());
        }

        @Override
        public Car getCar() {
                return car;
        }

}
----------------------------------------------------

Director.java
~~~~~~~~~~~~~
package com.anil.builder.design.pattern;

public class Director {

        private CarBuilder carBuilder;

        public Director(CarBuilder carBuilder) {
                super();
                this.carBuilder = carBuilder;
        }

        public CarBuilder getCarBuilder() {
                return carBuilder;
        }

        public void setCarBuilder(CarBuilder carBuilder) {
                this.carBuilder = carBuilder;
        }

        public void build() {
                carBuilder.makeCar();
        }
}
-----------------------------------------------------------
Test.java
~~~~~~~~~
package com.anil.builder.design.pattern;

public class Test {

        public static void main(String[] args) {
                CarBuilder builder=new USCarBuilder();
                Director director=new Director(builder);
                director.build();
                Car car=builder.getCar();
                System.out.println(" Car Engine : "+car.getEngine());
        }

 Pattern Definition
 Moves the Construction logic of the Object from the Object Class to Builder Class

Improves
  • Object Construction
  • Making new Changes
  • Modular Design

No comments:

Post a Comment