vendredi 24 novembre 2017

Java design pattern - class to represent a variable for interpreter

Please share your best practice, how is it better to make a class to store variables of different types? For example, there are 4 types of data that can be stored in the variable: int, float, boolean and string. And I'd like to make a class to store any of these types. What is better? To make a simple class that will hold any of these as an Object and will keep the type separately? Or just store the Object and use it's real type? Or to make a base class and then to make subclasses for each type? How to make a getter for the value in both cases? Just return Object that will have a real type of that variable? I don't like both methods because the access to the data is quite complicated - calling foo shall implement some logic to understand what is the real type there. But I cannot find any other. Please share your experience how it's usually done in Java? The area is - small interpreter or data exchange with the database. In other words, to keep data of different kind which real type will be known just in run time, and to process it.

Aucun commentaire:

Enregistrer un commentaire