Reputation: 1232
I want to build a simple application for a bookstore. There are a few types of things on sale: books, movies, and magazines. I was thinking to of building one abstract class with a toString()
method, and 3 subclasses - books, movies, magazines (there will be more later). Then in the program I want to operate on these objects and was thinking I would keep them all in an ArrayList<ParentClass>
. Is this a good choice of data structure?
Upvotes: 0
Views: 7518
Reputation: 19552
If you want to go the abstract class way, I'd recommend setting common properties. For example, as you are using an example of a bookstore, I'm sure that everything has a price. There are probably other things you could associate with it as well. This will save you from reimplementing simple things over-and-over again in your subclasses.
abstract class BookstoreEntity {
protected double price;
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
I'm a bigger fan of Interfaces than I am of inheriting from abstract classes. I'd suggest defining a BookstoreEntity
interface instead of using an abstract class.
interface BookstoreEntity {
public double getPrice();
public void setPrice(double price);
//more methods here.
}
I'd also recommend that you make a class BookStore
or ProductManager
or something of the like that defines things your program does as a whole. It may contain a native datastructure that represents it nicely (maybe Map<int, BookStoreEntity>
where int is the product id). I recommend that you do not subclass the datastructure in this case.
Upvotes: 0
Reputation: 329
Apparently , your problem is not the access method of an element. So any container type (List, Map, Vector etc.) may suit your access needs. More clear and comprehensive statement of problem , I guess , is that if a general treatment of objects is issued in context of object-oriented paradigm.
However, objects of any class can be put together in any generic container without any abstraction (via inheritance, interfaces or abstract classes) since all classes extends Object class by default. If any general treatment , like operating same operation on each element , is not required, as the title of question suggests , this can used as well : ArrayList<Object> bookStore
.
But , most of people find this method useless since it doesn't make any sense when regarding these objects are put together to be used in same way. I guess , you should work out to determine the ways of general treatments of these objects and then declare some properties. After that create a parent class and define the container with it.
Upvotes: 0
Reputation: 4324
You can create something like BookstoreEntity
abstract super class (or interface) with the common characteristics of the items you want to operate on. Like color
, material
, price
etc...
You can define the setters and getters method in the abstract super class. A list of BookstoreEntity
objects will hold anything that IS-A BookstoreEntity
.
You can operate on the common methods (defined in the super class) in a for loop for example (like getColor
, getPrice
etc...)
for(BookstoreEntity be: listOfItems) {
be.getColor();
be.getPrice();
//etc...
}
Regards!
Upvotes: 0
Reputation: 115328
First, I'd recommend you to use generics. If for example you are going to define abstract class (or probably better interface Item
) so create new ArrayList<Item>
, so you will not be able to add there anything except Item
.
Second, use interface. You definition should look like:
List<? extends Item> lll = new ArrayList<Item>();
The choice of the main data structure depends on your needs. I believe that you are going to perform some kind of search mechanisms. In this case probably you can use Map
or combine list where you store all objects sequentially and several maps that help you to perform search by different parameters.
Anyway you should wrap your data structure by some class (model) that provides basic functionality of your business logic, so if you want to change something you can do it within this class without any changes in other parts of your application.
Upvotes: 1
Reputation: 346
The data structure is quite dependent on what you need to do with the data. If all you're going to do is loop over all the objects calling your toString method, then go for it. If you need to do something with your data for which an ArrayList becomes deficient, then it's best to start looking at your other options.
Upvotes: 0