-1

I'm probably stumbling on weak OO bases but how can I load elegantly an arraylist of my custom object trough a function in my main program?

I'd like it to look like it from the main:

ArrayList<MyObject> mylist = new ArrayList<MyObject>();
mylist.FetchFromDb();

I can't figure what would be the right thing to do:

  • extending the ArrayList class seems bad
  • method in main passed with the arraylist as an argument seems ugly
  • method in MyObject class doesn't work since mylist is an instance of arraylist

Of course i would be making connection to db and iterating over my resultset in that function, which I have no problem with for a standard object.

Cœur
  • 37,241
  • 25
  • 195
  • 267
kal
  • 1
  • How are the objects stored? – bradimus Feb 18 '16 at 15:12
  • I would suggest you to create a separate class that serves as Data Access Object (DAO). write your method fetchFromDb() in this class and populate list inside this method and return this list to main. – Jimmy Feb 18 '16 at 15:15
  • @Jimmy Probably the best way to go about it. OOP isn't *always* the best approach, especially when a method doesn't really seem to fit anywhere. You should propose that as an answer. – Neil Feb 18 '16 at 15:17
  • @bradimus : inside mysql db, i use jdbc and i iterate over the resultset, i'd like to hide that inside a function – kal Feb 18 '16 at 15:17
  • @Jimmy : thank you, I'll give that a try – kal Feb 18 '16 at 15:19

3 Answers3

0

If you want to have a list that has such method, you should create your own class

class MyList<T> {

    private ArrayList<T> storage = new ArrayList<>();

    public void fetchFromDb() {
        // ...
    }

}

However I think you should create a separate function that fills the list, as a list function is not to fetch data from a database, but to provide functionality to store and retrieve elements.

Nadir
  • 1,799
  • 12
  • 20
  • It doesn't make much sense for MyList to be generic if it is meant to load the entities from the database. – Neil Feb 18 '16 at 15:15
  • It doesn't make sense to have such kind of class, but what if he wants to create subclasses that will fetch different kind of objects? – Nadir Feb 18 '16 at 15:16
  • Then MyList would be abstract? An abstract class that does nothing other than hold a List of type T and require that subclasses implement fetchFromDb? Seems a bit of an overreach, wouldn't you say? – Neil Feb 18 '16 at 15:20
  • Having this kind of classes is already an overreaching in OOP – Nadir Feb 18 '16 at 15:23
  • Then you should probably mention it in your answer. The right answer here isn't the one that shows the OP how to do it his way but the one that shows the best practice approach and the one that will benefit the most number of people reading it. – Neil Feb 18 '16 at 15:26
  • You didn't mention that these kinds of classes are overreaching in OOP, and it is not clear to me what you mean by a separate function. Technically fetchFromDb in MyList in the example that you wrote is a separate function. Did you mean a static method? – Neil Feb 18 '16 at 15:29
  • I said what a list functionality should be, and a separate function means to do in some place where it makes sense. For me it looked clear, but if OP needs a deeper explanation I'll post it – Nadir Feb 18 '16 at 15:35
0

You can't achieve that in Java without extending ArrayList or (even uglier, using delegate objects).

ernest_k
  • 44,416
  • 5
  • 53
  • 99
  • It isn't possible in any other way as the OP has written it, no, however perhaps you could propose some alternative approach instead? – Neil Feb 18 '16 at 15:18
0

Not to say you cannot do this but you are talking about adding a method FetchFromDb() to Java's ArrayList object.

What would seem more appropriate to do is add to your object a FetchFromDb() which returns a list of the MyObject and then initialize the ArrayList with that.

public class MyObject{
    //...
    public static ArrayList<Node> FetchFromDb(){
        //Code to get all the objects and add them to a list
        //Return the list
    }
}

Then you can call the static method

ArrayList<MyObject> mylist = MyObject.FetchFromDb();

The other option is to use a DAO which others have suggested and you can read about here

Community
  • 1
  • 1
nerdlyist
  • 2,842
  • 2
  • 20
  • 32
  • I've seen it done where entity classes will have static DAO methods regarding that entity like fetching and updates and the like. It is not a bad approach, but only if the class doesn't get cluttered. Did you mean for FetchFromDb to be static or not? – Neil Feb 18 '16 at 15:23
  • My answer was mostly to get away from the idea of extending the ArrayList object and showing to add it to the Object that OP was implementing. Yes I believe that the method in this instance would be static but I did not show implementation. Also, as some have stated a DAO would be a good idea. – nerdlyist Feb 18 '16 at 15:31
  • Seems the best option: i used DAO model and made classes as such : DAO, Object, ObjectDAO and Connect ; code is well hidden and i'm calling DAO foo = new ObjectDAO(Connect.getConnection()); ArrayList shinynewlist = foo.FetchFromDb(); – kal Feb 19 '16 at 11:19