Can I create all the request related model classes as inner class of one single class called Request

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Can I create all the request related model classes as inner class of one single class called Request



I have some 'n' model class for request similarly for response also.



Ex: Request class


public class CurrentData

public RequestTypes RequestType;
public String Node;
public String SessionKey;
public String ItemName;
public boolean ShowTimestamp;



public class HistoryData

public RequestTypes RequestType;
public BoundaryTypes BoundaryType;
public String Node;
public String SessionKey;
public String ItemName;
public String Filter;
public long StartTime;
public long EndTime;
public long NoOfValues;
public boolean BackwardInTime;
public boolean ShowTimestamp;




I would like to put these two model classes into one class called Request.


public class Request

public class CurrentData
public RequestTypes RequestType;
public String Node;
public String SessionKey;
public String ItemName;
public boolean ShowTimestamp;


public class HistoryData
public RequestTypes RequestType;
public BoundaryTypes BoundaryType;
public String Node;
public String SessionKey;
public String ItemName;
public String Filter;
public long StartTime;
public long EndTime;
public long NoOfValues;
public boolean BackwardInTime;
public boolean ShowTimestamp;




When client request for currentdata server uses new Request().new Current() object. Similarly it creates for historydata also. Is this correct?



Because in each data model i will have just data attributes like name, etc . If i create new classes separately then my project is having so many no of classes just for models. I thought Request class will have all the request related model class. Similarly, Response class will be created and will have response model classes.



My question is: Is it write way to do program? Please suggest me.





To be able to instantiate an inner class outside of that class you must add 'static' to the definition: public static class CurrentData { .... then you would do something like 'CurrentData data = new Request.CurrentData()'. Without 'static' only methods inside of Request can create the inner class.
– Richard
2 hours ago




3 Answers
3



Is there a specific reason why are you using three classes instead of just putting the data inside Request?


Request



Only separate classes where it make sense to do so. One example is when you have a complex object that will be used in multiple places (complex being a class that has 5 or 6 different variables or operations that will be commonly done on a set of inputs) so you Don't Repeat Yourself (DRY).


D


R


Y



Never create classes just to create classes. A class with one variable is never a good idea since it would just generate extra code and chaining:


(request.currentData.currentName)



has unnecessary data since that is the only variable in currentData.


request.currentName



is better since the data is where it belongs and does not have someone reading the code wondering "why is currentData even there?"



Remember: You will read code more than you write code and you will forget why you did something odd. Make things as simple as possible to be able to read and understand without having to try to remember "why did I do that?". This comes easier with time and experience but even the best programmers out there will sometimes make things more complicated than needed.



So the better way to do this would be:


public class Request
public String currentName;
public String historyName;



If this is only a data object and will never contains methods that operate on the data then having the data variables pubic works fine.



The Response class would be similar.


Response



Typical OOP and Java standards - usually when other methods are in the class that operate on the data - will use encapsulation as follows:


public class Request
private String currentName;
private String historyName;

public void setCurrentName(String currentName)
this.currentName = currentName;


public String getCurrentName()
return currentName;


public void setHistoryName(String historyName)
this.historyName = historyName;


public String getHistoryName()
return historyName;




Encapsulating like that allows the implementation to change if needed without having to change the code that uses the class.





Sorry i haven't put all the member variable. Below is my class structure public RequestTypes RequestType; public String Node; public String SessionKey; public String ItemName; public boolean ShowTimestamp;
– Maha Lak
2 hours ago




Here are some ideas:


RequestClass


CurrentData


HistoryData



For example:


public class CurrentData
//...

public class HistoryData
//...


public class RequestClass

private CurrentData current;
private HistoryData history;

// constructor with parameters of current and history data
//...
// get set methods for current and history
//...



When client request for currentdata server uses new Request().new
Current() object. Similarly it creates for historydata also. Is this
correct?



Make a request class object with current and history data:


CurrentData curr = new CurrentData();
HistoryData hist = new HistoryData();
RequestClass req = new RequestClass(curr, hist);



To access the current data from the request:


req.getCurrentData(); // this gets the current data associated witht the request.



Similarly one can model a ResponseClass.


ResponseClass



Looking at the code available in your post, your way of dealing with the situation you described is not right. It is also very difficult to analyse this question without looking at the client code where the Request object is actually getting created.



With an assumption that you just want to have data of both classes as part of single request while being called, your code should be like this:


public class Request
public CurrentData RequestCurrentData;
public HistoryData RequestHistoryData;



This will allow your client to send data of both classes as single Request object.



Note: This is a simple code sample. You can always optimise it.



Hope this helps!






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard