Sunday, April 11, 2010

Backdoor C++ Idiom - part II

After having see the motivation, let us now turn to generic problem and solution.

Context and Applicability
  • You are designing a class using a language such as C++, which provides public, protected and private access rights, constructor and destructor and in which friend classes can be declared.
  • The class offers some services that must be used with caution following a given protocol. If the guaranties are not ensured your system will crash.
  • This protocol can be ensured by third classes in multiple ways not know in advance by the class designer.
Problem

The class you are designing offers some services that must be used with caution following a given protocol, e.g. opened files must be closed, acquired mutex must be released, opened transaction must be suspended, committed or rolled back, suspended transaction must be resumed, or rolled back, resumed transactions must be suspended, committed or rolled back.
The program must be designed carefully to avoid these non-conformances. These errors aren't rare or exotic failures - they are virtually guaranteed to occur unless the interface is designed to avoid them.
Giving a public access would be dangerous, delegating to the client the responsibility to ensure a correct usage. Giving a private access to this services would force to declare the friend classes that are able to use this interface, while the class name and number which can correctly use this services are not known in advance.

Solution

Differentiate between two kinds of clients for your class:
  • Aware clients that are aware of the protocol constraints in order to construct safe interfaces adapter and
  • Unaware clients which use safe interfaces and which can be unaware of the protocol constraints.

Define a backdoor access to your class that acts as a bridge to your class. Allows friend access to the backdoor class.
Aware clients use the public interface and the back-door one with caution in order to respect the protocol constraints.
Unaware clients use the safe public interface and the Safe adapters.

Structure and Participants

The participants in the Backdoor idiom include the following:
Component: grants access to the BackdoorComponent
ComponentBackdoor: defines a restricted public interface to the private part of a component, which must be used with caution, and a protected one for the derived component backdoors.
ComponentSafeAdapter: Provides a safe mean for using the concept ensuring that the component protocol constraints are respected. ComponentSafeAdapter objects are constructed with a reference to a component object and use the ComponentBackdoor class aware of the protocol constraints. This class follows the RAII C++ idiom to ensure that on destruction the component instance is left on a stable state. A variant can be to left on an unstable state but be sure that the safety has been transferred before destruction to another object.
UnawareClient: uses the public interface of Component as well as the ComponentSafeAdapters.


Consequences
Benefits
  • The unsafe use has been limited to the aware component safe adapters.
  • The unaware client cannot use unsafe features and is guided by the component safe adapters.

Liabilities
  • The design is more complex, we have in addition to the component, a component back-door and several component safe adapters.
  • The backdoor interface must be designed carefully to open only the needed private services that help the component safe adapters design.
  • Modifications on the class interface may be synchronized on the backdoor interface.
  • The component designer can document and why not define helper “typedef” for the component safe adapters it knows the time the component is designed, but the other component safe adapters cannot be associated to the Component using the C++ language.
  • It is not possible with the C++ type system to know which clients are aware or not. Is the client using the backdoor interface which must be conscientious of its awareness in order to define a safe usage of the component ? One hint that could guide the client to know if it can use the backdoor interface would be to answer the question, “can this class be considered as an extension of the component interface, i.e. is a component safe adapter?”
Next part will concentrate on possible implementations in C++.

No comments:

Post a Comment