I do have an implementation issue that I condensed to the following code snippet, because the real code is much more complicated. The core problem is that I do have a container object that has a mixed list of persons, for example Managers and Engineers:interface Person {}class Manager implements Person {}class Engineer implements Person {}In the most cases I have to access the list of persons without knowing the concrete subclass of a person. But in some methods I have to know the concrete type of the members, for example to count the members or to add the members. In both implementations I do have ugly code:In implementation A it's the code to separate the person-types for counting.class EnterpriseA {    private final List<Person> members = new ArrayList<Person>();    public void addMember(Person person) {        this.members.add(person);    }    public void doSomethingWithManagers() {        // separate by instanceof    }    public void doSomethingWithEngineers() {        // separate by instanceof    }}In implementation B it's the code to add a person to the right list of Manager or Engineer.class EnterpriseB {    private final List<Manager> managers = new ArrayList<Manager>();    private final List<Engineer> engineers = new ArrayList<Engineer>();    public void addMember(Person person) {        if (person instanceof Manager) {            this.managers.add((Manager) person);        } else if (person instanceof Engineer) {            this.engineers.add((Engineer) person);        }    }    public void doSomethingWithManagers() {        // iterate over the managers.    }    public void doSomethingWithEngineers() {        // iterate over the engineers.    }}How can I improve the code and avoid the ugly code in addMember() and countManagers() respectively? Smart suggestions are welcome.Which alternative would you prefer? Please give reasons, in which cases you would prefer which solution.
How to improve code that has to handle list-objects in general and special cases?
class design;clean code