A
A
ape3642014-06-21 21:27:23
Java
ape364, 2014-06-21 21:27:23

How to store data in struct classes in Java?

My attempts to write in Java began not so long ago and the question arose: how to store data, for example, about an address?
There is a class AddressParcel (parcel - for convenient transfer of this class between activities) with fields:
private String streetType; // street type name (street, avenue, boulevard, etc.)
private String streetTypeShort; // short name of the street type
private String streetName; // street name
private String streetKladr; // KLADR street code
private String houseNumber; // house number
private String houseKladr; // house code KLADR
private String houseEntrance; // entrance
private String lat; // latitude
private String lon; // longitude
There was a desire to make three nested class-structures for "beauty": Street, House, Coords. I tried it - it's not very clear what visibility modifiers to set for each of the classes and what getters / setters should be - for the "main" class, or for each subclass separately with access like addressParcel.house.getNumber().
Please tell me how to do it right :)

Answer the question

In order to leave comments, you need to log in

3 answer(s)
P
pi314, 2014-06-22
@ape364

The correct approach in object design is not to spawn entities unnecessarily! "Beauty" in itself is not a reason. The reason may be that, for example, houses and pairs of coordinates are aggregated somewhere in separate lists, into which you don’t want to drag unnecessary trash.
And besides, if the data is persisted, for example, using ORM, or serialized, for example, in JSON, then any division is an additional join with the resulting loss of performance / memory, etc.
If there are still real justified reasons to separate entities, then it is correct to do this:

class House{
  private String number; // номер дома
  private String kladrCode; // код КЛАДР дома
  private String entrance; // подъезд

  public String getNumber(){ return this.number;}
  public String getKladrCode(){ return this.kladrCode;}
  public String getEntrance(){ return this.entrance;}

  public void setNumber(String number){ this.number = number;}
  public void setKladrCode(String kladrCode){ this.kladrCode = kladrCode;}
  //  и т.д.
}
class AddressParcel {
  private String streetType; // название типа улицы (улица, проспект, бульвар и т.д.)
  //...
  private House house = null; // это не обязательно, но "хорошая практика" - явно инициализировать объектные поля статически!
  // ...
  public House getHouse() { return this.house;}
  public void setHouse(House house) { this.house = house;}
}

And access is like this:
String someType = someAddress.getStreetType();
String someNumber = someAddress.getHouse().getNumber();

And one more thing: data types (only strings!) lead to some bad thoughts ...

S
svd71, 2014-06-21
@svd71

1. Java is not rich in syntax, and therefore the replacement of C structures and Pascal records remains to be stored in the class. If the matter is in the data set, then it is not at all necessary for it to have a constructor or getters / setters unnecessarily. The only thing you need to take care of is the visibility of these fields (i.e. Prived does not roll here).
2. I add getters when it is necessary to read some not quite standard records from such "structures": for example, the house number in Roman numerals or something else of the same plan. Setters - when you need to carry out some kind of transformation - for example, select only the number of the house and apartment from the address in a full line and write it in the required fields. The fact is that a structure is a structure and it does not make sense without a special need to update anything with code - it takes precious processor time to execute it.

F
Flasher, 2014-06-21
@Flasher

Don't forget what encapsulation is. For greater reliability, set the private modifier and pass all fields of the class instance, through the methods in which get/set will be defined.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question