The fact is that a material object must have a field that does not make sense to an immaterial object
Here is a class whose objects I want to divide into material and immaterial:
public class GameObject {
protected boolean isHidden;
protected Coordinate position;
protected int pictureWidth, pictureHeight;
public Object filling;
protected GameObject(int pictureWidth, int pictureHeight, Coordinate position, boolean isHidden, Color filling){
this.position = position;
this.pictureWidth = pictureWidth;
this.pictureHeight = pictureHeight;
this.isHidden = isHidden;
this.filling = filling;
}
protected GameObject(Coordinate position, boolean isHidden, BufferedImage filling){
this.position = position;
this.pictureWidth = filling.getWidth();
this.pictureHeight = filling.getHeight();
this.isHidden = isHidden;
this.filling = filling;
}
protected GameObject(GameObject gameObject) throws IOException{
this.position = new Coordinate(gameObject.getPosition());
this.pictureWidth = gameObject.getPictureWidth();
this.pictureHeight = gameObject.getPictureHeight();
this.isHidden = gameObject.isHidden();
this.filling = gameObject.getFilling(); // передается ссылка
}
private GameObject getGameObject() {
return this;
}
public void paint(Graphics gr) throws IOException{
if(filling instanceof BufferedImage) {
gr.drawImage((Image) filling, position.getX(), position.getY(), null);
}
else if(filling instanceof Color) {
gr.setColor((Color) filling);
gr.fillRect(position.getX(), position.getY(), pictureWidth, pictureHeight);
} else {
System.err.println("programmer, you forgot to add a way to draw this object");
}
}
Object getFilling() {
return filling;
}
int getPictureWidth() {
return pictureWidth;
}
int getPictureHeight() {
return pictureHeight;
}
boolean isHidden() {
return isHidden;
}
Coordinate getPosition() {
return position;
}
}
I suppose that you need to create 2 classes that inherit GameObject, and make the constructor of the GameObject class visible only for these two classes:
1) class MaterialGameObject:
public class MaterialGameObject extends GameObject{
final int materialHeight;
public MaterialGameObject(int materialHeight, int pictureWidth, int pictureHeight, Coordinate position, boolean isHidden, Color filling){
super(pictureWidth, pictureHeight, position, isHidden, filling);
this.materialHeight = materialHeight;
}
public MaterialGameObject(int materialHeight, Coordinate position, boolean isHidden, BufferedImage filling){
super(position, isHidden, filling);
this.materialHeight = materialHeight;
}
public MaterialGameObject(MaterialGameObject materialGameObject) throws IOException{
super(materialGameObject.getGameObject());
this.materialHeight = materialGameObject.getMaterialHeight();
}
public int getMaterialHeight() {
return materialHeight;
}
}
class ImMaterialGameObject:
public class ImMaterialGameObject extends GameObject{
public ImMaterialGameObject(int pictureWidth, int pictureHeight, int materialHeight, Coordinate position, boolean isHidden, Color filling){
super(pictureWidth, pictureHeight, position, isHidden, filling);
}
public ImMaterialGameObject(Coordinate position, boolean isHidden, BufferedImage filling){
super(position, isHidden, filling);
}
public ImMaterialGameObject(GameObject gameObject) throws IOException{
super(gameObject);
}
}
Do you know a better way to solve the problem?