Thursday, October 30, 2014

Builder Pattern

CreationalPattern is basically used for creating object with different parameters (some of them may be mandatory and some of them may be optional).Suppose we need to know all required materials for a Cake preparation. But this cake may be veg or non-veg. Now these two types of cake again can be divided with sugerfree or withsuger. So, using Builder Pattern, we can builds a complex object using simple objects and using a step by step approach.
------------------------------------------------------------
package com.exmpl.CreationalPattern;

public interface CakePreparation {
       // Mandatory and optional required materials for cake preparation.
       public void milkRequiredOrNot();

       public void eggRequiredOrNot();

       public void fruitRequiredOrNot();

       public void sugerRequiredOrNot();
}
------------------------------------------------------------
package com.exmpl.CreationalPattern;

public abstract class NonVegCake implements CakePreparation {

       @Override
       public void milkRequiredOrNot() {
              System.out.println("Milk is requird.");
       }

       @Override
       public void eggRequiredOrNot() {
              System.out.println("Egg is requird.");
       }

       @Override
       public void fruitRequiredOrNot() {
              System.out.println("Fruit is not requird.");
       }

       @Override
       public abstract void sugerRequiredOrNot();
}
------------------------------------------------------------
package com.exmpl.CreationalPattern;

public class SugerFreeNonVegCake extends NonVegCake {

       @Override
       public void sugerRequiredOrNot() {
              System.out.println("Suger is not requird.");
       }

}
------------------------------------------------------------
package com.exmpl.CreationalPattern;

public class WithSugerNonVegCake extends NonVegCake{

       @Override
       public void sugerRequiredOrNot() {
              System.out.println("Suger is requird.");
       }

}
------------------------------------------------------------
package com.exmpl.CreationalPattern;

public abstract class VegCake implements CakePreparation {

       @Override
       public void milkRequiredOrNot() {
              System.out.println("Milk is requird.");
       }

       @Override
       public void eggRequiredOrNot() {
              System.out.println("Egg is not requird.");
       }

       @Override
       public void fruitRequiredOrNot() {
              System.out.println("Fruit is requird.");
       }

       @Override
       public abstract void sugerRequiredOrNot();

}
------------------------------------------------------------
package com.exmpl.CreationalPattern;

public class SugerFreeVegCake extends VegCake{

       @Override
       public void sugerRequiredOrNot() {
              System.out.println("Suger is not requird.");
       }
}
------------------------------------------------------------
package com.exmpl.CreationalPattern;

public class WithSugerVegCake extends VegCake {

       @Override
       public void sugerRequiredOrNot() {
              System.out.println("Suger is requird.");
       }
}
------------------------------------------------------------
package com.exmpl.CreationalPattern;

public class ClientClass {
       public static void main(String args[]) {
              ClientClass obj = new ClientClass();

              // Check material requirement for SugerFreeNonVegCake.
              System.out.println("Check material requirement for SugerFreeNonVegCake.");
              SugerFreeNonVegCake sugerFreeNonVegCake = new SugerFreeNonVegCake();
              obj.requiredMaterialsStatus(sugerFreeNonVegCake);

              // Check material requirement for WithSugerVegCake.
              System.out.println("Check material requirement for WithSugerVegCake.");
              WithSugerVegCake withSugerVegCake = new WithSugerVegCake();
              obj.requiredMaterialsStatus(withSugerVegCake);
       }

       public void requiredMaterialsStatus(CakePreparation cakePreparation) {
              cakePreparation.milkRequiredOrNot();
              cakePreparation.eggRequiredOrNot();
              cakePreparation.fruitRequiredOrNot();
              cakePreparation.sugerRequiredOrNot();
       }
}
------------------------------------------------------------
Output รจ
Check material requirement for SugerFreeNonVegCake.
Milk is requird.
Egg is requird.
Fruit is not requird.
Suger is not requird.
Check material requirement for WithSugerVegCake.
Milk is requird.
Egg is not requird.
Fruit is requird.

Suger is requird.

No comments:

Post a Comment