Toss out Inheritance Forest

Toss out Inheritance Forest

In the previous article, we saw How we can solve the Inheritance forest problem by Decorator pattern,
In case if you missed that Article you can go here::

For who does not like to click the link while reading an Article, I just quickly state the Problem statement.

Problem Statement:
We have to create a ball and same can be made by any materials and can have any color.
So here, two constraints are

A ball can be made by any material (Rubber, plastic, Polythene, Feather, Leather, Leather-Rubber etc.).
Can be any color (Red, Orange, Blue, Yellow, Blue-Yellow etc).
Now, the ball can be any combination of material and color.(Red Rubber Ball, Yellow Rubber Ball etc).

Choosing Design Pattern :

To see the problem one thing is sure Ball’s material and Ball’s color are the variable parts here and it can have any possible values and the second part of the problem statement is it can be any combinations of material and color, technically any combination of two variable parts.

So the crux of the problem is if we create a child class which implements IColor, IMaterial interfaces then for each possible combinations we have to create a concrete class as an outcome creates  Inheritance Forest.

Challenges:
So we have to design our problem in such a way where we can supply the variable parts from outside or technically caller of our design can choose the material and color runtime and pass the same into our framework.

In a generic way  most of the use cases follow the same context, In Functional programming, we can do it through Higher order function where we can pass a function as an argument and that function holds the computation logic.

In Java, we can achieve it through IOC Where Higher level component talk to lower level components via an Interface,

Lower level component holds the strategy implementation and we pass them to Higher order components which hold the Algorithm.

we can achieve it through Strategy Pattern.



Strategy pattern :

According to Wikipedia definition , In computer programming, the strategy pattern (also known as the policy pattern) is a behavioural software design pattern that enables an algorithm's behavior to be selected at runtime.
So by using Strategy pattern, we can push Material and Color into ball creation algorithm as behavior/strategy can be pushed at runtime so we can create different combinations also.

Let See the diagram

Expel Inheritance forest by Strategy pattern





Here I create two interfaces called IColor and IMaterial, each has own concrete implementations Now High-Level components talks to lower level through those interfaces and caller of the program choose one of the implementations of interfaces and pass them to Higher level components for the algorithm computation.

Let see How the code look likes

IColor.java

package com.example.strategy;

public interface IColor {

   public String createColor();

}

IMaterial.java

package com.example.strategy;

public interface IMaterial {
   
   public String createMaterial();

}

Now the Higher Order Component

BallManager.java

package com.example.strategy;

public class BallManager {
   
   IColor color;
   IMaterial material;
   
   public void setColor(IColor color) {
      this.color = color;
   }


   public void setMaterial(IMaterial material) {
      this.material = material;
   }



   public void createBall(){
      String colorStr = color.createColor();
      String materialStr = material.createMaterial();
      displayInfo(colorStr,materialStr);
     
   }
   private void displayInfo(String... strings){
      System.out.println("Add Ball Color ::" + strings[0] );
      System.out.println("Add Ball material ::" + strings[1] );
      System.out.println("Produce the Ball");
   }

}


Client of Our framework creates Actual implementation on the fly.

Caller.java

package com.example.strategy;

public class Caller {
   public static void main(String[] args) {
     
      IColor redColor = new IColor(){

          @Override
          public String createColor() {
              return "Red";
          }
         
      };
     
      IColor blueColor = new IColor(){

          @Override
          public String createColor() {
              return "Blue";
          }
         
      };
     
      IMaterial plasticMaterial = new IMaterial(){

          @Override
          public String createMaterial() {
              return "Plastic";
          }
         
      };
     
     
      BallManager mgr = new BallManager();
      mgr.setColor(blueColor);
      mgr.setMaterial(plasticMaterial);
      mgr.createBall();
      System.out.println("=====================");
      mgr.setColor(redColor);
      mgr.setMaterial(plasticMaterial);
      mgr.createBall();
   }

}


Output:

Add Ball Color ::Blue
Add Ball material ::Plastic
Produce the Ball
=====================
Add Ball Color ::Red
Add Ball material ::Plastic
Produce the Ball

Can we Overload Main method in java.

Yes you can overload main method but public static void main(String[] args) is entry point in java. JVM always search for this method.
If it is not present in overloaded version ,when you try to run your code that gives run time exception no main method found.


From public static void main(String[] args), you can call other overloaded main function.

Keep in one thing in mind

If some one write

public static void main(String... args)//Varags

That is equivalent public static void main(String[] args) that will gives you compile time error.

Example :
package com.example.main;

public class OverLoadmain {
   
    public static void main(String[] args) {
       
        System.out.println("I am in main ");
       
    }
   
public static void main(int args) {
   
    System.out.println("I am in int");
       
    }


public static void main(String args) {
    System.out.println("I am in String");
}


/*public static void main(String... args) {
    System.out.println("I amn in varags ");
}*/

}