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

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

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

问题

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

  1. void do_some()
  2. a.do_some()

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

  1. public class Locker implements GeneralStorage {
  2. private final int current_capacity;
  3. public int getAvailableCapacity() {
  4. return current_capacity;
  5. }
  6. }
  7. public class LongTermStorage implements GeneralStorage {
  8. private final int current_capacity;
  9. private Locker locker = new Locker();
  10. public int getAvailableCapacity() {
  11. return locker.getAvailableCapacity();
  12. }
  13. }

我应该怎么办?(我想返回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:

  1. void do_some()
  2. a.do_some()

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

  1. public class Locker implements GeneralStorage {
  2. private final int current_capacity;
  3. public int getAvailableCapacity(){
  4. return current_capacity;
  5. }
  6. }
  7. public class LongTermStorage implements GeneralStorage {
  8. private final int current_capacity;
  9. private Locker locker=New Locker();
  10. public int getAvailableCapacity(){
  11. return locker.getAvailableCapacity

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

答案1

得分: 0

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

  1. public class Locker implements GeneralStorage {
  2. private final int current_capacity;
  3. public int getAvailableCapacity(){
  4. return current_capacity;
  5. }
  6. }
  7. public class LongTermStorage implements GeneralStorage {
  8. private Locker locker;
  9. public LongTermStorage() {
  10. locker = new Locker();
  11. }
  12. public int getAvailableCapacity(){
  13. return locker.getAvailableCapacity();
  14. }
  15. }

这样,你将重用 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:

  1. public class Locker implements GeneralStorage {
  2. private final int current_capacity;
  3. public int getAvailableCapacity(){
  4. return current_capacity;
  5. }
  6. }
  7. public class LongTermStorage implements GeneralStorage {
  8. private Locker locker;
  9. public LongTermStorage() {
  10. locker = new Locker();
  11. }
  12. public int getAvailableCapacity(){
  13. return locker.getAvailableCapacity();
  14. }
  15. }

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

答案2

得分: 0

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

  1. public class LongTermStorage extends Locker {
  2. // 无需实现 getAvailableCapacity(),因为从 Locker 继承的已经适用
  3. // TODO: 实现其他 LongTermStorage 特定的功能
  4. }

隐式地,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:

  1. public class LongTermStorage extends Locker {
  2. // No need to implement getAvailableCapacity() as the one from Locker is fine
  3. // TODO: implement other stuff specific for LongTermStorage
  4. }

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

答案3

得分: 0

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

  1. interface GeneralStorage {
  2. void addProduct(Product product);
  3. void removeProduct(Product product);
  4. void close();
  5. }
  6. public class Locker implements GeneralStorage {
  7. private Product[] products;
  8. public void addProduct(Product product) {...};
  9. public void removeProduct(Product product) {...};
  10. public void close() {...};
  11. }
  12. public class LongTermStorage implements GeneralStorage {
  13. private GeneralStorage storage;
  14. public LongTermStorage(GeneralStorage storage) {
  15. this.storage = storage;
  16. }
  17. public void addProduct(Product product) {
  18. // some kind of logic
  19. storage.addProduct(product);
  20. }
  21. public void removeProduct(Product product) {...};
  22. public void close() {...};
  23. }
  24. GeneralStorage storage = new LongTermStorage(new Locker());

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

英文:

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

  1. interface GeneralStorage {
  2. void addProduct(Product product);
  3. void removeProduct(Product product);
  4. void close();
  5. }
  6. public class Locker implements GeneralStorage {
  7. private Product[] products;
  8. public void addProduct(Product product) {...};
  9. public void removeProduct(Product product) {...};
  10. public void close() {...};
  11. }
  12. public class LongTermStorage implements GeneralStorage {
  13. private GeneralStorage storage;
  14. public LongTermStorage(GeneralStorage storage) {
  15. this.storage = storage;
  16. }
  17. public void addProduct(Product product) {
  18. // some kind of logic
  19. storage.addProduct(product)
  20. };
  21. public void removeProduct(Product product) {...};
  22. public void close() {...};
  23. }
  24. 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:

确定