“实例”链组装一个物体

发布于 2024-12-17 10:14:51 字数 1049 浏览 3 评论 0原文

我面临以下代码:

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

}   

BaseGroup 类就是以这种方式使用的。

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

我的问题是关于调用相应设置器来组装 BaseGroup 对象的“instanceof”链。执行此操作的更好方法是什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

你的他你的她 2024-12-24 10:14:51

您可以添加一个方法

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

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);
油焖大侠 2024-12-24 10:14:51

取决于您如何使用group1、'group2'、'group3'。类似的东西可以工作:

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

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

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);
    }
}
为你拒绝所有暧昧 2024-12-24 10:14:51

多态性(特别是方法重载)实际上可能在这里起作用。

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会自动选择合适的方法。

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.

风和你 2024-12-24 10:14:51

另一种选择是使用反射。

Another alternative is using Reflection.

南七夏 2024-12-24 10:14:51
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);
}
}
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);
}
}
陌路黄昏 2024-12-24 10:14:51
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);
    }

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

}
反话 2024-12-24 10:14:51

最好避免的情况是访问者模式,请使用该模式。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文