0

I really do not know if the title is appropriate or not.

I have 2 options: OPTION 1:

Class A {
   ArrayList<B> BList = new ArrayList<B>();

   public B[] getBList() {
       return (B[])BList.toArray();
   }
}

Class Facade {
   public B[] getAllB(){
       A a = new A();
       return a.getBList();
   }
}

OPTION 2:

Class A {
   ArrayList<B> BList = new ArrayList<B>();

   public ArrayList getBList() {
       return BList;
   }
}

Class Facade {
   public B[] getAllB(){
       A a = new A();
       ArrayList BList = a.getBList();
       return (B[])BList.toArray();
   }
}

Which option should i use ?

1
  • The array returned by ArrayList.toArray will actually have a runtime type of Object[]. Casting it may have unexpected consequences... Commented Jul 18, 2010 at 13:55

4 Answers 4

3

Use collection classes, unless you have a specific reason to use arrays. So, I'd choose option 2.

There are a number of other comments that can be made about your code.

First, It's better to program to an interface, not an implementation - make the member variable BList in class A a List<B> instead of an ArrayList<B>. Also, member variables should be private (unless there's a very good reason for them not to be private). Third, use generics whenever possible - why does your method getBList() return a raw ArrayList? The de-facto naming standard for variables in Java is camel case, starting with a lower-case letter. So don't call the member variable BList, but bList (or some other, better name).

class A {
    private List<B> bList = new ArrayList<B>();

    public List<B> getBList() {
        return bList;
    }
}

Is it really necessary for class Facade to return a B[]?

Another point to consider is to make your class A immutable, because unexpected things might happen if you'd get the list from an A object, and then add or remove elements from the list (the list inside your A object will also be changed and that can be confusing). You'd have to return a read-only view of the list in your getBList() method:

public List<B> getBList() {
    return Collections.unmodifiableList(bList);
}
Sign up to request clarification or add additional context in comments.

1 Comment

thanks for that, it is very useful for me. I have modified it :)
1

I don't have a strong preference either way. However, I notice you're using an array to return the list's contents, perhaps in an attempt to make it immutable. I recommend, instead, using Collections.unmodifiableList.

1 Comment

Good basic idea. I much prefer Google's Guava Immutable collections objects, as they are setup for generic type safety.
0

I think the only difference is whether Class A is part of the public interface of your component.

If it's not (e.g. only the Facade counts to the public interface), then it does not matter.

If Class A is part of the public interface, i'd go with Option 1.

1 Comment

Class A is actually hidden from the users, the GUI will interact directly with the facade class
0

Unless you really need too, where the native formt of something is bytes[] for something like images, one should always return Collection types in an API for a many of something rather than an array. In your case wrap the original Collection using Collections.unmodifiableXXX( ...).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.