3

I have ListView control in windows forms that is due to display list of items in either icons view or in details view. I'd like to separate the ListView state logic and created 2 states classes IconsState and DetailsState inhereting them from IState having all the methods to be called from UI window.

In details view there is RetrieveVirtualItem event and in icons view there is DrawItem event. In order to call them from IState variable present in UI both States descendant classes have to implement them. Having DrawItem in DetailsState does nothing but return. The same for RetrieveVirtualItem in IconsState.

Is there another design approach to avoid implementation of empty methods in states?

Chesnokov Yuriy
  • 1,760
  • 5
  • 21
  • 35

1 Answers1

0

Yes, there are better approaches. It's a design smell to force a class to have a member that is not supposed to be in there, ie in your words "Having DrawItem in DetailsState does nothing but return".

Alternatively, you can use an MVP pattern, which will allow you much greater testability. In a passive view variation of MVP you will have one model, two dumb views and a presenter that decides which view to render depending on the user choice.

Additional resources on MVP:
SO tag for MVP
Passive view sample
MVP examples for Windows Forms

Cœur
  • 37,241
  • 25
  • 195
  • 267
oleksii
  • 35,458
  • 16
  • 93
  • 163
  • Well, I use MVP and windows form having ListView control is already the view. I needed simple separation of ListView control behaviour in my view window form. I can not imagine have 2 MVPs here nor creating another MVP just for ListView control in my windows form view. – Chesnokov Yuriy Apr 11 '12 at 15:54
  • AFAI got it, your ListView view can be either icons' view or details' view, if it is the case you then can use say MVP inside MVP, in other words the parent view can display either of the views, there should not be any problems to make an MVP view composable – oleksii Apr 11 '12 at 16:40