3

I'm facing the following code:

public class BaseGroup {

    private Group1 group1;
    private Group2 group2;
    private Group3 group3;

    public void setGroup (IGroup group) {

        if(group instanceof Group1) {
            setGroup1((Group1) group);
        } else if(group instanceof Group2) {
            setGroup2((Group2) group);
        } else {
            setGroup3((Group3) group);
        }
    }

    public Group1 getGroup1() {
        return group1;
    }

    public void setGroup1(Group1 group1) {
        this.group1 = group1;
    }

    public Group2 getGroup2() {
        return group2;
    }

    public void setGroup2(Group2 group1) {
        this.group2 = group2;
    }

    public Group3 getGroup3() {
        return group3;
    }

    public void setGroup3(Group3 group1) {
        this.group3 = group3;
    }

}   

And the BaseGroup class is used in this way.

BaseGroup baseGroup = New BaseGroup();
basegroup.setGroup(group);

My question is about this chain of "instanceof's" calling the respective setters to assemble the BaseGroup object. What is the better approach to do this?

csgui
  • 151
  • 5
  • 1
    What about creating an overloaded setGroup method for each IGroup implementation? – pcjuzer Nov 23 '11 at 14:04
  • @pcjuzer: Keep in mind that overloading used static binding, so you could not use it when the static type of the object you use is IGroup, which is quite likely. – pushy Nov 23 '11 at 14:10

7 Answers7

5

You can add a method to

interface IGroup {
    public void addToGroup(BaseGroup bg);
}

class Group1 implements IGroup {
    public void addToGroup(BaseGroup bg) { bg.setGroup1(this); }
}
// etc for Group2 and 3.


IGroup group;
BaseGroup bg;
group.addToGroup(bg);
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
1

Depends on how you use group1, 'group2', 'group3'. Something like that could work:

class BaseGroup {
    private final List<Group> groups = new ArrayList<Group>();

    public void addGroup(Group group) {
         groups.add(group);
    }
}
denis.solonenko
  • 11,645
  • 2
  • 28
  • 23
1

Polymorphism (specifically method overloading) may actually work here.

public void setGroup(Group1 group) {
  this.group1 = group;
}
public void setGroup(Group2 group) {
  this.group2 = group;
}
public void setGroup(Group3 group) {
  this.group3 = group;
}

Java will automatically select the appropriate method.

Jaco Van Niekerk
  • 4,180
  • 2
  • 21
  • 48
  • 1
    Moreover, you might want get rid of the `IGroup` interface. At least from the question itself, I do not see its point if you have to handle implementations separately. – Matthias Meid Nov 23 '11 at 14:26
  • This does not work without casting to one of the `Group?` classes: the compiler does not know what is the runt-time class of the interface - still must use `instanceof` with that. – user85421 Nov 23 '11 at 14:38
0

Another alternative is using Reflection.

Mechkov
  • 4,294
  • 1
  • 17
  • 25
  • Take a look at the idea here. Look for Jordao's reply..http://stackoverflow.com/questions/2790144/avoiding-instanceof-in-java – Mechkov Nov 23 '11 at 14:38
0
public class BaseGroup {

private Map<Class<IGroup>, IGroup> groupsFactoryByClass;

public void setGroup (IGroup group) {
    IGroup oldGroup = groupsFactoryByClass.put(group.getClass());
    if (oldGroup != null)
        throw new IllegalArgumentException("Group with class " + group.getClass().getName() + " already set");
}

public <T implements IGroup> getGroup(Class<T> klazz) {
    return groupsFactoryByClass.get(klazz);
}
}
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
0
interface Group{}
class Group1 implements Group{}
class Group2 implements Group{}
class Group3 implements Group{}


public class BaseGroup {

    private Map<Class<Group>, <Group>>  groups = new HashMap<Class<Group>, <Group>>();

    public void setGroup (Group group) {
        groups.put(group.getClass(), group);
    }

}
AlexR
  • 114,158
  • 16
  • 130
  • 208
-1

The best thing to avoid instance of is visitor pattern please use that one.

Abhishek
  • 575
  • 3
  • 5
  • Are you trying to compare patterns usability with oops concept ????? Ru sure...what r u trying to do????? – Abhishek Nov 23 '11 at 18:44