本文介绍了如何用相同的方法“包装”两个类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须用相同的方法处理两个类,但是它们不实现相同的接口,也不会扩展相同的超类。我不能/不允许改变这个类,我不构造这个类的实例,我只得到这个对象。
什么是避免大量代码重复的最佳方法?



其中一个类:

  package faa; 

public class SomethingA {

private String valueOne = null;
private String valueTwo = null;

public String getValueOne(){return valueOne; }
public void setValueOne(String valueOne){this.valueOne = valueOne; }

public String getValueTwo(){return valueTwo; }
public void setValueTwo(String valueTwo){this.valueTwo = valueTwo; }
}

另一个...

  package foo; 

public class SomethingB {

private String valueOne;
private String valueTwo;

public String getValueOne(){return valueOne; }
public void setValueOne(String valueOne){this.valueOne = valueOne; }

public String getValueTwo(){return valueTwo; }
public void setValueTwo(String valueTwo){this.valueTwo = valueTwo;
}

(实际上这些类更大)



我现在唯一的想法是创建一个包装类,这是:

  public class SomethingWrapper { 

私人SomethingA someA;
私人SomethingB someB;

public SomethingWrapper(SomethingA someA){
// null check ..
this.someA = someA;
}

public SomethingWrapper(SomethingB someB){
// null check ..
this.someB = someB;
}

public String getValueOne(){
if(this.someA!= null){
return this.someA.getValueOne();
} else {
返回this.someB.getValueOne();
}
}

public void setValueOne(String valueOne){
if(this.someA!= null){
this.someA.setValueOne valueOne);
} else {
this.someB.setValueOne(valueOne);
}
}

public String getValueTwo(){
if(this.someA!= null){
return this.someA.getValueTwo() ;
} else {
return this.someB.getValueTwo();
}
}

public void setValueTwo(String valueTwo){
if(this.someA!= null){
this.someA.setValueTwo valueTwo);
} else {
this.someB.setValueTwo(valueTwo);
}
}
}

但我不是真的满意这个解决方案。有没有更好/更优雅的方式来解决这个问题?

解决方案

更好的解决方案是创建一个界面来表示统一接口到两个类,然后写两个实现接口的类,一个包装一个A,另一个包装一个B:

  public interface SomethingWrapper {
public String getValueOne();
public void setValueOne(String valueOne);
public String getValueTwo();
public void setValueTwo(String valueTwo);
};

public class SomethingAWrapper实现SomethingWrapper {

private SomethingA someA;

public SomethingWrapper(SomethingA someA){
this.someA = someA;
}

public String getValueOne(){
return this.someA.getValueOne();
}

public void setValueOne(String valueOne){
this.someA.setValueOne(valueOne);
}

public String getValueTwo(){
return this.someA.getValueTwo();
}

public void setValueTwo(String valueTwo){
this.someA.setValueTwo(valueTwo);
}
};

然后另一个类就像SomethingBWrapper一样。


I have to handle two classes with identical methods but they don't implement the same interface, nor do they extend the same superclass. I'm not able / not allowed to change this classes and I don't construct instances of this classes I only get objects of this.What is the best way to avoid lots of code duplication?

One of the class:

package faa;

public class SomethingA {

private String valueOne = null;
private String valueTwo = null;

public String getValueOne() { return valueOne; }
public void setValueOne(String valueOne) { this.valueOne = valueOne; }

public String getValueTwo() { return valueTwo; }
public void setValueTwo(String valueTwo) { this.valueTwo = valueTwo; }
}

And the other...

package foo;

public class SomethingB {

private String valueOne;
private String valueTwo;

public String getValueOne() { return valueOne; }
public void setValueOne(String valueOne) { this.valueOne = valueOne; }

public String getValueTwo() { return valueTwo; }
public void setValueTwo(String valueTwo) { this.valueTwo = valueTwo; }
}

(In reality these classes are larger)

My only idea is now to create a wrapper class in this was:

public class SomethingWrapper {

private SomethingA someA;
private SomethingB someB;

public SomethingWrapper(SomethingA someA) {
    //null check..
    this.someA = someA;
}

public SomethingWrapper(SomethingB someB) {
    //null check..
    this.someB = someB;
}

public String getValueOne() {
    if (this.someA != null) {
        return this.someA.getValueOne();
    } else {
        return this.someB.getValueOne();
    }
}

public void setValueOne(String valueOne) {
    if (this.someA != null) {
        this.someA.setValueOne(valueOne);
    } else {
        this.someB.setValueOne(valueOne);
    }
}

public String getValueTwo() {
    if (this.someA != null) {
        return this.someA.getValueTwo();
    } else {
        return this.someB.getValueTwo();
    }
}

public void setValueTwo(String valueTwo) {
    if (this.someA != null) {
        this.someA.setValueTwo(valueTwo);
    } else {
        this.someB.setValueTwo(valueTwo);
    }
}
}

But I'm not realy satisfied with this solution. Is there any better / more elegant way to solve this problem?

解决方案

A better solution would be to create an interface to represent the unified interface to both classes, then to write two classes implementing the interface, one that wraps an A, and another that wraps a B:

public interface SomethingWrapper {
    public String getValueOne();
    public void setValueOne(String valueOne);
    public String getValueTwo();
    public void setValueTwo(String valueTwo);
};

public class SomethingAWrapper implements SomethingWrapper {

    private SomethingA someA;

    public SomethingWrapper(SomethingA someA) {
        this.someA = someA;
    }

    public String getValueOne() {
        return this.someA.getValueOne();
    }

    public void setValueOne(String valueOne) {
        this.someA.setValueOne(valueOne);
    }

    public String getValueTwo() {
        return this.someA.getValueTwo();
    }

    public void setValueTwo(String valueTwo) {
        this.someA.setValueTwo(valueTwo);
    }
};

and then another class just like it for SomethingBWrapper.

这篇关于如何用相同的方法“包装”两个类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-15 08:54