处理两个不同对象调用相同函数的方法

huangapple go评论79阅读模式
英文:

How to Handle two different Objects for same Function

问题

我有以下两个对象之一,ObjOneObjTwo,它们都共享相似的 getter 和 setter 方法,它们将传递给我的函数。

目前我有一个中间件,一个映射器,在内部方法之间使用,但可能有一种更清晰的方法来做到这一点,而不需要映射器,但缺乏具体的语法。

public String mapper(Object obj){

   Map<String, String> map = new HashMap<>();
   
   if(obj instanceof ObjOne){
      ObjOne obj1 = (ObjOne)obj;
      map.put("firstKey", obj1.getFirstValue());
   }
   else if(obj instanceof ObjTwo){
      ObjTwo obj2 = (ObjTwo)obj;
      map.put("firstKey", obj2.getFirstValue());
   }

   return secondFunction(map);
      
}

private String secondFunction(Map<String, String> map){
   
   return thirdFunction(map.get("firstKey"));
}

在此是否有类似于 (ObjOne || ObjTwo)obj).getFirstValue() 的语法可以传递给 thirdFunction

编辑:我导入了这些对象,所以我不能为它们声明一个父类,它们共享的 getter 和 setter 方法对我的情况很方便。

英文:

I have either one of following Objects, ObjOne and ObjTwo, going into my function, both sharing similar getters/setters.

Currently I have an intermediary, a mapper, used across internal methods, but there might be a cleaner way to do this without a mapper but lacking specific syntax.

public String mapper(Object obj){

   Map&lt;String, String&gt; map = new HashMap&lt;&gt;();
   
   if(obj instanceof ObjOne){
      ObjOne obj1 = (ObjOne)obj;
      map.put(&quot;firstKey&quot;, obj1.getFirstValue());
   }
   else if(obj instanceof ObjTwo){
      ObjTwo obj2 = (ObjTwo)obj
      map.put(&quot;firstKey&quot;, obj1.getFirstValue());
   }

   return secondFunction(map);
      
}

private String secondFunction(Map&lt;String, String&gt; map){
   
   return thirdFunction(map.get(&quot;firstKey&quot;));
}

Is there such syntax for (ObjOne || ObjTwo)obj).getFirstValue() to feed into thirdFunction herein?

Edit: I imported these Objects, so I can't declare a parent class for them, they do share getters/setters that are convenient for my scenario.

答案1

得分: 1

更面向对象的方法是将那些你无法控制的对象组合到一个你可以控制的新对象中,然后根据你控制的对象编写你的API。

final class ObjOne {
    String getFirstValue() {
        return "foo";
    }
}

final class ObjTwo {
    String getFirstValue() {
        return "bar";
    }
}

class MyAdapter {
    final Map<String, String> map = new HashMap<>();

    MyAdapter(ObjOne o1) {
        this(o1.getFirstValue());
    }

    MyAdapter(ObjTwo o2) {
        this(o2.getFirstValue());
    }

    MyAdapter(String firstKey) {
        map.put("firstKey", firstKey);
    }
}

public String secondFunction(MyAdapter adapter) {
    return thirdFunction(adapter.map.get("firstKey"));
}
英文:

A more OO approach is to compose the objects you don't control within a new object that you do control. Then write your API in terms of the object you control.

final class ObjOne {
    String getFirstValue() {
        return &quot;foo&quot;;
    }
}

final class ObjTwo {
    String getFirstValue() {
        return &quot;bar&quot;;
    }
}

class MyAdapter {
    final Map&lt;String, String&gt; map = new HashMap&lt;&gt;();

    MyAdapter(ObjOne o1) {
        this(o1.getFirstValue());
    }

    MyAdapter(ObjTwo o2) {
        this(o2.getFirstValue());
    }

    MyAdapter(String firstKey) {
        map.put(&quot;firstKey&quot;, firstKey);
    }
}

public String secondFunction(MyAdapter adapter) {
    return thirdFunction(adapter.map.get(&quot;firstKey&quot;));
}

答案2

得分: 0

建议不要传递对象,而是像这样创建一个基本模型并使用多态性。例如。

abstract class BaseObj {
    abstract public String getFirstValue();
}

class ObjOne extends BaseObj {

    @Override
    public String getFirstValue() {
        return "something useful";
    }
}

class ObjTwo extends BaseObj {

    @Override
    public String getFirstValue() {
        return "something useful";
    }
}

不确定这里的用例是什么但您总是可以相应地进行调整

public String mapper(BaseObj obj) {
    Map<String, String> map = new HashMap<>();
    map.put("firstKey", obj.getFirstValue()); //常见的函数调用
    return secondFunction(map);

}

private String secondFunction(Map<String, String> map) {

    return thirdFunction(map.get("firstKey"));
}

private String thirdFunction(String firstKey) {
    return null;
}
英文:

One suggestion don't pass Object instead do something like this create a base model use here polymorphism. for example.

abstract class BaseObj {

    abstract public String getFirstValue();
}

class ObjOne extends BaseObj{


    @Override
    public String getFirstValue() {
        return  &quot;something useful&quot;;
    }
}

class ObjTwo extends BaseObj{


    @Override
    public String getFirstValue() {
        return  &quot;something useful&quot;;
    }
}

not sure what is the use case here but you can always mold accordingly.

public String mapper(BaseObj obj){
    Map&lt;String, String&gt; map = new HashMap&lt;&gt;();
    map.put(&quot;firstKey&quot;, obj.getFirstValue()); //common function call
    return secondFunction(map);

}

private String secondFunction(Map&lt;String, String&gt; map){

    return thirdFunction(map.get(&quot;firstKey&quot;));
}

private String thirdFunction(String firstKey) {
    return null;
}

huangapple
  • 本文由 发表于 2020年7月29日 01:23:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/63139581.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定