Design template: which one to choose?

First of all, this is just a concept, I have no real programming. This is the situation:

I have a class A that uses Filesystemwatcher to view changes in a folder. (using events)

Then I have a "Collection Class" B that has a list A.

Now I want this to happen

The folder is changing. A detects this and sends a message to B, B passes this message to class C. Class C then starts a method that updates the GUI. (What changes were made, etc.)

Now I searched and thought for quite some time on this topic, but I can not find a solution. But, I found 2 design patterns:

Mediator and observer.

As a software engineer, to some extent I once created an Observer pattern, so I know some of its foundations.

Now to my questions:

  • Which template is best used in this situation?

  • How do I make B pass a message to C?

  • Do I need special events / delegates to pass data from A to B, or can I use inline events?

PS: I use C # as a programming language.

edit: Thanks everyone for helping me vote on the go.

+6
c # design-patterns
source share
5 answers

For what I am doing from this, there are a bunch of โ€œAโ€ objects that transmit events asynchronously to one B, which in turn passes this information to a single C character.

So, let B contain and observe A and C observe B.

If you have a lot of A, you might want B to do some assembly / caching of A events before notifying C. Especially if C serves the user interface.

Side Note : Do not rebuild your software. Try to be unbiased and always find the simplest and easiest solution. Use only the template in which it fits, and not just because it is possible. I saw how many people dropped proxies, command templates, observers, MVC, intermediaries, etc., where they were unnecessary.

Good luck.

+4
source share

The observer is fine. You can either make C an observer of B (so that B broadcasts events from A to C) or make C listen directly to A (this is probably the worst choice as it creates a direct dependency on C to A).

Note that this is basically a variation of the Model-View-Controller , where A is the models and C is the view. Now, whether B will make the right controller is largely dependent on his responsibilities: if it is just a collection of A, it is not a good idea to make it a controller. Without details about your classes and responsibilities, it's hard to say more.

+5
source share
public class A { public event FileSystemEventHandler FileSystemEvent; A() { this.fsw = new FileSystemWatcher(); this.fsw.OnFileSystemEvent += (sender, e) => { if(this.FileSystemEvent != null) this.FileSystemEvent(this,e); }; } } public class B { public event FileSystemEventHandler FileSystemEvent; B() { this.RegisterAClasses(); foreach( A item in this.AClasses ) item.FileSystemEvent += (sender, e) => { if(this.FileSystemEvent != null) this.FileSystemEvent(sender, e) }; } } public class C { C() { this.RegisterBClass(); this.BClass.FileSystemEvent += (sender, e) => { /* update gui... */ }; } } 

(psuedo code ...)

+3
source share

In the end, I used almost the same script to demonstrate Reactive Extensions.

RX is the formalism of the observer pattern, but generalizes as inverse / double with respect to the iterator pattern.

Details and source code - http://code.msdn.microsoft.com/RxDemos

+1
source share

The observer is the right pattern here. I do not understand why and said the following:

Then I have a "Collection Class" B which has a list A.

because for the Observer pattern, I think B should observe A, so there are some Bs in class A to listen to the fire of event A (the folder needs to be changed). Similarly, class C must observe B, so there are some C register objects in class B to listen for the fire of events B. Mandatory events for custom events or assembly events depend on your class. If its class is .NET, I think there is some kind of event for notification of a change in the dicrectory. If not, you should write your own events / delegate.

0
source share

All Articles