简体   繁体   中英

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.

Interface B contains getters and setters and modifying methods.

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. 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 .

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.

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.

Elaborating on the excellent suggestion by @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

   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. 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.

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(). Probably your best bet would be to follow the precedent set in java's Collections and throw an UnsupportedOperationException. As a convenience, you could offer a method

public boolean isModifiable();

but you can't force the caller to use and respect that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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