简体   繁体   English

如何使这个Java接口灵活

[英]How to make this Java Interface flexible

I have this Java Interface: 我有这个Java接口:

public interface Box {
   public void open();
   public void close();
}

This interface is extended by this class: 此类接口由此类扩展:

public class RedBox implements Box {

   public void open() {

   }

   public void close() {

   }
}

The problems is that I'm looking to add other classes in the future that will also implement the Box Interface. 问题是我希望将来添加其他类也将实现Box接口。 Those new classes will have their own methods, for example one of the classes will have a putInBox() method, But if I add the putInBox() method to the Box Interface, I will also be forced to add an empty implementation of putInBox() method to the previous classes that implemented the Box Interface like the RedBox class above. 那些新类将有自己的方法,例如其中一个类将有一个putInBox()方法,但如果我将putInBox()方法添加到Box接口,我也将被迫添加一个putInBox的空实现( )实现Box接口的前一个类的方法,如上面的RedBox类。

I'm adding putInBox() to the Box Interface because there is a class Caller that takes an object of classes that implemented the Box Interface, example: 我将putInBox()添加到Box接口,因为有一个类Caller,它接受一个实现Box接口的类对象,例如:

public class Caller {

    private Box box;
    private int command;

    public Caller(Box b) {
        this.box = b;
    }

    public void setCommandID(int id) {
        this.command = id;
    }

    public void call() {

        if(command == 1) {
            box.open();
        }
        if(command == 2) {
            box.close();
        }

        // more commands here...
    }
}

Caller c = new Caller(new RedBox());

c.call();

How do I implement the Box Interface in the new classes without been forced to add empty implementation of new methods to each of the previous classes that implemented the Box Interface. 如何在新类中实现Box接口,而不必强制将新方法的空实现添加到实现Box接口的每个先前类中。

You are not limited to a single interface - you can build an entire hierarchy! 您不仅限于单一界面 - 您可以构建整个层次结构! For example, you can make these three interfaces: 例如,您可以创建以下三个接口:

public interface Box {
    public void open();
    public void close();
}
public interface LockableBox extends Box {
    public void lock();
    public void unlock();
}
public interface MutableBox extends Box {
    public void putItem(int item);
    public void removeItem(int item);
}

Now your boxes can implement an interface from the hierarchy that fits your design. 现在,您的盒子可以从适合您设计的层次结构中实现一个界面。

public class RedBox implements LockableBox {
    public void open() {}
    public void close() {}
    public void lock() {}
    public void unlock() {}
}

public class BlueBox implements MutableBox {
    public void open() {}
    public void close() {}
    public void putItem(int item) {}
    public void removeItem(int item) {}
}

With a hierarchy in place, you can continue programming to the interface: 使用层次结构,您可以继续编程到界面:

MutableBox mb = new BlueBox();
mb.putItem(123);
LockableBox lb = new RedBox();
lb.unlock();

实现Box接口的新类只需要实现Box接口中的方法,而不是实现Box接口的其他类中的任何其他方法。

As said above, there's no need to have a new method in a new class added to the Box interface itself, you can just leave it in that new class, and so it won't interfere with any other implementation. 如上所述,没有必要在添加到Box接口本身的新类中使用新方法,您可以将其保留在新类中,因此它不会干扰任何其他实现。

But if you do want to have new methods at the interface level, a way to introduce some flexibility is to use an (abstract) base implementation of your interface that provides (empty) implementations of all methods: 但是如果你想在接口级别拥有新方法,那么引入一些灵活性的方法是使用接口的(抽象)基本实现,它提供所有方法的(空)实现:

public abstract class BoxBase implements Box {
  public void open() { }

  public void close() { }

}

public class RedBox extends BoxBase {

   @Override
   public void open() {
     // open a red box
   }
}

This way, when adding new methods to your Box interface, you will only need to add an implementation of the method to the BoxBase class, and it won't interfere with your RedBox class. 这样,在向Box界面添加新方法时,您只需要将方法的实现添加到BoxBase类,它不会干扰您的RedBox类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM