Observer pattern Or DataBinding

半城伤御伤魂 提交于 2019-12-11 02:29:02

问题


My question is, would you implement the Observer pattern, or use databinding to do the following:

At the moment, i'm initializing a list of GuiDataObj. When an event is triggered, I look up the GuiDataObjById and then modify the object, which is databound to a GuiElement; which updates the GUI.

There are several GuiElements (at the moment are all of the same type, but this could change in the future), I want to be able to modify a class object, and have the GuiElement to auto-magically update the GuiElement with the reflected change.

public class GuiElement : ObservableImpl
{
    private string guiElementName;
    public String GuiElementName
    {
        get { return guiElementName; }
        set
        {
            guiElementName = value;
            NotifyObservers(guiElementName);
        }
    }

    private string status;
    public String Status
    {
        get { return status; }
        set
        {
            status = value;
            NotifyObservers(status);
        }
    }
}

public interface IObserver
{
    void Notify<T>(T watchObj);
}

public interface IObservable
{
    void Register(IObserver observer);
    void UnRegister(IObserver observer);
}

public class ObservableImpl : IObservable
{

    protected Hashtable _observerContainer = new Hashtable();

    public void Register(IObserver anObserver)
    {
        _observerContainer.Add(anObserver, anObserver);
    }

    public void UnRegister(IObserver anObserver)
    {
        _observerContainer.Remove(anObserver);
    }

    public void NotifyObservers<T>(T anObject)
    {
        foreach (IObserver anObserver in _observerContainer.Keys)
            anObserver.Notify(anObject);
    }
}

And then have my GuiElement update the Gui when notified of a change?

public partial class GuiElementControl : UserControl, IObserver
{

    public GuiElementControl()
    {
        InitializeComponent();
    }

    #region Implementation of IObserver

    public void Notify<T>(T watchObj)
    {
        if (watchObj is GuiElement)
        {
            UpdateGui(watchObj);
        }
    }

    private static void UpdateGui(object obj)
    {
        GuiElement element = obj as GuiElement;
        if (element != null)
        {
            NameLbl.Text = element.GuiElementName;
            StatusLbl.Text = element.Status;
        }
    }

    #endregion

would it be a more flexible design if I implemented data binding, instead of notifying an observer of changes? I guess what I'm really asking is, what's the most flexible way of visually representing a business object, that constantly has updates in real-time.

alt text http://devpupil.net/GuiConcept.PNG


回答1:


I'd use an Observer. GUIs that auto-update their values when the underlying model/data they represent changes is one of the classic examples of using the Observer pattern.




回答2:


When I developed our project, we came up with the Observer and EventListener patterns as you did. I believe that those patterns are the same concept as publish/subscribe pattern. We did choose the EventListener(EventObject) way to make loose-coupling between objects and to make more extensibility of functions for events.

The benefit of the EventListener is that we can add more reasonable event functions(?) to the EventListener based on our event requirements. Please, correct me if it goes wrong direction.

I hope it helps.

Tiger.



来源:https://stackoverflow.com/questions/1126035/observer-pattern-or-databinding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!