Saturday 1 October 2022

 Design Pattern: Command

a)Command Design Pattern:

In command design pattern, we overwrite a method in interface in such a way that we can automate operations like LIGHTS ON or OFF, BUTTON ON or OFF, MODE ON or OFF, MACHINE ON or OFF, etc.

Basically, this design pattern is used to execute binary (ON or OFF), ternary (ON, HOLD, OFF) or multiple stages of operation (like MODE 1, 2, 3 , 4, 5, 6, etc in various machines)

//create an interface with execute method defined in it

a) interface Command{

    public void execute();

}

// create a concrete class which will have different operations in it

b) public class Bulb {

   public void on(){

       System.out.println("Bulb is turned on");

}

  public void off(){

     System.out.println("Bulb is turned off");

}

}

//Create a concrete class which will execute command design pattern

c) public class BulbOnCommand implements Command {

     private Bulb bulb;

     public BulbOnCommand(Bulb bulb){

     this.bulb = bulb;

}

  public void execute(){

 bulb.on();  }

}

d) public class BulbOffCommand implements Command {

     private Bulb bulb;

     public BulbOffCommand(Bulb bulb){

     this.bulb = bulb;

}

  public void execute(){

 bulb.off();  }

}

//User class which will execute command logic

e) public class CommandDemo {

    Command command = new 



}

 Design Pattern: Strategy 

a)Strategy Design Pattern:

In strategy design pattern, we can change class behaviour or algorithm on fly.

E.g.: We create interface which accepts two numbers. Now, we can call either of the concrete class which is implementing logic for this interface and thus change the behavior of final calling class.


//Create an interface

a) public interface Numbers {

     int operation(int a, int b);

}

//Create concrete classes which will implement above interface

b) public class AddNumber implement Numbers {

  @Override

   public int operation(int a, intb){

   int c = a+b;

return c;

}

}

c) public class SubtractNumber implement Numbers {

@Override

   public int operation(int a, intb){

   int c = a-b;

return c;

}

}

//Create intermediate class which implements the strategy classes above

d) public class Context {

private Numbers numbers;


//this is parametrized class instance of Context

public class Context(Numbers numbers){

this.numbers = numbers;

}

public int mathOperation(int c, int d){

   numbers.operation(c,d)

}

}

//Now implement strategy class

e) public class StrategyDemo {

  

        public void strategyMethod(){

Context addContext = new Context(new AddNumber());

addContext.mathOperation(7,8); 

//output here is 15 because it call operation method from AddNumber class

Context subContext = new Context(new SubtractNumber());

subContext.mathOperation(4,2);

// output is 2 because it will call operation method from SubstractNumber class

}

}

//If you see here, we can either call the SubtractNumber or AddNumber class and thus change the final outcome of the strategyMethod()


 Design Pattern: Mediator

a)Mediator Design Pattern:

In mediator pattern,  we develop as class which act as coupling among different  concrete classes. It can look like Facade or bridge design pattern at times, but difference lies in it that it calls object of different classes and their methods as per need.


E.g.: We want to build a bottle filling plant. 

The processes in it are: 

a) Create a bottle,

b) Fill it with juice,

c) Put air-tight cap on it,

d) Put label,

e) Dispatch it

1) Create multiple classes for functionality:

a) public class Bottle(){

  public String createBot(String message){

System.out.println(message);

}

}

b) public class FillJuice {

int processValue;

print int processParam(){

int formula = (3.14*7.9(12)+12*34);

formula = processValue;

return formula;

}

  public String fill(){

System.out.println("Fill juice in bottle with process prameter: " + processValue);

}

}

c) public class BottlePack{

  public String pack(){

  System.out.println("Bottle capped, labelled & ready for dispatch");

}

}

//Create concrete class which calls all the above classes and executes the bottle filling process

d) public class BottleFill {

Bottle bottle = new Bottle();

FillJuice fillJuice = new FillJuice();

BottlePack bottlePack = new BottlePack();

bottle.createBot("Create a glass bottle for juice");

fillJuice.fill();

bottlePack.pack();

}


 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();

}