Absolut Absolut - 1 year ago 40
Java Question

Objects are not procedural data structures, okay, so what to do?

I've read a lot about "true"(maybe) OOP when we have ideal incapsulation, objects that send messages and so on. I'm trying to use such concepts in my everyday programming, but it's hard.

For example, i have a CRUD app. It stores books.

Class Book {

private String isbn;
private String title;
private String author;

//constructor, accessors, but NO methods
}


What is "book"? Just a simple string in catalogue (title, author, ISBN, etc.) I don't need to
read()
or
trasformToPdf()
methods. But what i need? Just keep my catalogue of books. How i can apply all these principles? How to express simple group of values in Java? I see two solutions:


  • there is a NamedTuple thing in Python, so i think i can adapt it in Java using HashMap;

    BTW, i never saw such implementation. Seems, Java world is happy with data-storing objects.

  • redisign my app to eliminate needs of data-storing objects (but i don't know how)



So, how to deal with "true" OOP and "present" design?

Answer Source

But what I need? Just keep my catalogue of books.

Then you might create a Catalogue class which simply encapsulates Map<Identifier, Book> and provides methods to work with that collection.

I don't need to read() or trasformToPdf() methods.

Normally, they should not be a part of the Book class. A book can't read itself or transform itself into a PDF.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download