I have a database layer at my application. At my front side I have graphics to show users. I get necessary data from database and show that data at web site. When I get raw data from database I process that data for which API I use (highcharts etc.). I designed something like that:
ChartDTO
The main interface for all chart implementations. Has methods as like:
public Object getTotalUsersPieChart(DatabaseData databaseData);
and
HighChartsDTOImpl
An implementation of ChartDTO. All its methods returns Object type variables.
At client side I directly feed my graphics with the data coming from server side without needing any pre-process at client side.
My question is that:
I know there is a design mistake here and maybe it is not a DTO design (at least my architecture says that)
How I could design that scenario?
I suggest you use some sort of Factory pattern.
You should pass the client a "hint" on the type of chart you want to create,
And have a ChartFactory create it for you.
Here is a code snippet:
You can have this factory to be a Singleton,
so if you need to do some factory initializing, you will do that only once.
ChartParameters should hold parameters for creating an object of specific chart type.
It can be a simple map of keys and value, i.e look like:
Or this can have another implementation.
You may also decide to have parameter class per each one of the subclasses of Chart (all these parameter classes will extend ChartParameters),
But this may cause you some sort of duplication at the inheritance tree.
To conclude,
In this way all you need to send to the client is the chart type, and the parameters, and it will instantiate that proper chart object.