I have a situation in a current application I'm working on that requires featuring batch editing and I'm wondering if there are any design patterns that I should be considering.

The task at hand is given a collection of objects that represent files on disk the user can edit a property on one object and have the edit ripple to all objects in the collection.

The properties that are edited belong to child objects of the file object.

I'm using C# / .NET in this instance and, if possible, I'd like to avoid too much or any reflection, hence the question about design patterns.

Doing this with plain framework events and reflection is the most obvious path but I'd be really interested to know if there is an established pattern for dealing with this kind of scenario?

  • Why do you think reflection would be useful here? On the face of it, the problem sounds like something you could solve with a simple foreach loop – Ben Cottrell Dec 3 '17 at 15:30
  • Indeed, but the scenario in my code is more complicated than the illustration I provided. I over-simplified. But, I am going to do some reorganisation to bring it down to something this simple. – Jammer Dec 3 '17 at 15:45
  • 3
    I appreciate the need to simplify for the purpose of asking the question, but somewhere it feels as if the essence of the problem you're facing (i.e. the reason you're considering reflection) has been lost along the way. Is the problem that you don't know the name of the property at compile time? In other words, what's stopping you from simply typing foreach(var x in y) x.a = b; ? – Ben Cottrell Dec 3 '17 at 16:12

Look into the Composite Pattern.

enter image description here

Basically it's two classes, a leaf and a composite, that implement the same interface. That means the client doesn't have to know which it's talking to. The composite is usually backed by a collection that takes leaves and composites. It passes any calls sent to it to everything in it's collection by looping through them.

Since you can put a composite in the collection this can follow the tree structure of a file system nicely.

  • This is actually very much like the solution I ended up coding. Will compare this to my solution and see if it can be improved. Thanks. – Jammer Dec 4 '17 at 9:38

Summary of the problem

You have a collection of objects. A user performs some modification to one of the object. You then want to perform the same modifications to the other objects.

Proposed solution

The first approach that comes to mind is the command pattern, where a command (edit or modification) is to be applied to a receiver (in your case the file).

This pattern requires the receiver to be identified in the command when it is created. So when the command is created for the initial object, you'd then create clones of this command for each receiver in the collection and launch the execution of these queued commands.

Commands are often used to implement an edit/undo mechanism (by executing a command, and simultaneously queuing the reverse command). So here you'd use it similarly to repeat the same command on other objects.

  • This is also a good approach, will have a look into this. Thanks. – Jammer Dec 4 '17 at 9:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.