Hi,
I have been trying to implement the "Progressive Disclosure" pattern for a small testing
API with success. The wanted behaviour was to have the "plusFour()" service available for a
Base object, but not the "timesThree()". I wanted "timesThree" to be available only upon
request, that is, only after an "ext()" call.
This code actually compiles (if I add class implementations) and does what I want. the only
thing that bothers me is that cast from a Base class to its Derived class in "ext()". It seems to make
sense here but it feels weird.
Since I have not found a single example on the web on how to implement this, I was wondering:
is there a better -- safer -- way to do this?
Code for the present implementation (I have left out obvious method bodies):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
|
#include<iostream>
#include<stdio.h>
class ExtBase;
class Base
{
public:
Base() = delete;
Base(int p_attr);
int getAttr() const;
ExtBase* ext() {return reinterpret_cast<ExtBase*>(this);} // Unsure about this...
void plusFour(); // adds four to the attribute
protected:
int m_attr;
};
class ExtBase : public Base
{
public:
void timesThree(); // Multiplies by three the attribute
};
int main()
{
Base var(0);
std::cout << "m_attr = " << var.getAttr() << std::endl; // m_attr = 0
var.plusFour();
std::cout << "m_attr = " << var.getAttr() << std::endl; // m_attr = 4
// Access to new functionnality:
ExtBase var1 = *(var.ext());
std::cout << "New functionnality \"timesThree()\" enabled..." << std::endl;
std::cout << "m_attr = " << var1.getAttr() << std::endl; // m_attr = 4
var1.timesThree();
std::cout << "m_attr = " << var1.getAttr() << std::endl; // m_attr = 12
var1.plusFour(); // Still usable...
std::cout << "m_attr = " << var1.getAttr() << std::endl; // m_attr = 16
getchar();
return 0;
}
|
Thanks!
Progressive Disclosure:
http://queue.acm.org/detail.cfm?id=1071731