Which code is better for programming

I have several different objects, but I have to do similar actions with them. What is better to use: 1. Several methods and use these objects as type parameters. 2. Use one method that receives the System.Object parameter as the parameter. And inside this method, I will check the type of the parameter and take some action.

For example, I have to send notifications for some actions. I have Action1, Action2, Action3 ... ActionN objects containing data about these actions. Should I use:

   public void SendNotificationForAction1(Action1 action) {}
   public void SendNotificationForAction2(Action2 action) {}
   public void SendNotificationForActionN(ActionN action) {}

or

   public void SendNotification(Object action) 
   {
       //here I will check type of action and do something
   }
+3
source share
8 answers

The first is type safe, the second is not. Therefore, if I choose between these two options, I would choose the first.

, ? Action, ? "GetDetailsForNotification", , SendNotificationForAction.

- , , , , :

interface IAction
{
   string GetDetailsForNotification();
}

public class Action : IAction{
   public string GetDetailsForNotification()
   {
        return "details from Action";
   }
}

public class Action2 : IAction{
   public string GetDetailsForNotification()
   {
        return "details from Action2";
   }
}


public void SendNotificationForAction(IAction action) {

   var details = action.GetDetailsForNotification();
   ...
}
+8

, :

? :

public void SendNotificationFor<T>(T action) {}

overload :

public void SendNotification(Action1 action) {}
public void SendNotification(Action2 action) {}
public void SendNotification(ActionN action) {}
+8

:

private interface INotificationStrategy<T> // or non-generic with object
{
    void SendNotification(T action);
}

public class StringNotificationStrategy : INotificationStrategy<string>
{
    public void SendNotification(string action)
    {
        throw new NotImplementedException();
    }
}

A Factory , ...

+2

.

, , , , typesafe.

, , (, ), , , , - .

0

.

SendNotification :

public void SendNotification<T>(T action) where T : SpecialAction {
}

/ SpecialAction , . :

[abstract class] [interface] SpecialAction {
    public string GetName();
    public bool CanDoX();
}
0

, , () , (, ).

interface ISendNotificationHandler
{
    Type ActionType { get; }
    void SendNotification(object action)
}  


class Action1SendNotificationHandler : ISendNotificationHandler
{
    public Type ActionType {get{return typeof(Action1);}}
    public void SendNotification(object action)
    {
        Action1 a = (Action1)action;
        // TODO: send notification
    }   
}

// your method originally posted 
public void SendNotification(Object action)     
{
        var handlers = new ISendNotificationHandler[]{ new Action1SendNotificationHandler(), /* etc*/}

        // 
        var handler = handlers.FirstOrDefault(h=>action.GetType().IsSubclassOf(h.ActionType))
        if(handler != null)
        {
            handler.SendNotification(action);
        }
        else
        {
            throw new Exception("Handler not found for action " + action);
        }
} 
0

Wiki Link .

, , , - () , .

0

:

ActionN Action:

class Action1: Action, INotify
{
   public void override SendNotification() {...}
}

, Action.

, , :

class Sender
{
    public void SendNotif(INotify actn)
    {
       actn.SendNotification();
    }

}

SendNotif().

0

All Articles