MrUll's

Juli 28, 2004

a basic OO concept

i read a basic statement about OO which is worth to be memorized. often i heard the phase to delegate sth to another class. a vague understanding states that this is to be accomplished using a method call on another object. having separation of varying concerns in mind, a delegation of functionality means a method call on another object, passing an objects self-reference (this).

e.g. in a first step this allows the separation of data and functionality from a class, a second step could impose another functionality. this is not far away from being called strategy pattern.

c-style; doing and data in same Object:

public void solveProblem( int how ) {
 switch (how) {
   case QUICKANDDIRTY: doItQuickAndDirty(); break;
   case SECURED: doTransactionalProcessed(); break;
}
}

OO approach using delegation (strategy):

public void solveProblem( iHowToBeProcessed processor ) {
 processor.doItYourWay( this );


iHowToBeProcessed is an Interface; implementations are realizing e.g. quick-and-dirty or secured processings. This allows to introduce additional processings without affecting the
class to be modified on which processing has to be done.