Factory Implementation - Better Way :click here
Thread Safe Singleton Implementation Without any code : click here
Decorator:
Decorator provides a flexible alternate to subclassing for extending the functionality runtime depending upon the usage of the objects.
A concrete object is wrapped with additional functionality using the Decorator and the subclasses of it.
Inheritance is static and the functionality is decided at compile time. Where as Decorator maintains a concrete instance and can be wrapped with heterogeneous objects to attach functionality at runtime.
Sample Code:
class I {
public:
virtual ~I() { }
virtual void do_it() = 0;
};
class A : public I {
public:
~A() { cout << "A dtor" << '\n'; }
/*virtual*/ void do_it() { cout << 'A'; }
};
class D : public I {
public:
D( I* inner ) { m_wrappee = inner; }
~D() { delete m_wrappee; }
/*virtual*/ void do_it() { m_wrappee->do_it(); }
private:
I* m_wrappee;
};
class X : public D {
public:
X( I* core ) : D( core ) { }
~X() { cout << "X dtor" << " "; }
/*virtual*/ void do_it() {
D::do_it();
cout << 'X';
}
};
class Y : public D {
public:
Y( I* core ) : D( core ) { }
~Y() { cout << "Y dtor" << " "; }
/*virtual*/ void do_it() {
D::do_it();
cout << 'Y';
}
};
class Z : public D {
public:
Z( I* core ) : D( core ) { }
~Z() { cout << "Z dtor" << " "; }
/*virtual*/ void do_it() {
D::do_it();
cout << 'Z';
}
};
int main( void ) {
I* anX = new X( new A );
I* anXY = new Y( new X( new A ) );
I* anXYZ = new Z( new Y( new X( new A ) ) );
anX->do_it(); cout << '\n';
anXY->do_it(); cout << '\n';
anXYZ->do_it(); cout << '\n';
delete anX; delete anXY; delete anXYZ;
}
// AX
// AXY
// AXYZ
// X dtor A dtor
// Y dtor X dtor A dtor
// Z dtor Y dtor X dtor A dtor
Thread Safe Singleton Implementation Without any code : click here
Decorator:
Decorator provides a flexible alternate to subclassing for extending the functionality runtime depending upon the usage of the objects.
A concrete object is wrapped with additional functionality using the Decorator and the subclasses of it.
Inheritance is static and the functionality is decided at compile time. Where as Decorator maintains a concrete instance and can be wrapped with heterogeneous objects to attach functionality at runtime.
Sample Code:
class I {
public:
virtual ~I() { }
virtual void do_it() = 0;
};
class A : public I {
public:
~A() { cout << "A dtor" << '\n'; }
/*virtual*/ void do_it() { cout << 'A'; }
};
class D : public I {
public:
D( I* inner ) { m_wrappee = inner; }
~D() { delete m_wrappee; }
/*virtual*/ void do_it() { m_wrappee->do_it(); }
private:
I* m_wrappee;
};
class X : public D {
public:
X( I* core ) : D( core ) { }
~X() { cout << "X dtor" << " "; }
/*virtual*/ void do_it() {
D::do_it();
cout << 'X';
}
};
class Y : public D {
public:
Y( I* core ) : D( core ) { }
~Y() { cout << "Y dtor" << " "; }
/*virtual*/ void do_it() {
D::do_it();
cout << 'Y';
}
};
class Z : public D {
public:
Z( I* core ) : D( core ) { }
~Z() { cout << "Z dtor" << " "; }
/*virtual*/ void do_it() {
D::do_it();
cout << 'Z';
}
};
int main( void ) {
I* anX = new X( new A );
I* anXY = new Y( new X( new A ) );
I* anXYZ = new Z( new Y( new X( new A ) ) );
anX->do_it(); cout << '\n';
anXY->do_it(); cout << '\n';
anXYZ->do_it(); cout << '\n';
delete anX; delete anXY; delete anXYZ;
}
// AX
// AXY
// AXYZ
// X dtor A dtor
// Y dtor X dtor A dtor
// Z dtor Y dtor X dtor A dtor
No comments:
Post a Comment