Are passive objects considered good design practice?

I very often create an object that does not have public methods and is self-sufficient. It usually processes the events of arguments passed to its constructor in its private methods, and does not raise any events or does not disclose any public methods.

I call this type of object "passive" objects - objects that do not have public methods. All interaction occurs inside them in private methods and events of arguments passed in the constructor.

Usually this is some kind of utility class, for example, one that ensures that two forms will stick 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 a kind of controller that provides interaction between two types:

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

"" ?

?

+3
5

, . . . . , , , -, , .

"". "" , , , .

, .

+2

. , , !

+1

, , .

: , , , "" "". .

, , .

+1

, : ? , , , , , , , , .

, , .

+1

. , . .

UPDATE: , , ( ) , StickyForm

public class VeryStickyForm : StickyForm
{
//some implementation here
//but interface is completely inherited from StickyForm
}
public class SomewhatStickyForm : StickyForm
{
//some implementation here
//but interface is completely inherited from StickyForm
}

, ... . , : - , , , . , .

0

Source: https://habr.com/ru/post/1722767/


All Articles