C++ (Qt)class vector_base{public: virtual void method1() = 0; virtual void method2() = 0;}; class null_vector : public vector_base{public: virtual void method1(); // посылает исключение virtual void method2(); // посылает исключение}; class valid_vector : public vector_base{public: valid_vector( int size ) { ... } virtual void method1(); // выполняет действие virtual void method2(); // выполняет действие}; // ========================================================= class vector{public: vector() : m_vec( new null_vector ) {} vector( int size ) : m_vec( new valid_vector( size ) ) {} void setup( int size ) { delete m_vec; m_vec = new valid_vector( size ); } void medhod1() { m_vec->method1(); } void medhod2() { m_vec->method2(); } private: vector_base *m_vec;};
C++ (Qt)struct base_state{ virtual void police() = 0;}; struct null_state : public base_state{ virtual void police() { throw exception; } // Кидаем исключения}; struct valid_state : public base_state{ virtual void police() {} // Ничего не делаем, просто пропускаем.}; class vector{public: vector() : m_state( new null_state ) {} vector( int size ) : m_state( new valid_state) {} void setup( int size ) { delete m_state; m_state = new valid_state; } void medhod1() { m_state->police(); .... .... } void medhod2() { m_state->police(); .... .... } private: base_state *m_state;};
C++ (Qt)class Solver{public:Solver(): x(){// Why we create some vector at this point? Why you choose such dimension for vector?// We don't know anything about it. And we don't understad what is vector of zero dimension.// Is it valid? Can we use it? Will be it used somewhere?....// Only now we know dimension of vectorx = Vector(dimension);}private:Vector x;}
C++ (Qt)class Solver{public:Solver(): x(null){// It is obviously that x is null-object. We don't know anything about it. // Any action with x should crash program. Only assigment allowed.....// Now we know dimension of vectorx = Vector(dimension);}private:Vector x;}
C++ (Qt)typedef Vector NullVector class Solver{public:Solver(): x(NullVector()){// It will not crash, but at least it is clear that vector x is null-object.....// Now we know dimension of vectorx = Vector(dimension);}private:Vector x;}