Let’s say I have multiple Objects to be stored:
Person ------------ Employee ------------ Sales Engineer
| |
Customer Field Engineer
So: Person, Customer, Employee, Sales Engineer, Field Engineer.
I need to keep track of all of these…what is the best way to store them? In an ArrayList? A custom ArrayList?
The way they are stored also may affect future expansion – in the future, these objects might be generated by fields from an SQL Server. (Also, this is an Android App – so that could be a factor.)
You’ll want a
List<Person>. Your diagram suggests inheritance, so you’ll want to have a collection of the super class and let polymorphism do the rest.Your code can do this:
Your design as expressed won’t allow Engineers to be Customers, or Sales engineers to go into the Field, but that’s the curse of inheritance in cases like yours.
A better design, if you need the flexibility, might be to keep the Person class and assign a Person a Role in decorator fashion.
A decorator would add behavior using composition rather than inheritance, like this: