I am writing a game. The question is simple and concerns fundamentals such as abstraction and, possibly, patterns.

There is an interface for an inactive object. That is, the object that is drawn can change the shape, size, position on the screen, etc. Here is its essence:

public interface IBasicObject { byte update(float dt); float getX(); float getY(); void setX(float pX); void setY(float pY); //... дальше идут другие графические геттеры и сеттеры } 

There is an object that implements this interface, called BasicObject. Such objects are used for decoration.

There is also the interface of a “living object”

 public interface IHealthObject extends IBasicObject{ Body mBody = null; float GetHealth(); int GetTypeOfBlast(); Body GetBody(); void ResetBody(); void ReduceHealth(float healthToReduce1); void Kill(); } 

Well, there is a HealthObject object that implements this interface, and at the same time it is extended from the BasicObject class. It looks like this:

 public class HealthObject extends BasicAnimatedObject implements IHealthObject 

That is, HealthObject not only has all the graphic components, but can also participate in collisions.

Now I have different classes (based on Array) to handle each type of object. How to create a class that can store any kind of object (either BasicObject or HealthObject). In this case, the appeal will be only to the functions specified in the most basic interface - to Update (dt) and getter-setters.

  • Any object that implements BasicObject or HealthObject or other derivatives of these interfaces can be passed as a parameter (BasicObject basicObject) - JVic
  • What is the issue with the work of the IDE Android Studio? - pavlofff
  • Generics not considered? - Senior Pomidor

2 answers 2

Yes, there is a template, but I do not remember the name. Sorry for a little inadequate code, but I think the essence of it will be clear. We have to work with the base interface in this class.

Main class

 /** * Created by Andrej on 18.10.2016. */ public class Main { public static void main(String[] args) { IDoingSomethingWithAllObjectsImplementingIBasic iDo = new IDoingSomethingWithAllObjectsImplementingIBasic(); iDo.addObj(new BasicObject()); iDo.addObj(new BasicObject()); iDo.addObj(new HealthObject()); iDo.doSome(); } } 

Class IDoingSomethingWithAllObjectsImplementingIBasic

 import java.util.HashSet; import java.util.Set; /** * Created by Andrej on 18.10.2016. */ public class IDoingSomethingWithAllObjectsImplementingIBasic { Set<IBasicObject> allObj = new HashSet<>(); void addObj(IBasicObject obj){ allObj.add(obj); }; void doSome(){ for(IBasicObject obj : allObj ){ obj.doSamething(); } }; } 

As you can see, we use the same class to work with different objects. Subsequently, when adding new types of objects, you do not have to change this piece of code. Since this class knows nothing about implementations. For him, the main thing is a contract - an interface. And any class that implements this interface will be processed without problems.


Class HealthObject

 public class HealthObject implements IHealthObject { int x,y; @Override public void doSamething() { System.out.println("Do do do... Health"); } @Override public int getX() { return x; } @Override public int getY() { return y; } @Override public void setX(int x) { this.x = x; } @Override public void setY(int y) { this.y = y; } } 

BasicObject class

 public class BasicObject implements IBasicObject { int x,y; @Override public void doSamething() { System.out.println("Do do do... Bas"); } @Override public int getX() { return x; } @Override public int getY() { return y; } @Override public void setX(int x) { this.x = x; } @Override public void setY(int y) { this.y = y; } } 

Interface IBasicObject

 public interface IBasicObject { void doSamething(); int getX(); int getY(); void setX(int x); void setY(int y); } 

IHealthObject interface

 public interface IHealthObject extends IBasicObject { } 

enter image description here

  • Thank. Yesterday he came to this idea. But I think, let the question remain here - can someone come in handy. It was not clear to me how the base class will be created with the IBasicObject field. That is, it is not known in advance - how much memory will be allocated for this field, which means it is not known how much memory to allocate for an object of the base class. Now I have come closer to understanding this, but the question still remains - is it possible to initialize such an "interface" field not in the constructor of the base class, but in any method? - Andrey Kostrov

I would, instead of IBasicObject, IHealthObject introduce some more private interfaces (HasPosition, ..., HasHealth, Drawable)

You inherit the interfaces as you normally inherit classes, I do not recommend this, because flexibility is lost.

  • Try to write more detailed answers. Explain what is the basis of your statement? - Nicolas Chabanovsky
  • Yes, I myself come to the same thought. Interface inheritance is a bad idea. The same is done downtime by the implementation of both interfaces through a comma. In this case, the interfaces are not connected to each other. At the same time, an object can exist that does not need a graphical interface, but needs a physical one. I am still in doubt - to which the coordinates of the object relate more - to its graphic implementation or to the physical one. Now I am more inclined to the physical. A schedule to leave only the data on the textural polygons, and the coordinates for drawing to transfer from physics. - Andrey Kostrov