我发现自己经常创建一个没有公共方法并且是自包含的对象。 它通常处理在其私有方法中传递给其构造函数的参数事件,并且不会引发任何事件或公开任何公共方法。
我将这种类型的对象称为“被动”对象 - 没有定义任何公共方法的对象。 所有交互都发生在私有方法和构造函数中传递的参数事件中。
通常它是一些实用程序类,就像确保两个表单将粘在一起的实用程序类:
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.
更多推荐
发布评论