Tri Nguyen Tri Nguyen - 2 months ago 10
Java Question

Java, Database - Why have assign a new object when we can put it straight into database

I have a window to let user book an appointment with some fields

(date, start time, end time, ...)
and save it into a database. Why do I need to save these fields in an object such as

Appointment a = new Appointment(date,start,end,...);

and then using
methods to get this set of data back instead of saving it directly into a database?

Someone tells me because of how object oriented programming works but the task is just inserting a new set of data to a database, no amendment needed.


Yes, we can put them directly, but it's inconvenient.

We work in OOP style trying not to interact with raw input data. After receiving some data, we normally construct an instance of some class and then work only with this object and its state.

It's convenient because an instance keeps all needed information about an object that it presents. We unproblematically can pass it into different methods by using only one argument (not thinking about a number of fields that we want to save).

During processing, an instance may go through few milestones (like filtering, validation) before it is prepared to persist. Therefore, it's important that such infrastructure methods take a complete, already formed object and just touch up its state.

And finally, with all of that, OOP makes your code more readable and self-explanatory.