Avoiding sub-type selection in view code
Posted
by John Donoghue
on Stack Overflow
See other posts from Stack Overflow
or by John Donoghue
Published on 2010-02-01T17:32:50Z
Indexed on
2010/05/15
10:04 UTC
Read the original article
Hit count: 159
Hi, I have some code where the model contains some classes like (vb.net pseudocode, but could be any OO language):
Enum AttributeType
Boolean
Date
String
End Enum
MustInherit Class Attibute
Must Override Function Type As AttributeType
End Class
Class BooleanAttribute: Attribute
Function Type As AttributeType
Return AttributeType.Boolean
End Function
End Class
And the view contains some code like:
Select Case AttributeType
Case Boolean
//Display checkbox control
Case Date
//Display date picker control
Case String
//Display textbox control
End Select
I don't really like the code in the view, for the hopefully obvious reasons (what happens when I get a new attribute type etc). My question is, how should I replace it?
I could easily add a method to the concrete classes, but that pollutes the model with UI stuff so that's a horrible idea.
I could move the select into a factory, but that seems to be just hiding the problem.
Can anybody advise a better approach?
© Stack Overflow or respective owner