M
Myster Ious
Polymorphism replaces switch statements, making the code more
compact/readable/maintainable/OO whatever, fine!
What I understand, that needs to be done at the programming level, is
this:
a switch-case has a variable (most probably an enumeration) &
associated symbols or integral value. Selection is made, base on what
symbol/value the variable holds. So
variable = VALUE1; // selection 1
....
variable = VALUE2; // selection 2
....
switch (variable) {
case VALUE1: DoForValue1(); break;
case VALUE2: DoForValue2(); break;
}
to replace it w polymorphism, the variable substitue is pointerToBase
& symbol/value substitute is 'an instantiation of DerivedClass', e.g.,
pointerToBase = new DerivedClass1; // selection 1
....
pointerToBase = new DerivedClass2; // selection 2
....
pointerToBase->DoAccordingToDerivedClass(); // virtual-call! switch
not needed
Lets look at it with another perspective, i.e., the source of that
selection! Two questions:
1 - If the selection-source is some set of radio-buttons & User
selects & reselects different radios multiple times, the "switch
scenario" doesn't get much effected, but for "polymorph. scenario" we
have to new & delete the various derived classes that much time, it's
like playing with memory (pretty bad particularly if Derived Objects
take-up large memory). Is there any decent substitute?
2 - We can easily persist the variable value in the "switch scenario",
load it again & apply another switch. For "polymorph. scenario",
persisting is easy, call pointerToBase->PersistAccToDerivedClass();
but for loading we have to apply a 'switch statement' because the
persisted object would be just a character-string or integral value
identifying different derivedObjects, how to avoid a switch statement
at this time?
compact/readable/maintainable/OO whatever, fine!
What I understand, that needs to be done at the programming level, is
this:
a switch-case has a variable (most probably an enumeration) &
associated symbols or integral value. Selection is made, base on what
symbol/value the variable holds. So
variable = VALUE1; // selection 1
....
variable = VALUE2; // selection 2
....
switch (variable) {
case VALUE1: DoForValue1(); break;
case VALUE2: DoForValue2(); break;
}
to replace it w polymorphism, the variable substitue is pointerToBase
& symbol/value substitute is 'an instantiation of DerivedClass', e.g.,
pointerToBase = new DerivedClass1; // selection 1
....
pointerToBase = new DerivedClass2; // selection 2
....
pointerToBase->DoAccordingToDerivedClass(); // virtual-call! switch
not needed
Lets look at it with another perspective, i.e., the source of that
selection! Two questions:
1 - If the selection-source is some set of radio-buttons & User
selects & reselects different radios multiple times, the "switch
scenario" doesn't get much effected, but for "polymorph. scenario" we
have to new & delete the various derived classes that much time, it's
like playing with memory (pretty bad particularly if Derived Objects
take-up large memory). Is there any decent substitute?
2 - We can easily persist the variable value in the "switch scenario",
load it again & apply another switch. For "polymorph. scenario",
persisting is easy, call pointerToBase->PersistAccToDerivedClass();
but for loading we have to apply a 'switch statement' because the
persisted object would be just a character-string or integral value
identifying different derivedObjects, how to avoid a switch statement
at this time?