观察者模式或数据绑定 [英] Observer pattern Or DataBinding

查看:106
本文介绍了观察者模式或数据绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是,你将实现观察者模式,或使用数据绑定做以下:

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

此刻,我初始化一个GuiDataObj的列表。当一个事件触发时,我查找GuiDataObjById然后修改对象,这是数据绑定到一个GuiElement;它更新了GUI。

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.

有几个GuiElements(目前都是相同的类型,但这可能会在将来改变),我想能够修改类对象,并使GuiElement自动神奇地更新GuiElement与反射的更改。

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);
    }
}

然后让我的GuiElement更新Gui一个变化?

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.

推荐答案

我会使用Observer。当底层模型/数据代表变化时,自动更新其值的GUI是使用Observer模式的典型示例之一。

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.

这篇关于观察者模式或数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆