Object oriented design/principles and persistence via /r/learnprogramming

Object oriented design/principles and persistence

Hello, What I'm struggling with is combining the practices I've read in some books with data persistence in my application.

For example, lets say that I have a Person who can own multiple items. An item can be: book, chair, door, car.

To model the example, I would have an Person class with a collection of Item objects (Item would be an interface). Each of the given item types would be in a separate class implementing the Item interface.

So, I'll have something like this:

Person p = new Person(); p.addItem(new Book()); p.addItem(new Car()); // etc. p.getItems(); // would return collection containing the book and the car 

My problem comes when I have to store the person's owned items in a relational database.

I would have the following tables: * person * book * chair * door * car

each of the tables (except person) would have an foreign key to the person table (owner_id).

I'm confused how to make the getItems() method able to load the different item types from the different tables in the database, without coupling the Person object to the database.

Can you give me some ideas?

Submitted July 16, 2017 at 11:38AM by toptenprog
via reddit http://ift.tt/2t4qqnP


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s