1

Let's say that I have an ArrayList of class Person and I have two objects that are generated from two different classes which read and write to this ArrayList.

For example,

public class Main { 
   public static void main(String[] args) { 
       A a = new A();
       B b = new B();
   }
}

What, in your opinion is the best design to handle this ArrayList. I can think about two options:

  1. create the Array List in class Main:

    public class Main { 
       public static ArrayList<Person> list = new ArrayList<>();
    
       public static void main(String[] args) { 
           A a = new A();
           B b = new B();
       }
    }
    

    and then access the list inside classes A and B via Main.list.

  2. Create the ArrayList as a local variable in main method and send to the constructor of A and B.

    public class Main {
        public static void main(String[] args) {
            ArrayList<Person> list = new ArrayList<>(); 
            A a = new A(list);
            B b = new B(list);
        }
    }
    
    public class A { 
        private ArrayList<Person> list;
        public A(ArrayList<Person> list) { 
           this.list = list;
        }
    }
    

    and then list is an attribute of objects A and B.

  • 2
    None of them. Try this: A loops in the array list. Meanwhile, B adds, removes or modify entries. In these cases, you want a single access point for the list. – Laiv May 27 '17 at 14:31
  • Can you show a small code example? – CrazySynthax May 27 '17 at 14:32
  • @Laiv Not necessarily. First, there is an issue only if a and b access the list from separate threads. Second, since this is not language-specific ArrayList could be a thread-safe container. – Rado May 27 '17 at 15:54
  • By the syntaxis, I assumed it was Java. In Java, (I think) ArrayList are not thread safe. On the other hand, if the ArrayList is a global resource, nothing is preventing you from accessing the list from different threads. – Laiv May 27 '17 at 15:56
  • @Rado, how can you guarantee, that this publicly accessible list is only accessed by classes you know of? – Basilevs May 27 '17 at 16:08
9

With option 1 your list is actually global mutable state which is usually regarded as a Bad Thing. Passing dependencies to constructors as you do in option 2 is the right thing to do.

There could be issues with your option 2 like concurrency as mentioned by Laiv in a comment, main could not be the best place for this code or you might want to use factories to instantiate A or B to name a few, but you didn't provide enough context to assess these potential problems. As it is written, option 2 looks fine to me.

  • The problem with solution 2 is that if someone reads the code of class A, he/she may think mistakenly that only class A may affect the content of the list (only if the reader pays attention to the constructor, he/she may figure out that it's a dependency injection). Solution 2 is also a global state, since the list is placed in the "area" of class Main, and classes A and B only look at it. Solution 1, however, clarifies that the list is a global state, and whenever I see such a solution, it's very clear that the list content is not affected only by class A. – CrazySynthax May 27 '17 at 15:42
  • 3
    @CrazySynthax That would be an incorrect assumption since the list is not private to A, but comes in as a parameter. The 2nd option is preferable since it clearly states the dependencies of your classes and makes them testable. – Rado May 27 '17 at 15:56
  • Why? If we use solution 2, then class 'A' may have a private variable ArrayList<Person> and its constructor will assign the "global" state to the private variable (see edited post). – CrazySynthax May 27 '17 at 16:00
  • Because the A' private attribute is just a reference to the very same ArrayList in B. This is what we know as parameters by reference (opposite to parameters by value). The private's A list is not a copy. It's always the same list. – Laiv May 27 '17 at 16:14
  • 2
    @CrazySynthax You are not responsible for other people making unjustified assumptions. And no, the list declared inside Main.main in option 2 is not global state. As you correctly stated in your question it is a local variable. – Goyo May 27 '17 at 16:27
1

I've seen this handled with a read lock and a write lock. When a write lock is held by one thread, the read lock cannot be obtained by any thread, and no other thread may obtain the write lock. When one thread holds the read lock, other threads may also get the read lock, but no thread may get the write lock. The data can only be mutated when one thread holds the write lock (and by design no threads hold a read lock). This ensures that nobody mutates the data while it is being read. Also, in this scenario, there was only a single thread that was ever able to obtain the write lock. (It was the main UI thread because the data was mutated by the user making changes to the UI.) A similar architecture might work for your scenario.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.