Where to modify a list in java based on different object types
<p>Long time reader/first time poster. I'm new to java and software development so forgive me if this is a terrible question!</p>

<p>I currently have a class called ItemsOrderManager that has a List of Item Objects it maintains, call it ItemsOrder. This list is instantiated with some default items in the correct order when the ItemsOrderManager Class is constructed.</p>

<p>I then have another class called Timeline that has a HashMap of Events where the Date Time of that Event is the key of the HashMap (I.e. HashMap Date, Event). Event has a handful of subclasses that extend it (i.e. EventA, EventB, EventC etc.), due to them doing different things/changing slightly from each other/but sharing similar methods/attributes.</p>

<p>My problem is the ItemsOrderManager class will need to process the Timeline of Events in Date order, these Events decide how the ItemsOrder List will be modified. It will do things like find a particular Item in the list and then:</p>

<ul>
<li>change some properties of the Item, OR</li>
<li>add a new Item before it,</li>
<li>add a new Item after it.</li>
</ul>

<p>How it does that changes depending on what type of Event it is.</p>

<p>It will look something like this:</p>

<pre><code>public class ItemsOrderManager {

    private List&lt;Item&gt; items = new ArrayList&lt;Item&gt;();
    private Timeline timeline;

    Public ItemsOrderManager() {
        initialiseItems(); //add items to the items list
        initialiseTimeline(); //for simplicity i put this here to show the timeline gets initialised/we set the events in it and then sort the hashmap
        processTimeline(); //again for simplicity just showing we need to then process the timeline which modifies the items list
    }
}
</code></pre>

<p>So my question is should I be manipulating the ItemsOrder List by using methods in the ItemsOrderManager class or should I be making use of a method in the Event class/subclass that takes in the list as a parameter and does the operating there? Or this there another way of doing this? What is the best design practice here?</p>

<p>So either:</p>

<pre><code>public void processTimeline() {
    for (Event event : timeline.getEvents().values()) {
        if (event instanceof EventA) {
            //manipulate items list
        } else if (event instanceof EventB) {
            //manipulate items list in a different way
        } else {
            //manipulate items list in a different way altogether
        }
    }
}
</code></pre>

<p>Or should I be doing this:</p>

<pre><code>public void processTimeline() {
    for (Event event : timeline.getEvents().values()) {
        //call something like event.applyEventToList(this.items); to modify the list or returned the modified list
    }
}
</code></pre>

<p>Or is there a better way? Apologies in advance if its a dumb question.</p>