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?

2
  • 1
    What about creating an overloaded setGroup method for each IGroup implementation? Commented Nov 23, 2011 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. Commented Nov 23, 2011 at 14:10

7 Answers 7

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); 
Sign up to request clarification or add additional context in comments.

Comments

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); } } 

Comments

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.

2 Comments

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.
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.
0

Another alternative is using Reflection.

1 Comment

Take a look at the idea here. Look for Jordao's reply..stackoverflow.com/questions/2790144/avoiding-instanceof-in-java
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); } } 

Comments

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); } } 

Comments

-1

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

1 Comment

Are you trying to compare patterns usability with oops concept ????? Ru sure...what r u trying to do?????

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.