Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Progressive Disclosure in C++ API

Following my reading of the article Programmers Are People Too by Ken Arnold, I have been trying to implement the idea of progressive disclosure in a minimal C++ API, to understand how it could be done at a larger scale.

Progressive disclosure refers to the idea of "splitting" an API into categories that will be disclosed to the user of an API only upon request. For example, an API can be split into two categories: a base category what is (accessible to the user by default) for methods which are often needed and easy to use and a extended category for expert level services.

I have found only one example on the web of such an implementation: the db4o library (in Java), but I do not really understand their strategy. For example, if we take a look at ObjectServer, it is declared as an interface, just like its extended class ExtObjectServer. Then an implementing ObjectServerImpl class, inheriting from both these interfaces is defined and all methods from both interfaces are implemented there.

This supposedly allows code such as:

public void test() throws IOException {
    final String user = "hohohi";
    final String password = "hohoho";
    ObjectServer server = clientServerFixture().server();
    server.grantAccess(user, password);

    ObjectContainer con = openClient(user, password);
    Assert.isNotNull(con);
    con.close();

    server.ext().revokeAccess(user); // How does this limit the scope to 
                                     // expert level methods only since it
                                     // inherits from ObjectServer?

    // ...
});

My knowledge of Java is not that good, but it seems my misunderstanding of how this work is at an higher level.

Thanks for your help!

like image 306
BobMorane Avatar asked Oct 29 '22 16:10

BobMorane


1 Answers

Java and C++ are both statically typed, so what you can do with an object depends not so much on its actual dynamic type, but on the type through which you're accessing it.

In the example you've shown, you'll notice that the variable server is of type ObjectServer. This means that when going through server, you can only access ObjectServer methods. Even if the object happens to be of a type which has other methods (which is the case in your case and its ObjectServerImpl type), you have no way of directly accessing methods other than ObjectServer ones.

To access other methods, you need to get hold of the object through different type. This could be done with a cast, or with an explicit accessor such as your ext(). a.ext() returns a, but as a different type (ExtObjectServer), giving you access to different methods of a.

Your question also asks how is server.ext() limited to expert methods when ExtObjectServer extends ObjectServer. The answer is: it is not, but that is correct. It should not be limited like this. The goal is not to provide only the expert functions. If that was the case, then client code which needs to use both normal and expert functions would need to take two references to the object, just differently typed. There's no advantage to be gained from this.

The goal of progressive disclosure is to hide the expert stuff until it's explicitly requested. Once you ask for it, you've already seen the basic stuff, so why hide it from you?

like image 168
Angew is no longer proud of SO Avatar answered Nov 15 '22 07:11

Angew is no longer proud of SO