In MFC, certain mechanisms provided allow the programmer to bypass modularity and encapsulation and information hiding, arguable the most desirable features of an Object Oriented framework.
One (of many) examples is Owner Drawn controls:
you can choose to either implement DrawItem
in a child control subclass and do all of that control's drawing in that subclass, making it appear more modular:
class CustomButton: CButton{
// --- Lots of stuff, DECLARE_DYNAMIC etc
virtual void DrawItem(LPDRAWITEMSTRUCT lpdis){
// Drawing code for this button in the button's subclass
}
};
...or you can choose handle the WM_DRAWITEM
message in a parent Window class via OnDrawItem
class MainFrame: CFrameWnd{
// --- Lots of stuff, DECLARE_DYNAMIC etc
CustomButton button;
afx_msg void OnDrawItem(LPDRAWITEMSTRUCT lpdis, UINT id){
if(id == CUSTOM_BUTTON_ID){
// Drawing code for this button in the button's subclass
}
}
};
In the later situation, the drawing of the control is outside of the control subclass, meaning that the concept that "OOP data structures tend to carry their own operators around with them" is undermined.. right?
So my question is: which one is considered 'best practice'? There must be a reason that the second one exists - can anyone suggest a circumstance in which undermining modularity would be a better option?