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

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

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

问题

以下是代码的翻译部分:

  1. Class A {
  2. boolean someVariable;
  3. String anotherVariable;
  4. //getters and setters
  5. }
  6. Class B {
  7. boolean someVariable;
  8. String anotherVariable;
  9. //getters and setters
  10. }
  11. Class LetsSayMainClass {
  12. //init instances for A and B class
  13. performThis(a);
  14. performThis(b);
  15. private boolean performThis(Object obj) {
  16. if (obj instanceof A)
  17. //使用A类型强制转换将变量赋值为obj的值
  18. else if (obj instanceof B)
  19. //使用B类型强制转换将变量赋值为obj的值
  20. if (obj.getSomeVariable)
  21. //做一些操作
  22. if (obj.anotherVariable.isEmpty())
  23. //做一些操作
  24. }
  25. }

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

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

  1. private boolean performThis(Object obj) {
  2. if (obj instanceof A)
  3. obj = (A) obj;
  4. else if (obj instanceof B)
  5. obj = (B) obj;
  6. if (obj.getSomeVariable)
  7. //做一些操作
  8. if (obj.anotherVariable.isEmpty())
  9. //做一些操作
  10. }

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

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

英文:

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

  1. Class A {
  2. boolean someVariable;
  3. String anotherVariable;
  4. //getters and setters
  5. }
  6. Class B {
  7. boolean someVariable;
  8. String anotherVariable;
  9. //getters and setters
  10. }
  11. Class LetsSayMainClass {
  12. //init instances for A and B class
  13. performThis(a);
  14. performThis(b);
  15. private boolean performThis(Object obj) {
  16. if (obj instanceof A)
  17. //assign a variable with the value of obj type casting to A
  18. else if (obj instanceof B)
  19. //assign a variable with the value of obj type casting to B
  20. if (obj.getSomeVariable)
  21. //Do Something
  22. if (obj.anotherVariable.isEmpty())
  23. //Do Something
  24. }
  25. }

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

  1. private boolean performThis(Object obj) {
  2. if (obj instanceof A)
  3. obj = (A) obj;
  4. else if (obj instanceof B)
  5. obj = (B) obj;
  6. if (obj.getSomeVariable)
  7. //Do Something
  8. if (obj.anotherVariable.isEmpty())
  9. //Do Something
  10. }

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

以下是翻译好的内容:

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

  1. class AorB {
  2. A a;
  3. B b;
  4. public AorB(Object o) {
  5. if (o instanceof A) {
  6. a = (A) o;
  7. } else if (o instanceof B) {
  8. b = (B) o;
  9. } else {
  10. // 抛出异常?
  11. }
  12. }
  13. public boolean getSomeVariable() {
  14. return isA()
  15. ? a.someVariable
  16. : b.someVariable;
  17. }
  18. public String getAnotherVariable() {
  19. return isA()
  20. ? a.anotherVariable
  21. : b.anotherVariable;
  22. }
  23. public boolean isA() {
  24. return b == null;
  25. }
  26. // 其余部分
  27. }

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

  1. private boolean performThis(Object obj) {
  2. AorB aOrB = new AorB(obj);
  3. if (aOrB.getSomeVariable())
  4. System.out.println(aOrB.getAnotherVariable());
  5. if (!aOrB.getAnotherVariable().isEmpty())
  6. System.out.println(aOrB.getAnotherVariable());
  7. return aOrB.isA();
  8. }

并且可以像这样使用它:

  1. class Main {
  2. public static void main(String[] args) {
  3. A a = new A();
  4. a.someVariable = true;
  5. a.anotherVariable = "This is an A";
  6. B b = new B();
  7. b.someVariable = true;
  8. b.anotherVariable = "This is a B";
  9. LetsSayMainClass lsmc = new LetsSayMainClass();
  10. lsmc.performThis(a); // 输出 "This is an A" 2次
  11. lsmc.performThis(b); // 输出 "This is a B" 2次
  12. }
  13. }
英文:

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.

  1. class AorB {
  2. A a;
  3. B b;
  4. public AorB(Object o) {
  5. if (o instanceof A) {
  6. a = (A) o;
  7. } else if (o instanceof B) {
  8. b = (B) o;
  9. } else {
  10. // throw something?
  11. }
  12. }
  13. public boolean getSomeVariable() {
  14. return isA()
  15. ? a.someVariable
  16. : b.someVariable;
  17. }
  18. public String getAnotherVariable() {
  19. return isA()
  20. ? a.anotherVariable
  21. : b.anotherVariable;
  22. }
  23. public boolean isA() {
  24. return b == null;
  25. }
  26. // rest
  27. }

That would allow you to define performThis like so:

  1. private boolean performThis(Object obj) {
  2. AorB aOrB = new AorB(obj);
  3. if (aOrB.getSomeVariable())
  4. System.out.println(aOrB.getAnotherVariable());
  5. if (!aOrB.getAnotherVariable().isEmpty())
  6. System.out.println(aOrB.getAnotherVariable());
  7. return aOrB.isA();
  8. }

And use it like so:

  1. class Main {
  2. public static void main(String[] args) {
  3. A a = new A();
  4. a.someVariable = true;
  5. a.anotherVariable = "This is an A";
  6. B b = new B();
  7. b.someVariable = true;
  8. b.anotherVariable = "This is a B";
  9. LetsSayMainClass lsmc = new LetsSayMainClass();
  10. lsmc.performThis(a); // prints "This is an A" 2x
  11. lsmc.performThis(b); // prints "This is a B" 2x
  12. }
  13. }

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:

确定