Saturday 1 October 2022

 Design Pattern: Bridge  & Facade 

a)Bridge Design Pattern:

In bridge design pattern, we design in such a way that we keep abstract class/ interface functioning separate from Concrete class.

This way, we can alter the abstract class methods and still not affect the functioning of concrete class. Bridge design pattern is mostly designed for bridging between abstract class and concrete class, because in abstract class can add or delete methods and it shouldn't affect functioning of concrete class.

a) abstract class bottle(){

  void cap();

int shape();

public String color(){

System.out.println("The bottle color by default is white");

}

}

//Concrete class

b) public class GlassBottle extends bottle{


@Overwrite

public void cap(){

System.out.println("The bottle has red cap");

}

@Overwrite

public int shape(){

int width =10;

int height = 10;

int area = width* height;

System.out.println("The bottle cross-section is:" + area);

}


public String message(){

System.out.println("The bottle is for cold-drink purpose");

}

}

// Bridging class

c) public class BridgingClass {

GlassBottle glassBot = new GlassBottle();

glassBot.message();

glassBot.shape();

glassBot.cap();

glassBot.color();

}

b)Facade Design Pattern:

In facade design pattern, we design in such a way that we share a different class which is not concrete or implementation class of interface or abstract class.

This way, we donot expose the logic used in concrete class and if saved from changes by external user.

1) Create products in form of interfaces:

a) interface bottle(){

  void cap();

int shape();

String color();

}

//Concrete class to implement logic for interface

b) public class OilBottle implements Bottle {

  String capShape = "Circular";

 String capMaterial = "aluminium";

  @Override

   public void cap() {

   System.out.println("The cap is : " + capShape +  "and " + capMaterial + "with company logo on it");

}

 String bottleShape = "Cylindrical";

 int height = 12;

 int diameter = 10;

int volumeBottle;

  

  public int shape(){

   System.out.println("The bottleShape is : " + bottleShape);

  int areaBottle = 3.14 * height * (diameter/2)*(diameter/2);

  return areaBottle;

}


 String bottleColor = "red";  

public String color(){

  return bottleColor;

}

}

//Another concrete class

c) public class JuiceBottle implements Bottle {

  String capShape = "Circular";

 String capMaterial = "plastic";

  @Override

   public void cap() {

   System.out.println("The cap is : " + capShape +  "and " + capMaterial + "with company logo on it");

}

 String bottleShape = "Cylindrical";

 int height = 12;

 int diameter = 10;

int volumeBottle;

  

  public int shape(){

   System.out.println("The bottleShape is : " + bottleShape);

  int areaBottle = 3.14 * height * (diameter/2)*(diameter/2);

  return areaBottle;

}


 String bottleColor = "white";  

public String color(){

  return bottleColor;

}

}

d)Intermediate class

public class BottleClass {

         JuiceBottle juiceBot = new JuiceBottle();

       OilBottle oilBottle = new OilBottle();

     public void oilBottleMet(){

      oilBottle.cap();

       oilBottle.shape();

      oilBottle.color();

}


//This method will call all the methods of juice bottle class

 public void juiceBottleMet(){

      juiceBottle.cap();

       juiceBottle.shape();

      juiceBottle.color();

}


}

//Facade class is calling intermediate class which will publish all the methods of required concrete class

d) public FacadeClass {

BottleClass productJuiceBot = new BottleClass();

productJuiceBot.juiceBottleMet();

BottleClass productOilBot = new BottleClass();

productOilBot.oilBottleMet();

}

No comments:

Post a Comment