“被动”物体被认为是一种好的设计实践吗?(Are “passive” objects considered a good design practice?)

编程入门 行业动态 更新时间:2024-10-26 00:22:42
“被动”物体被认为是一种好的设计实践吗?(Are “passive” objects considered a good design practice?)

我发现自己经常创建一个没有公共方法并且是自包含的对象。 它通常处理在其私有方法中传递给其构造函数的参数事件,并且不会引发任何事件或公开任何公共方法。

我将这种类型的对象称为“被动”对象 - 没有定义任何公共方法的对象。 所有交互都发生在私有方法和构造函数中传递的参数事件中。

通常它是一些实用程序类,就像确保两个表单将粘在一起的实用程序类:

public class StickyForm : IDisposable { private readonly Form form; private readonly Form parentForm; public StickyForm(Form form, Form parentForm) { this.form = form; this.form.StartPosition = FormStartPosition.Manual; this.parentForm = parentForm; this.parentForm.LocationChanged += new EventHandler(parent_LocationChanged); this.parentForm.SizeChanged += new EventHandler(parent_SizeChanged); SetLocation(); } void parent_SizeChanged(object sender, EventArgs e) { SetLocation(); } void parent_LocationChanged(object sender, EventArgs e) { SetLocation(); } private void SetLocation() { //compute location of form based on parent form } public void Dispose() { this.parentForm.SizeChanged -= parent_SizeChanged; this.parentForm.LocationChanged -= parent_LocationChanged; } }

但有时它也是某种控制器,提供两种视图之间的交互:

public class BrowseController { private IBrowserView view; private IFolderBrowser folderBrowser; public BrowseController(IFolderBrowser folderBrowser, IBrowserView view) { this.view = view; this.folderBrowser = folderBrowser; this.folderBrowser.NodeOpened += folderBrowser_NodeOpened; } private void folderBrowser_NodeOpened(object sender, Core.Util.TEventArgs<IPictureList> e) { this.Browse(e.Value); } public void Browse(IPictureList content) { //stripped some code AddItemsToView(content); } private void AddItemsToView(IPictureList browser) { //call methods on view } }

这种“被动”物体是否被视为良好的设计实践?

这类课程有更好的名字吗?

I find myself very often creating an object that has no public methods and is self-contained. It typically handles events of arguments passed to its constructor in its private methods and does not raise any events or expose any public methods.

I am calling this type of objects "passive" objects - objects that do not have any public methods defined. All interaction occurs inside them in private methods and events of arguments passed in constructor.

Typically it is some utility class, like one that assures that two forms will be sticked together:

public class StickyForm : IDisposable { private readonly Form form; private readonly Form parentForm; public StickyForm(Form form, Form parentForm) { this.form = form; this.form.StartPosition = FormStartPosition.Manual; this.parentForm = parentForm; this.parentForm.LocationChanged += new EventHandler(parent_LocationChanged); this.parentForm.SizeChanged += new EventHandler(parent_SizeChanged); SetLocation(); } void parent_SizeChanged(object sender, EventArgs e) { SetLocation(); } void parent_LocationChanged(object sender, EventArgs e) { SetLocation(); } private void SetLocation() { //compute location of form based on parent form } public void Dispose() { this.parentForm.SizeChanged -= parent_SizeChanged; this.parentForm.LocationChanged -= parent_LocationChanged; } }

But sometimes it is also some kind of controller, providing interaction between two views:

public class BrowseController { private IBrowserView view; private IFolderBrowser folderBrowser; public BrowseController(IFolderBrowser folderBrowser, IBrowserView view) { this.view = view; this.folderBrowser = folderBrowser; this.folderBrowser.NodeOpened += folderBrowser_NodeOpened; } private void folderBrowser_NodeOpened(object sender, Core.Util.TEventArgs<IPictureList> e) { this.Browse(e.Value); } public void Browse(IPictureList content) { //stripped some code AddItemsToView(content); } private void AddItemsToView(IPictureList browser) { //call methods on view } }

Are such "passive" objects considered a good design practice?

Is there a better name for this kind of class?

最满意答案

对我来说好像很好的设计。 我不确定这个名字是被动的。 这些课看起来非常活跃。 他们对事件作出反应并做事。 我认为如果你必须在它上面调用方法让它做东西,那么一个类就更加被动,但通常它除了戳之外什么也不做。

如何命名“控制器”。 “controller”是UI中使用的类的更常用名称,它引起视图和数据之间的交互,并且它们通常不需要具有公共方法。

我确定还有其他名字的想法。

Seems like fine design to me. I'm not sure about the name passive though. Those classes do seem pretty active. They react to events and do stuff. I would think a class is more passive if you have to call methods on it to get it to do stuff, but normally it wouldn't do anything unless poked.

How about the name "controller". "controller" is the more usual name for a class used in a UI that causes interaction between a view and data, and they quite often don't need to have public methods.

I'm sure there's other ideas for names.

更多推荐

本文发布于:2023-08-04 11:27:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1415340.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:是一种   物体   passive   objects   practice

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!