android - Data set for storing objects in java -
let's have multiple objects stored:
person ------------ employee ------------ sales engineer | | customer field engineer
so: person, customer, employee, sales engineer, field engineer.
i need keep track of of these...what best way store them? in arraylist? custom arraylist?
the way stored may affect future expansion - in future, these objects might generated fields sql server. (also, android app - factor.)
you'll want list<person>
. diagram suggests inheritance, you'll want have collection of super class , let polymorphism rest.
your code can this:
list<person> people = new arraylist<person>(); // class extends person can added people.add(new customer()); people.add(new fieldengineer()); (person person : people) { system.out.println(person); }
your design expressed won't allow engineers customers, or sales engineers go field, that's curse of inheritance in cases yours.
a better design, if need flexibility, might keep person class , assign person role in decorator fashion.
a decorator add behavior using composition rather inheritance, this:
public class customer { private person person; public customer(person p) { this.person = p; } public void buyit() { // customer here } } public class fieldengineer { private person person; public fieldengineer(person p) { this.person = p; } public void fixit() { // field engineer here } }
Comments
Post a Comment