如何使用一个通用方法来对两个不同实例的用户对象执行相同的操作?

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

How to have a common method to perform same action with two different instances of user objects?

问题

以下是代码的翻译部分:

Class A {
   boolean someVariable;
   String  anotherVariable;
   //getters and setters
}

Class B {
   boolean someVariable;
   String  anotherVariable;
   //getters and setters
}

Class LetsSayMainClass {
   //init instances for A and B class
   performThis(a);
   performThis(b);

private boolean performThis(Object obj) {
     if (obj instanceof A)
        //使用A类型强制转换将变量赋值为obj的值
     else if (obj instanceof B)
        //使用B类型强制转换将变量赋值为obj的值

     if (obj.getSomeVariable)
       //做一些操作
     if (obj.anotherVariable.isEmpty())
       //做一些操作

   }
}

AB 类都有一些共同的变量和方法,我想对它们进行一些检查。我无法修改这些类以具有共同的可实现接口和工厂以实现更好的实例化方式。所以,在这一点上,由于要执行的检查对这两个类都是共同的,我尝试在 performThis 方法中使用单个实例以避免代码重复。

我不能在 performThis 方法中这样做:

private boolean performThis(Object obj) {
     if (obj instanceof A)
        obj = (A) obj;
     else if (obj instanceof B)
        obj = (B) obj;
     if (obj.getSomeVariable)
       //做一些操作
     if (obj.anotherVariable.isEmpty())
       //做一些操作

   }

使用变量的条件语句会引发错误,因为它们仍将将 obj 视为 Object 的实例,而不是 AB 的实例。

我可以有两个不同的方法来解决这个问题。但是,在现有的设置中有没有避免代码重复的方法呢?

英文:

Here is how the code I am working on is currently looking like:

Class A {
   boolean someVariable;
   String  anotherVariable;
   //getters and setters
}

Class B {
   boolean someVariable;
   String  anotherVariable;
   //getters and setters
}

Class LetsSayMainClass {
   //init instances for A and B class
   performThis(a);
   performThis(b);

private boolean performThis(Object obj) {
     if (obj instanceof A)
        //assign a variable with the value of obj type casting to A
     else if (obj instanceof B)
        //assign a variable with the value of obj type casting to B

     if (obj.getSomeVariable)
       //Do Something
     if (obj.anotherVariable.isEmpty())
       //Do Something

   }
}

The Classes A and B have common variables and methods over which I would like to perform some checks. I cannot alter these classes to have a common implementable interface and factory to instantiate better. So, at this point, as the checks to be done are common for both of these classes, I am trying to use a single instance in the performThis method to avoid code repetition.

I cannot do something like this in the performThis method

private boolean performThis(Object obj) {
     if (obj instanceof A)
        obj = (A) obj;
     else if (obj instanceof B)
        obj = (B) obj;
     if (obj.getSomeVariable)
       //Do Something
     if (obj.anotherVariable.isEmpty())
       //Do Something

   }

The if conditions using the variables would throw error as they would still see the obj as an instance of Object than of either A or B.

I could have two different methods to take care of this issue. But, ts there a way to avoid the code repetition? Something I could use with the existing setup?

答案1

得分: 1

以下是翻译好的内容:

我可能不会这样做,但是如果你坚持不重复,你可以将类型连接作为代理。

class AorB {
  A a;
  B b;

  public AorB(Object o) {
    if (o instanceof A) {
      a = (A) o;
    } else if (o instanceof B) {
      b = (B) o;
    } else {
      // 抛出异常?
    }
  }

  public boolean getSomeVariable() {
    return isA()
      ? a.someVariable
      : b.someVariable;
  }

  public String getAnotherVariable() {
    return isA()
      ? a.anotherVariable
      : b.anotherVariable;
  }

  public boolean isA() {
    return b == null;
  }

  // 其余部分
} 

这将使您能够像这样定义 performThis 方法:

private boolean performThis(Object obj) {
     AorB aOrB = new AorB(obj);      
     if (aOrB.getSomeVariable())
       System.out.println(aOrB.getAnotherVariable());
     if (!aOrB.getAnotherVariable().isEmpty())
       System.out.println(aOrB.getAnotherVariable());
     return aOrB.isA();
 }

并且可以像这样使用它:

class Main {
  public static void main(String[] args) {
    A a = new A();
    a.someVariable = true;
    a.anotherVariable = "This is an A";
    B b = new B();
    b.someVariable = true;
    b.anotherVariable = "This is a B";
    LetsSayMainClass lsmc = new LetsSayMainClass();
    lsmc.performThis(a); // 输出 "This is an A" 2次
    lsmc.performThis(b); // 输出 "This is a B" 2次
  }
}
英文:

I probably wouldn't do this but, if you insist on having no repetition, one thing you can do is join the types as a proxy.

class AorB {
  A a;
  B b;

  public AorB(Object o) {
    if (o instanceof A) {
      a = (A) o;
    } else if (o instanceof B) {
      b = (B) o;
    } else {
      // throw something?
    }
  }

  public boolean getSomeVariable() {
    return isA()
      ? a.someVariable
      : b.someVariable;
  }

  public String getAnotherVariable() {
    return isA()
      ? a.anotherVariable
      : b.anotherVariable;
  }

  public boolean isA() {
    return b == null;
  }

  // rest
} 

That would allow you to define performThis like so:

 private boolean performThis(Object obj) {
     AorB aOrB = new AorB(obj);      
     if (aOrB.getSomeVariable())
       System.out.println(aOrB.getAnotherVariable());
     if (!aOrB.getAnotherVariable().isEmpty())
       System.out.println(aOrB.getAnotherVariable());
     return aOrB.isA();
 }  

And use it like so:

class Main {
  public static void main(String[] args) {
    A a = new A();
    a.someVariable = true;
    a.anotherVariable = "This is an A";
    B b = new B();
    b.someVariable = true;
    b.anotherVariable = "This is a B";
    LetsSayMainClass lsmc = new LetsSayMainClass();
    lsmc.performThis(a); // prints "This is an A" 2x
    lsmc.performThis(b); // prints "This is a B" 2x
  }
}

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

发表评论

匿名网友

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

确定