jeudi 3 septembre 2020

Mediator Pattern Implementation with message passing and multi If/else statements

I have developed a GUI application that benefits from Mediator Design pattern. So there is a mediator object that has a one to many associations with the views that are colleagues. Each colleague class has a reference to the mediator instance and calls the notify method of the mediator object. here is the example of calling the mediator's notify method:

 _mediator.Notify(this,new NotificationEventArgs(Messages.DeliveryDateChanged)
            {
                MessageDateTime = deliveryTime
            });

So in the above example, the view sends a message of type DeliveryDateChanged and also sends an extra value that is from the type of DateTime class. And here is the NotificationEventArgs class:

public class NotificationEventArgs
{
    public Messages Message { get; set; }

    public Colleague ViewColleague { get; set; }

    public Action<DialogResult> DialogResultReceiver;
    public Item CategoryItem { get; set; }
    
    public string MessageText { get; set; }
    public float MessageFloat { get; set; }
    public bool MessageBool { get; set; }
    public  DateTime MessageDateTime { get; set; }
    public NotificationEventArgs()
    {
        
    }
    
    public NotificationEventArgs(Messages message)
    {
        Message = message;
    }

}

above class will be used for encapsulating messages that are sending between mediator and colleagues. And here is the notification receiver method in the mediator object:

 public void Notify(Colleague sender, NotificationEventArgs e){
            if (e.Message == Messages.InitRequest)
            {
                OfferRequestView?.Receive(new NotificationEventArgs(UIMessages.Hide));
               SelectionView?.Receive(new NotificationEventArgs(UIMessages.Show));
                BackStack.Push(OfferRequestView);
                activeView = SelectionView;
            } 
            else if (e.Message ==Messages.ShowCustomizationView)
            {
               SelectionView?.Receive(new NotificationEventArgs(Messages.Hide));
                CustomizationView?.Receive(new NotificationEventArgs(Messages.Show));
                BackStack.Push(SelectionView);
                activeView = CustomizationView;
            }
            else if (e.Message ==Messages.ShowDetails)
            {
                CustomizationView?.Receive(new NotificationEventArgs(Messages.Hide));
                ViewDetails?.Receive(new NotificationEventArgs(Messages.Show));
                BackStack.Push(CustomizationView);
                activeView = ViewDetails;

              
            }
            else if (...)
            {
                 ...
            }
            ...
            ...
            else if (...)
            {
                 ...
            }
        }

Now, I think that this notification method will contain many if/else statements. would this implementation hurt the code(for adding other views or removing a view) in the future? it this a real problem? Do I need to use the strategy design pattern for the notification handling? There are tens of conditional statements in this method.

The other problem is the NotificationEventArgs that is like the swiss knife. since it will be used for passing data between colleagues and mediators; it has many types, so if a view wants to pass a string it will use MessageText and if another view wants to pass a Date it will use its MessageDateTime attribute.

Do you know How can I achieve a better message passing system that is not using boxing and unboxing technique? (but not like the swiss army knife)

Aucun commentaire:

Enregistrer un commentaire