Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 333483
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T09:57:56+00:00 2026-05-12T09:57:56+00:00

So, I have this public API that my application exposes, allowing customers to write

  • 0

So, I have this public API that my application exposes, allowing customers to write plug-ins. For the sake of this example, let’s say it’s a fairly simple key-value pair system, something like:

public interface Key {
  // marker interface, guaranteed unique in some scope
}

public interface KVPService {
  Set<Key> getKeys();
  Object getValue(Key k); // must be a key provided by getKeys() or it blows up
}

Now let’s say that internally, the Key has some attributes that shouldn’t be exposed — say a database ID. What I’m currently doing is something like this:

/** Internal impl of external Key */    
class InternalKey implements Key {
  int getDatabaseId() {
    // do something...
  }
}

/** Internal impl of external KVPService */    
class InternalKVPService implements KVPService {
  // ...

  public Object getValue(Key k) {
    InternalKey ik = (InternalKey) k;
    return getValueFromDBOrWherever(ik.getDatabaseId());
  }

  // ...
}

This seems less than ideal. Is there some way I can rearrange the responsibilities to avoid the cast and still maintain the inside/outside encapsulation?

Note that in the real world it’s a fair bit more complicated than this; the Key equivalents have a fair bit of metadata attached and there’s a number of things someone might want to do with one other than get a simple value, so just exposing, say, Map.Entry-like objects instead won’t necessarily solve the problem.

The only thing I’ve been able to come up with is to separate the internal and external keys entirely, and keep around a Map<Key, InternalKey>. But in that case I’d have to either copy the metadata, which violates DRY, or have the external Key delegate to InternalKey, in which case the Map violates DRY.

Can anyone think of something more clever?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T09:57:56+00:00Added an answer on May 12, 2026 at 9:57 am

    One approach I’ve seen is to expose a common interface for all objects (in this case keys) and provide a base implementation that simply throws an UnsupportedOperationException (or do nothing) for each method. Then sub-class implementations subsequently override method(s) to provide functionality. Granted it’s not very OO-like but you’ll find some examples in the JDK API (e.g. Iterator‘s remove() method).

    Another option: You could use the visitor pattern to have each object perform functionality without downcasting; e.g.

    public interface KeyVisitor {
      void visitInternalKey(InternalKey k);
      void visitFooBarKey(FooBarKey k);
    }
    
    public interface Key {
      void visit(KeyVisitor kv);
    }
    
    public class InternalKey implements Key {
      public void visit(KeyVisitor kv) {
        kv.visitInternalKey(this);
      }
    }
    

    The disadvantage here is that you would have to expose the methods on InternalKey (at least via an interface) to allow your visitor implementations to call them. However, you could still keep the implementation detail at the package level.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm building a small application that pulls statistics from an API I have no
I have this route that worked before we upgraded our application to MVC3: routes.MapRoute(
I have this two sharepoint web application that resides on the same server with
I have this: public class ClientSession : TcpClient { public int SessionGUID = 0;
I have this public Result SomeMethod() { Popup popup = new Popup(); popup.Closed +=
Best to describe this in code... I have this public class A<T> { public
I'm using Spring 3.0 with a genericDAO. I have this: public interface GenericDAO<T, ID
How would I create a nested list, I currently have this public function getNav($cat,$subcat){
I have this controller: public class StandingsController : Controller { public ViewResult Index(int id)
I have this enumeration: public enum Translation { English=1, Russian, French, German } I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.