dimanche 19 mars 2017

How to connect Ui information and domain information

Say, I'm making a game with a user interface and I want certain persons to be drawn on a canvas, let's say by representing them using rectangles with above them, a textbox with the specific name of the person. Pure from a domain perspective, my person has a name. Later specific domain-logic can be added if necessary

public class Person{
  private String name;
  //Constructor and methods
}

I also need to keep track of the Ui-information concerning the person, so I decide to make a seperate class that does just that.

public Class PersonUi {
  private int x;
  private int y;
  private int length;
  private int width;

  //Constructor and methods
}

Now I have Ui and domain data in different classes. Say I want to know the name of the person when clicking on a certain Person. I can link the correct PersonUi object, but how do I know the name of the person? How to link the Ui and domain data as object oriented as possible?

e.g. how are following options considering good object oriented design?

1) Saving both in lists inside another class and using the order of the lists to know which ui object belongs to which domain object

2) Using a Map to map the two classes

3) Using a wrapper class to wrap the two classes.

Or is it okay to just define all the data in the previous example in just 1 class?

Aucun commentaire:

Enregistrer un commentaire