观察者模式或数据绑定
我的问题是,您会实现观察者模式,还是使用数据绑定来执行以下操作:
目前,我正在初始化 GuiDataObj 列表。 当事件被触发时,我查找 GuiDataObjById,然后修改该对象,该对象与 GuiElement 进行数据绑定; 更新 GUI。
有几个 GuiElement(目前都是同一类型,但这将来可能会改变),我希望能够修改类对象,并让 GuiElement 使用反映的更改自动更新 GuiElement 。
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?
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
如果我实现数据绑定而不是通知观察者更改,这会是一个更灵活的设计吗? 我想我真正要问的是,以视觉方式表示业务对象的最灵活的方式是什么,并且不断实时更新。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会使用观察者。 当它们表示的底层模型/数据发生变化时,GUI 会自动更新它们的值,这是使用观察者模式的经典示例之一。
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.
当我开发我们的项目时,我们像您一样提出了 Observer 和 EventListener 模式。 我相信这些模式与发布/订阅模式是相同的概念。 我们确实选择了EventListener(EventObject)方式来实现对象之间的松耦合,并为事件提供更多的功能扩展性。
EventListener的好处是我们可以根据自己的事件需求,给EventListener添加更合理的事件函数(?)。 如果方向错误,请纠正我。
我希望它有帮助。
老虎。
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.