使用Java中的接口和组合 – 使用不同变量重用代码

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

using interface and composition in java - reuse code with different variables

问题

我在尝试重用代码时遇到了问题。
我想使用主接口和两个类,但其中一些方法具有相同的实现,因此我决定使用组合。
我的意思是,声明“a”类的实例并在方法上执行:

void do_some()
a.do_some()

问题是与一些数据成员相关的函数。
例如:

public class Locker implements GeneralStorage {
    private final int current_capacity;

    public int getAvailableCapacity() {
        return current_capacity;
    }
}

public class LongTermStorage implements GeneralStorage {
    private final int current_capacity;
    private Locker locker = new Locker();

    public int getAvailableCapacity() {
        return locker.getAvailableCapacity();
    }
}

我应该怎么办?(我想返回LongTermStorage.current_capacity

英文:

I have a problem during trying to do reuse code.
I want to use main interface with two classes, but some of the methods have same implements, so I decided to use compostion.
I mean, to declare instance of the "a" class and do on the method:

void do_some()
a.do_some()

the problem is that the function related to some data members.
for example:

 public class Locker implements GeneralStorage {
    private final int current_capacity;

  public int getAvailableCapacity(){
        return current_capacity;
      }
}

public class LongTermStorage implements GeneralStorage {
    private final int current_capacity;
    private Locker locker=New Locker();
       public int getAvailableCapacity(){
           return locker.getAvailableCapacity

what can I do? (I want to return LongTermStorage.current_capacity)

答案1

得分: 0

你可以这样做,但最好在构造函数中为组合创建一个实例。代码如下所示:

public class Locker implements GeneralStorage {
  private final int current_capacity;

  public int getAvailableCapacity(){
    return current_capacity;
  }
}

public class LongTermStorage implements GeneralStorage {
  private Locker locker;

  public LongTermStorage() {
    locker = new Locker();
  }

  public int getAvailableCapacity(){
    return locker.getAvailableCapacity();
  }
}

这样,你将重用 Locker 类的方法和属性。

英文:

You can do so, but it'd be better if you create an instance for composition at your constructor. So it would be like this:

public class Locker implements GeneralStorage {
  private final int current_capacity;

  public int getAvailableCapacity(){
    return current_capacity;
  }
}

public class LongTermStorage implements GeneralStorage {
  private Locker locker;

  public LongTermStorage() {
    locker = new Locker();
  }

  public int getAvailableCapacity(){
    return locker.getAvailableCapacity();
  }
}

So, you will reuse Locker methods and also its props.

答案2

得分: 0

如果LongTermStorage是一种特殊类型的储物柜(Locker),你可以使用继承,让LongTermStorage 继承自 Locker。这样,你只需要实现那些不同的方法:

public class LongTermStorage extends Locker {
    // 无需实现 getAvailableCapacity(),因为从 Locker 继承的已经适用

    // TODO: 实现其他 LongTermStorage 特定的功能
}

隐式地,LongTermStorage 也实现了 GeneralStorage 接口,因此你可以在任何需要使用 GeneralStorage 的地方使用它。

英文:

If LongTermStorage is a special kind of Locker you can use inheritance and let LongTermStorage extend from Locker. This way you only have to implement the methods which are different:

public class LongTermStorage  extends Locker {
    // No need to implement getAvailableCapacity() as the one from Locker is fine

    // TODO: implement other stuff specific for LongTermStorage
}

Implicitly LongTermStorage also implements the GeneralStorage interface so you can use it wherever you want to use GeneralStorage

答案3

得分: 0

我不太了解Java,但请按照上面的建议进行修改:

interface GeneralStorage {
    void addProduct(Product product);
    void removeProduct(Product product);
    void close();
}

public class Locker implements GeneralStorage {
  private Product[] products;

  public void addProduct(Product product) {...};
  public void removeProduct(Product product) {...};
  public void close() {...};
}

public class LongTermStorage implements GeneralStorage {
  private GeneralStorage storage;

  public LongTermStorage(GeneralStorage storage) {
    this.storage = storage;
  }

  public void addProduct(Product product) {
    // some kind of logic
    storage.addProduct(product);
  }
  public void removeProduct(Product product) {...};
  public void close() {...};
}

GeneralStorage storage = new LongTermStorage(new Locker());

不要在构造函数中调用new来将LongTermStorageLocker耦合在一起。

英文:

I don't know Java well, but do this instead of what was recommended above:

interface GeneralStorage {
    void addProduct(Product product);
    void removeProduct(Product product);
    void close();
}

public class Locker implements GeneralStorage {
  private Product[] products;

  public void addProduct(Product product) {...};
  public void removeProduct(Product product) {...};
  public void close() {...};
}

public class LongTermStorage implements GeneralStorage {
  private GeneralStorage storage;

  public LongTermStorage(GeneralStorage storage) {
    this.storage = storage;
  }
  
  public void addProduct(Product product) {
    // some kind of logic
    storage.addProduct(product)
  };
  public void removeProduct(Product product) {...};
  public void close() {...};
}

GeneralStorage storage = new LongTermStorage(new Locker())

Don't couple LongTermStorage to Locker by calling new in the constructer.

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

发表评论

匿名网友

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

确定