问题
I have following class implementation (not complete, just to give an idea)
public class MySwitch{
Command command = Command.Red;
public Command GetNext() {
command = GetNext(command); // circular enum values
return command;
}
}
public enum Command { Red =0, Blue=1, Green=2}
public class LED {
public void Glow(Command command){
this.setColor(ColorForm(command));
this.Glow();
}
}
public class Commander {
public Commander(LED target, MySwitch source){
this.LED = LED;
this.MySwitch = MySwitch;
}
public void Execute(){
this.LED.Glow(this.MySwitch.GetNext());
}
}
I want these objects to map to UI items. Consider, I have win form app where switch and LED are two panel where I want GDI to draw it.
Issue is what is best way sync objects with UI elements. Options are:
Create UI element inherited with panel and should contains one instance of object.
Create UI element (say LEDUI) inherited from BO, and should contain container (panel) to draw and implement draw method using this.Color (LED for example) - this will lead to file cound 2* BO
Keep UI element and BO separate and let presenter to be bridge between them.
Implement method on BO itself to render (assuming single UI) on winform. Since it cannot be added to winform directly, so create a
CustomForm
object which allows such elements (assumeIMyObj
) to be added, and callCustomFOrm.Render()
, which eventually call render method of all childElements. Pretty same way Form and controls are rendered.Any other way
In my opinion, point 2 is better way. Please suggest what pros and cons on different way of mapping BO with UI, and how to sync them. Game developers may have better understanding.
EDIT
My mistake, there could be many LED and switches. Each switch may be attached to many LED. Also the classes I have created is independent of UI. I don't expect solution to how to find the control and glow, but what is the best way to implement if you are given these classes and told to make a winform app, assuming u will least touch the classes as well as write minimum code, along with following standard way of UI development
回答1:
I've always been taught that referencing goes before inheriting, that if you don't have to access protected members of a certain class you better create an object of that type in your class than inherit the whole class (correct me please if im wrong).
in that light option 3 seems best to me.
回答2:
In C# (well, WinForms) each UI element has a Tag
member into which you can put whatever you want. So, when the user presses the 'switch', the code can iterate over the controls looking for controls with a Tag
that matches the current selection.
For example, suppose the LED images have the text 'LED=' in their Tag
field, then you can write a function to set the glow state:
// example usage: SetGlow ("Red");
void SetGlow (string colour)
{
SetGlow (Controls, colour);
}
void SetGlow (ControlContainer controls, string colour)
{
foreach (Control control in controls)
{
if (control.Tag is a string) // syntax escapes me
{
string tag = (string) control.Tag;
if (tag.StartsWith ("LED="))
{
if (tag == ("LED=" + colour))
{
// enable glow
}
else
{
// disable glow
}
}
}
SetGlow (control.Controls, colour);
}
}
来源:https://stackoverflow.com/questions/6734113/how-to-map-objects-with-ui