I have a design question about data. I have a class "T" that has a single interface handling two data types as described below. The class T is constructed to handle one of the two data types. Users of T class do not need to know which type of data T class handles.
- Data class "L" that has a known (limited) number of instances, which are constructed during runtime and they do not change. I plan to use a singleton to hold a container for all such data objects. In the class T, I can directly access the singleton and therefore avoid the need for passing the data object as a function argument.
- Data class "UL" that have an unknown (unlimited) number of instances. The class T works on each instance one at a time. It seems I will have to pass this type of data in as an argument. This would be fine if T happens to handle UL. But if T handles data type "L", then the argument of "UL" will end up not being used. Is this a legitimate design? It feels strange as the UL argument can be redundant. But can it also be harmless?
- At this point both type of objects are mutable during their lifetime. The data objects will not change in "T" / "Ts", but could change in class "P".
- The data type "L" is needed because it has a distinct business meaning and L class have a pre-defined number of instances. the "L" data is like global data, while the "UL" data is like user data. The "UL" data is handled one by one.
I was also wondering if there is a way to avoid passing the "UL" object as a function argument. For example, I can set up a reference outside the class T, and each time the UL object changes, the new UL is assigned to the reference. Would this allow me to hide the different instances of UL behind a fixed reference? Can I then somehow access the fixed reference in class T, in a way similar to the singleton for data type L?
I got stuck in this design and I feel I may have missed something big.
- A T object may work with either "L" type or "UL" type. Multiple "T" objects are aggregated in another class Ts. The number of T objects within a Ts object can vary.
- A Ts object is used in a user class "P", which read "UL" objects one by one, and pass the "UL" object to the "Ts" object that it holds. The "Ts" object then pass the UL object to the individual "T" object, which may or may not use the "UL" object.
- I have this design because it seems desirable to have a single interface for the class "T", so that they can be used in a consistent way within "TS" / "P" object.
class P { Ts ts_; public void work(UL_Data ul_data) { ts_.work(ul_data) } } class Ts { IList<T> ts_; public void work(UL_Data ul_data) { //... foreach(var t in ts_) { double val = t.read(ul_data); //.... } //... } } class T { //set to true if the class read data type L bool data_type_L; public double read(UL_Data ul_data) { if(!data_type_L) //...read data type L from singleton else //read data type UL from function argument ul_data } }