lundi 18 février 2019

Is the below a Java Multiple Inheritance Design Pattern if yes which one?

I have been working in a Swing application for more than 8 years but it was designed perhaps 20 years ago, the designers created their own custom made components (I think initially using AWT but it was updated to Swing at some stage).

There is a widget used by multiple screens that presents data in a Gantt Chart stile that allows for user interaction, drag and drop among other things. A simplified depiction of this design is as below:

//Many lines of implementation code and only 1 constructor
public class GanttChartImplementation extends JPanel {

  public GanttChartImplementation(String p1, Object p2, boolean p3, boolean p4){
    //Implementation code
  }

}


//No implementation only constructors
public class GanttChartInterface extends GanttChartImplementation {

  public GanttChartInterface(String p1, Object p2, boolean p3, boolean p4) {
    super(p1,p2,p3,p4);
  }

  public GanttChartInterface(String p1, Object p2, boolean p3) {
    super(p1,p2,p3,true); //defaults p4 to true as it's not passed in
  }

  public GanttChartInterface(String p1, Object p2) {
    super(p1,p2,true,true); //defaults p3, p4 to true as these are not passed in
  }
}

All the screens in the application will extend GanttChartInterface that its first obvious use is to hide the implementation class from the screens that use the Gantt Chart widget. Even in theory GanttChartInterface may be changed to extend a different implementation class without having to change the screens that use the Gantt Chart widget.

Another benefit about this design, that I have found over the years, is that when adding a new parameter to the constructor in GanttChartImplementation class then screens that use the widget don't need to be changed because It's only required to modify existing constructors in GanttChartInterface so they set the new param value to a default and then will create a new constructor, in example as below:

//Add new parameter to constructor used to have 4 now 5
public class GanttChartImplementation extends JPanel {

   public GanttChartImplementation(String p1, Object p2, boolean p3, boolean p4, boolean p5){
     //Implementation code
   }

}

Only need to modify GanttChartInterface so it compiles and there is no need to change any of pre-existing screens that use the widget:

//No implementation only constructors had 3 constructors now has 4
public class GanttChartInterface extends GanttChartImplementation {

  //Create new constructor that receives the new parameter
  public GanttChartInterface(String p1, Object p2, boolean p3, boolean p4, boolean p5) {
    super(p1,p2,p3,p4,p5); 
  }

  public GanttChartInterface(String p1, Object p2, boolean p3, boolean p4) {
    super(p1,p2,p3,p4,true); //defaults p5 to true as it's not passed in
  }

  public GanttChartInterface(String p1, Object p2, boolean p3) {
     super(p1,p2,p3,true,true); //defaults p4, p5 to true as these are not passed in
  }

  public GanttChartInterface(String p1, Object p2) {
     super(p1,p2,true,true,true); //defaults p3, p4, p5 to true as these are not passed in
  }
}

I have looked at many articles around the web and SO questions and this doesn't seem to be a GoF design pattern and haven't found anywhere if this is a widespread used design pattern or not? I am trying to find information as I would like to know who came up with this design. Thank you in advance for any help.

Aucun commentaire:

Enregistrer un commentaire