Progressive Disclosure in the C ++ API

After reading the article "Programmers are People," Ken Arnold, I tried to implement the idea of ​​progressive deployment into a minimal C ++ API in order to understand how this can be done on a wider scale.

Progressive disclosure refers to the idea of ​​“dividing” an API into categories that will only be disclosed to an API user upon request. For example, the API can be divided into two categories: the base category, which is (available to the user by default) for methods that are often necessary and convenient to use, and the extended category for expert-level services.

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

This supposedly allows you to use code, for example:

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 so good, but it seems that I do not understand how this work is at a higher level.

Thank you for your help!

+5
source share
1 answer

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

In the above example, you will notice that the server variable is of type ObjectServer . This means that when viewing the server you can only access the ObjectServer methods. Even if the object has a type that has other methods (which is the case in your case and its type is ObjectServerImpl ), you have no way to directly access methods other than ObjectServer .

To access other methods, you need to master the object using a different type. This can be done with a throw or with explicit access, for example, with your ext() . a.ext() returns a , but as a different type ( ExtObjectServer ), giving you access to various a methods.

Your question also asks how server.ext() limited to expert methods when ExtObjectServer extends ObjectServer . The answer is: it is not, but it is correct. This should not be limited to this. The goal is not to provide only expert functions. If so, then client code, which should use both regular and expert functions, would have to take two references to an object that was simply printed differently. There are no benefits to this.

The purpose of progressive disclosure is to hide expert material until it explicitly requests. As soon as you ask for it, you have already seen the basic things, so why hide them from you?

+2
source

Source: https://habr.com/ru/post/1260155/


All Articles