One property, many codepieces want to set it - how do you coordinate them?

  • Thread starter Thread starter Christopher84
  • Start date Start date
C

Christopher84

Guest
Ive been noticing a pretty common problem in UI Programming and found a solution, but I wonder if there are other ways to solve this issue. And maybe if the approach I came up with already had a name or pattern - wich might include best practices and tips I might have overlooked. Plus I would get a name for the class (I was never any good with picking names).

The Problem to solve:

You have many pieces of code that try to set a single property to a specific value. It is the Topmost issue applied to single property - everyone wants "thier" value to be set and used, but there must be some order of precedence and coordination as to wich one is actually shown. And once the most relevant value is no longer needed, a less relevant one should be take over.

Example:

The Speed of a Game:
There is the speed the user wants too
The speed the computer can actually deliver (the game should slow down to not prevent rendering)
The player might pause the game (wich only hides to original speed, not eliminates it)
The game might pause the game (because the user went to the menu)

It can be easily dififculty to coordiante who gets to show "his" message on the StatusBar:

When nothing else is more important and the programm has loaded, just "Ready" should be visible.
While a file is downloaded/read/printed/saved a corresponding message and progress bar should be shown.
Directly after an Error, a error message shoudl be displayed (until the user actions imply he has noticed it). Maybe that message should change colors or blink to draw more attention to it.

The best Idea I had so far is something like this:

class StatsuBarController{
public string currentValue { get; private set; }

public string IdleValue { private get; set; }
public string BusyValue { private get; set; }
public string ErrorValue { private get; set; }

//this function selects and sets the currentValue
//and is called in every setter except currentValue
protected void selectCurrentValue() { }
}

currentValue could also implement change notification, so binding can take care of the rest.
Granted something more complex then a mere string might be better, but I think it serves to demonstrate the solution I came up with.

Another solution is to take a Collection with CollectionChanged Notification, and pick the "highest" index that isnt null every time the collection changes. Could propably be done with Binding and a "ObservableCollection<T> too T"-Converter. This one would allow best scaling, but it could be hard to figure out how many "levels of precedence" there are supposed to be.


Lets talk about MVVM: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b1a8bf14-4acd-4d77-9df8-bdb95b02dbe2 Please mark post as helpfull and answers respectively.

Continue reading...
 
Back
Top