简体   繁体   English

需要有关结构接口的建议

[英]Need an advice on structuring interfaces

I have a structure like follows 我有一个如下的结构

   Interface A
   Interface B extends Interface A
   abstract Class C implements Interface B
   now concrete Class D extends Class C

Now I am using Interface B in a different class and returning the concrete class D object. 现在,我在不同的类中使用接口B,并返回具体的D类对象。

Interface B contains getters and setters and modifying methods. 接口B包含getter和setter以及修改方法。

What I want is that I want to take out all the getters from Interface B somehow and put them in a separate interface so that when I return a concrete object I don't have access to the setters and modifiers of Interface B. But I want to use Interface B as my return object with this newly built read-only concrete object. 我想要的是我想以某种方式从接口B中取出所有吸气剂,并将它们放在一个单独的接口中,这样当我返回一个具体对象时,我将无法访问接口B的设置器和修饰符。但是我想要将接口B用作此新建只读混凝土对象的返回对象。 I am not get any idea about how to achieve this? 我不知道如何实现这一目标?

实现此目的的一种方法是创建一个只读包装器对象,该对象实现接口B,将吸气剂传播到包装的对象,并在设置器和修饰符内引发异常(如IllegalAccessEXceptionInvalidStateException )。

It sounds like you are referring to the Proxy design pattern : http://en.wikipedia.org/wiki/Proxy_pattern . 听起来您是在指代Proxy设计模式: http : //en.wikipedia.org/wiki/Proxy_pattern

In your case, you want Interface B to support getting/setting of certain fields, but you want it to provide a specific proxy for setting and getting those fields, rather then directly editing them. 在您的情况下,您希望接口B支持某些字段的获取/设置,但是您希望它为设置和获取这些字段提供特定的代理,而不是直接对其进行编辑。

This is rarely done, but you can create an inner-interface, which is specific and local to the interface you want to support. 很少这样做,但是您可以创建一个内部接口,该接口对于要支持的接口来说是特定的并且是本地的。 For example : 例如 :

public interface Proxiable {

    public static interface Proxy
    {

    }


    public ProxySub getProxy();
}

Thus, your interface is defining a proxy interface - and anyone who extends your interface will have to provision a Proxy provider. 因此,您的界面正在定义代理界面-扩展您的界面的任何人都必须提供代理提供者。

However, unless you have a REALLY good reason for doing this, you might be overabstracting. 但是,除非您有充分的理由这样做,否则您可能会过于抽象。 Interfaces are generic enough that it is usually sufficient to leave the details of HOW methods are implemented to subclasses, rather than forcing this superstructure at the interface level. 接口足够通用,通常只需将HOW方法的细节留给子类,而不是在接口级别强制使用此超结构即可。

Elaborating on the excellent suggestion by @rsp : 详细阐述@rsp的出色建议:

Create a "read-only" interface 创建一个“只读”界面

   public interface MyInterfaceRO {
      public int getFoo();
      public String getBar();
      // etc...
   }

Create the "read-write" interface, corresponding to your B 创建与您的B对应的“读写”界面

   public interface MyInterfaceRW extends MyInterfaceRO {
      public void setFoo(int foo);
      public void setBar(String bar);
      // ...
   }

IMO, the simplest way to do what you want (prevent modification) is to just return MyInterfaceRO. IMO,执行所需操作(防止修改)的最简单方法是仅返回MyInterfaceRO。 The caller (unless they do a cast) will have no ability to call setFoo(), in fact, in their IDE they won't even see .setFoo() as an option. 调用者(除非他们进行强制转换)将无法调用setFoo(),实际上,在他们的IDE中,他们甚至不会看到.setFoo()作为选项。

I don't understand why you really want to return a type B (my RW above), but, if you do, you are stuck with the caller having the ability to see and call setFoo(). 我不明白您为什么真正想要返回B型(上面我的RW),但是,如果这样做,您就被调用者所困扰,它能够查看和调用setFoo()。 Probably your best bet would be to follow the precedent set in java's Collections and throw an UnsupportedOperationException. 最好的选择是遵循Java的Collections中设置的先例,并抛出UnsupportedOperationException。 As a convenience, you could offer a method 为方便起见,您可以提供一种方法

public boolean isModifiable(); 公共布尔isModifiable();

but you can't force the caller to use and respect that. 但您不能强迫呼叫者使用并遵守。

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

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