I'm designing a "view" handling system for a game. The goal is to be able to have different "views" that can be shown in sequence or stacked on-top of each other. For example, the initial splash screen is one View and the main menu that follows the splash screen is also a View which is rendered when the splash screen terminates.
While defining the interface for View I realize that the stacked nature of View s corresponds to the input focus priority.
Get Our Secret to Good Code Documentation Guide
The pause View is offered first chance to act on user input to continue the game and it swallows all other input. To me this makes perfect sense. Now, not all View s are necessarily interested in receiving input.
Some might only be interested in mouse events and some might only be interested in keyboard events. My initial approach was to just let them have pass-through input handlers.
But then I remembered the Interface Segregation Principle so it would make sense to define two more interfaces IKeyboardHandler and IMouseHandler and let the relevant View classes implement those when needed.
I'm not sure if this approach is any more elegant than having empty input handlers and I'm looking for input and ideas on the design.
Interface segregation principle c++ example pdf
The interface segregation principle is focused more on completely irrelevant functionality. Here it's not so clear cut.
A blatant example of a violation of ISP is a Scrollbar interface with a setText function inherited from Widget , when a scrollbar won't even show any text. In such a case, the function may even be documented as irrelevant to callers, and to attempt to call such a function on a scrollbar may indicate a logical error. Such blatant violations of ISP tend to be most foul, with monolithic interfaces that might have functions with only 20 of them being relevant for the actual object in question.
I see it often in GUI designs which are based around massive inheritance hierarchies, and often with documentation mechanisms trying to filter out the enormous amount of irrelevant derived functionality. The fact that it doesn't is still relevant information, and worth calling the function to find out. So I don't see this as a clear violation of ISP.
You could try to split up the interface into multiple interfaces like IKeyboardHandler , but it's likely to make things harder rather than easier.
It's worth noting the scope here in terms of dependencies. A game will typically only have a handful of such views. You might have a main menu screen of sorts, splash screen, HUD, game view, maybe an in-game pie menu or something.
It's a handful of views.
The clients will also tend to be few in number, maybe just one broad client which is the one handling events from the operating system and calling the relevant view functions. That said, just for completeness, I'll show you a simple way to avoid the cast in these kinds of scenarios.
You can do this:. Ones that do can simply return this;. This way you can find out if a view supports keyboard handling with an interface query which doesn't involve casting, and if so, handle keyboard input for that view.
Taking this technique further to a greater degree of flexibility will often have you approaching COM-style designs which does involve casting but hides it from the client , and even further, entity-component systems which can be very useful to know about as a game developer, but probably outside of this view context.
Yet I think this is absolute overkill for your case even with the basic form, and likely to just add more burdens upfront without alleviating any later.
Interface Segregation Principle (SOLID) - Code Walks 023
Yet this basic version shown above can be a useful design technique if you had, say, functions in your system which can work strictly with IKeyboardHandler , and you potentially have things other than views which might implement this keyboard handling interface. Don't use a pattern just because someone says it's a good idea.
Following design patterns is an antipattern. It looks to me like keyboard and mouse handling absolutely should be part of View , not something else. There should probably be a ChainedView base class that implements View , which offer a default implementation of the virtual methods for keyboard and mouse handling.
Some View s, like pause, will override that to only accept input they understand and ignore the rest - or maybe they won't even be a ChainedView.
Interface segregation principle
Most View s will override it to handle what they can and pass the rest onto the next chained view. Any View that doesn't need to handle input itself will automatically pass it on, so there is already separation there. Home Questions Tags Users Unanswered. Asked 5 years, 3 months ago. Active 2 years ago. Viewed 1k times. A view is a renderable target that can accept user input. May be called multiple times per update.
False if the event should continue to lower views. Only called once per update. But it gives you an idea. Emily L. In your case, I think it's a bit different since given a function like this: virtual bool handleInput KeyEvent aKeyEvent, Avoiding the Dynamic Cast That said, just for completeness, I'll show you a simple way to avoid the cast in these kinds of scenarios.
Good answer. I would however recommending using a Null Object rather than returning null from the event handler getters, as this would simplify client code.
This could be returned by the base class methods to simplify implementation of View subclasses. What do you mean with following design patterns is an antipattern? Oct 5 '14 at The entire concept of "design patterns" exists entirely to sell books about design patterns. In real code, the important question is "what will make this code maintainable? EmilyL o11c is correct in his first sentence, wrong in the second sentence.
Don't just use them blindly. But definitely do follow them. Design patterns are time tested patterns found in huge swaths of successful code. For the specific case of the Gang of Four book, there's a large amount of evidence to support the value of each pattern. The patterns give you, the developer, guidance when designing software -- they are your starting point for any design because the patterns lead to good practices.
But you do have to understand why each pattern leads to good code or else you get code bloat.
SRM Thanks. I'm aware of that, and disregarded the second sentence.
Jul 7 '15 at Sign up or log in Sign up using Google. Sign up using Facebook.
Subscribe to RSS
Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. How to create micro-interactions with react-spring: Part 1.
This week, StackOverflowKnows syntactic sugar, overfit or nah, and the….
Interface Segregation Principle (ISP)
Featured on Meta. Thank you, Shog9. Thank you, Robert Cartaino. Related Hot Network Questions. Question feed.