The inheritance hierarchy is changed to reduce code duplication
I am having difficulty customizing the structure of my code. In one project I have two classes LeftCell
and RightCell
extending the class Cell
. Now, to avoid code duplication, I want to use these objects in several other projects. The problem is that I also want to add additional functionality to these objects (specifically to the object Cell
) that are different for each project.
Suppose I am creating a new project in which I want to render objects Cell
using the method void draw()
. My first thought was to create a new class CellProject1
that extends the class Cell
and includes a method draw()
:
class CellProject1 extends Cell {
void draw() {}
}
The problem is that any LeftCell
/ objects RightCell
I create of course don't have access to this method draw()
. I guess I want to somehow compress the subclass Cell
in the class hierarchy so that it changes:
Cell
LeftCell
RightCell
in
Cell
CellProjectX
LeftCell
RightCell
depending on the project I'm running on. I have been playing around with generics but can't get it to work. All suggestions are welcome!
source to share
The problem is that any LeftCell / RightCell objects I create execute of course don't have access to that draw () method.
A method specific to a child class cannot, of course, be called on a parent instance that doesn't know it.
Since your requirement
The inheritance hierarchy is changed to reduce code duplication. In one project, I have two classes LeftCell and RightCell, both extensions of the Cell class. Now, to avoid code duplication, I want to use these objects in several other projects
I think you should be doing something completely different.
If you want to avoid an explosion in the number of possible combinations, not duplication LeftCell
and RightCell
as in your example:
Cell
CellProjectX
LeftCell
RightCell
which can be finalized:
Cell
CellProjectY
LeftCell
RightCell
Cell
CellProjectZ
LeftCell
RightCell
you must approve composition over inheritance to create specific project implementations Cell
.
For common cell structures:
A subclass Cell
can be an interface Cell
that defines common methods for anyone Cell
, and you can have a class AbstractCell
that defines a common implementation for it.
public interface Cell{
int getValue();
void setValue(int value);
}
public abstract class AbstractCell implements Cell{
...
}
Then you can define RightCell
and LeftCell
by expanding AbstractCell
:
public class RightCell extends AbstractCell {
...
}
public class LeftCell extends AbstractCell {
...
}
For project-specific cell implementations:
Now in a specific project, you can create a custom implementation Cell
by composing it with an instance Cell
(finally an instance LeftCell
or RightCell
) that under the hood will be used to implement Cell
in a specific specific project class.
In a concrete implementation, you could of course add any project-specific method you need.
For example:
class CellProject1 implements Cell {
private Cell cell;
CellProject1 (Cell cell){
this.cell = cell;
}
public int getValue(){
cell.getValue();
}
public void setValue(int value){
cell.setValue(value);
}
public void draw(){
...
}
}
You can create instances like this CellProject1
:
CellProject1 leftCell = new CellProject1(new LeftCell());
CellProject1 rightCell = new CellProject1(new RightCell());
leftCell.draw();
rightCell.draw();
And in another project that uses instances CellProject2
with a specific method write()
, you can write:
CellProject2 leftCell = new CellProject2(new LeftCell());
CellProject2 rightCell = new CellProject2(new RightCell());
leftCell.write();
rightCell.write();
source to share
The problem is that I also want to add additional functionality to these objects (specifically the Cell object) that are different for each project.
I recommend creating an interface that implements Cell
, allowing its child classes to implement the methods you want, especially if it's Cell
an abstract class.
public interface Features {
...
}
public abstract class Cell implements Features {
...
}
source to share