Reputation: 141
I have below classes.
public abstract class BaseRequest{
protected SomeEntity entity;
public BaseRequest() {
entity= new SomeEntity();
}
public void setName(String name){
entity.setName(name);
}
public void setAge(String age){
entity.setAge(age);
}
}
public class Request extends BaseRequest{
private String address;
public Request() {
super();
}
public void setAddress(String address){
this.address=address;
}
public String getAddress(){
return this.address;
}
}
I create BaseRequest because it can be used by many sub classes. Also clients should not have access to SomeEntity property of BaseRequest.java
Now clients can use Request
as below.
Request re = new Request();
re.setName("xxx");
re.setAge("xxxxx");
re.setAddress("xxxxxx");
and clients will send this request to service method as below.
public void saveData(Request request){
//here i should get SomeEntity instance directly.
}
To get SomeEntity populated instance directly i can provide a getter method in BaseRequest.java
. But if i provide a getter then even clients can also get SoemEntity instance by instantiating Request class. which i dont want to allow the clients to access it. Now the question is is there any best way to get someEntity instace inside saveData() ?
Upvotes: 0
Views: 58
Reputation: 2763
It seems that you need a protected getter for someEntity in BaseRequest
protected SomeEntity getSomeEntity() {
return someEntity;
}
With this getter you don't need a protected entity
field in BaseRequest
, thus you can make it private.
Upvotes: 1