If your VideoProcessing
class has a dependency on the ImageProcessing
class, however, you only wish to make your MainForm
aware of VideoProcessing
, then propagating your message in the manner that you are is probably acceptable. It does however create a strong coupling between your objects, which, you may or may not care about at this stage.
Another method could be to use some sort of message bus such as an EventAggregator
or a Mediator
. Both of these behavioural patterns are designed to decouple objects involved in the messaging process and provide a publish/subscribe
mechanism.
As an example if you were to implement the EventAggregator
your MainForm
would be a subscriber
, listening out for notifications sent from a publisher
which in your scenario is the ImageProcessing
class.
A contrived code example might look like the following:
public class MainForm : Form
{
private readonly EventAggregator eventAggregator;
private readonly VideoProcessing videoProcessing;
public MainForm()
{
this.eventAggregator = new EventAggregator();
this.eventAggregator.Subscribe<NewBitmapEvent>(HandleNewBitMap);
this.videoProcessing = new VideoProcessing(this.eventAggregator);
}
private void HandleNewBitmap(Bitmap args)
{
// Do something
}
}
public class VideoProcessing
{
private readonly EventAggregator eventAggregator;
private readonly ImageProcessing imageProcessing;
public VideoProcessing(EventAggregator eventAggregator)
{
this.eventAggregator = eventAggregator;
this.imageProcessing = new ImageProcessing(this.eventAggregator);
}
}
public class ImageProcessing
{
private readonly EventAggregator eventAggregator;
public ImageProcessing(EventAggregator eventAggregator)
{
this.eventAggregator = eventAggregator;
this.eventAggregator.Publish<NewBitmapEvent>(new Bitmap(...));
}
}
When the ImageProcessing
class publishes
a NewBitmapEvent
, only the MainForm
is notified as only it, and not the VideoProcessing
class is subscribed
to the event. Obviously if other classes were subscribed then they too would be notified when the event is published
.