English 中文(简体)
1. “instanceof s”链条,用以组装物体
原标题:Chain of "instanceof s" to assemble an object

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

我的问题是这一链条,即要求各组装组标的“instanceof s”。 这样做的更好办法是什么?

问题回答

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

页: 1 这样做可以奏效:

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

    public void addGroup(Group group) {
         groups.add(group);
    }
}

聚合物(特定方法超负荷)实际上可能在此发挥作用。

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.

另一种选择是利用反思。

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

}

避免出现这种情况的最佳办法是访问模式。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签