I am just practising Java OOPs concepts by building a dummy project of Library management system.
Initially I had classes for Book, Customer, Administrator (with Customer, Administrator extending abstract user class)
I then created list classes BookCollection, CustomerCollection which hold the list of instances of above classes in ArrayList (for a while am not dealing with databases) and perform add, delete, sort methods on corresponding ArrayList (just one inline question: will it be a good design practice if I replace ArrayList related code with database operation once I start dealing with database, with each xyzCollection dealing with xyzTable in database)
The main problem:
Since I thought earlier that I will have to maintain only list of books, customers across app, I made ArrayLists static. Also, I wrote enough of static methods: addXyz, deleteXyz, searchXyz, sortXyz methods However now I realize that for search of Customers or Books I may have (or rather should) to return list of them matching the name, that means I have to return another ArrayList, which should be an instance of xyzCollection, however I cant use ArrayList in these xyzCollection as it is staic shared among all instances.
Initially it was appearing I will need shared ArrayList, but am now doubting my initial decision.
What should be correct?:
- Should I make ArrayList and corresponding methods non static and make any corresponding code changes at calls
- Or should I return ArrayList instead of XyzCollection
What will be better in terms of code design? Or I have made definite mistake in making them all static?
I assume that you are creating collection books/customers after fetching data from DB. As you say its a library management system which means multiple users can use it simultaneously. And each request will be independent of other (like a typical distributed system).
So would suggest you to use normal class instead of static. You can use static for managing utilities like connection to db but not as placeholder like list of books/customers.