Composition and interaction with the owner instance

I was wondering what is the best fit for accessing the owner instance when using composition (rather than aggregation).

public class Manager
{
    public List<ElementToManage> Listelmt;
    public List<Filter> ListeFilters;

    public void LoadState(){}
}

public class Filter
{
    public ElementToManage instance1;
    public ElementToManage instance2;

    public object value1;
    public object value2;

    public LoadState()
    {
    //need to access the property Listelmt in the owner instance (manager instance)
    //instance1 = Listelmt.SingleOrDefault(...
    }
}

      

So far, I am thinking of two possibilities:

  • Save the link to the owner in the filter instance.
  • Declare an event in the Filter class. The manager instance subscribes to it and the filter drops it when needed.

I like to use the second option better. It seems to me more OOP, and there are fewer dependencies between the classes (any refactoring will be easier later),

But debugging and tracing can be a little tricky in the long run. As far as business grade classes, I don't remember seeing events for this purpose.

Any insight would be appreciated

+3


source to share


3 answers


There is no concept of "owner" of an instance of a class, there should not be a strong connection between an instance Filter

and an object that has an instance of that object.

In this case, the event seems like a good fit: it allows you to bind freely, allowing you to use the functions you want. If you went with option # 1, on the other hand, you would have limited the overall usefulness of the class Filter

- now it can only be contained in the classes Manager

, I don't think that's what you would like.



In general, by looking at your code, which you can pass into the appropriate data, the method LoadState

works, so it doesn't need to "stretch".

+1


source


I recommend linking to the owner of the filter instance. The event can be handled by a large number of handlers and can change the result of the previous handler (s). And you propadly don't want to change the owner while the filter is alive without notifying the filter instance.



+1


source


My short answer: Nothing.

The first option to keep the link to the owner is problematic for several reasons. The class Filter

no longer bears any responsibility. Filter

and Manager

tightly bound. and etc.

The second option is a little better, and yes, I've used events in similar scenarios, it's rarely good.

It is difficult to give specific advice without much detail. Some thoughts:

1) Are you sure your classes are the way they should be? Maybe the class should be one ElementToManage

and one Filter

?

2) Who is in charge of creation Filter

? For example, if it Manager

could Manager

possibly provide a list as a construct parameter? Perhaps you can create a class FilterFactory

that does whatever initialization you need.

3) Who is calling filter.LoadState()

? Perhaps the required list can be passed as a parameter to the method LoadState()

.

4) I often use the "init design pattern" (my terminology). For example, I will BinaryTree

have where parent and child will point to each other. Factory

creates nodes in normal state and calls the initialization method with other required objects. The class gets tricky because I probably have to make sure that the uninitialized object throws an error for every other use and must ensure that the object is initialized only once, initialized only through Factory

, etc. But if it works, this is the best solution in my opinion.

5) I am still trying to find out "Dependency Injection" and not go anywhere, I think it might have something to do with your question. I wonder if anyone will have an answer related to dependency injection.

0


source







All Articles