I am using a servlet that is taking parameters from a registration form, what I am doing at present is using a java class that will process on those parameters and store them in Database but I feel like the way I am using this class is wrong. My code is like this:
UserRegister ureg=new UserRegister(name,age,sex,country);
And that is all, my constructor in the class will do the entire job from processing the data to storing it in db by calling methods in the UserRegister class on its own.
Is it the right approach, or there is any better way of doing this?
EDIT: my UserRegister class will do just some processing on the data and it will call a business layer to store the data, it itself is not going to interact with the database.
What I am asking is what approach is to be followed when you have a class where after initializing there are just a fixed series of steps to be performed inside it
Wrong. Never program this way.
Come out of c’tor ASAP after intializing the basic necessity of it’s class.
The method in which
UserRegisterobject is created. use that method to call methods onuregobject.For example:
This is only an example to improve your current design. however you should have some utility classes also. class
UserRegistershould have data and behaviour which represent a User, only a User.Design Utility classes such that they take input in a format, do some calculation and then return the output data to it’s caller. So, whenevr
processRequestmethod needs a job to be done, it doesn’t do it by itself but calls another method (it may be in same class or some other class)EDIT:
It depends on the nature of those steps. Find out how are they related. are they related to User? if yes, put them as methods in User class. if not, combine related steps together, create a class with meaning full name and use it. When creating a separate class, try to design in such a way they can be used as utility and not only for the current purpose.