Replace conditional with polymorphism - nice in theory but not practical

后端 未结 9 1490
时光说笑
时光说笑 2020-12-24 01:52

\"Replace conditional with polymorphism\" is elegant only when type of object you\'re doing switch/if statement for is already selected for you. As an example, I have a web

相关标签:
9条回答
  • 2020-12-24 02:52

    Polymorphism is a method of binding. It is a special case of thing known as "Object Model". Object models are used to manipulate complex systems, like circuit or drawing. Consider something stored/marshalled it text format: item "A", connected to item "B" and "C". Now you need to know what is connected to A. A guy may say that I'm not going to create an Object Model for this because I can count it while parsing, single-pass. In this case, you may be right, you may get away without object model. But what if you need to do a lot of complex manipulations with imported design? Will you manipulate it in text format or sending messages by invoking java methods and referencing java objects is more convenient? That is why it was mentioned that you need to do the translation only once.

    0 讨论(0)
  • 2020-12-24 02:54
    public abstract class BaseAction
    {
        public abstract void doSomething();
    }
    
    public class ViewAction : BaseAction
    {
        public override void doSomething() { // perform a view action here... }
    }
    
    public class EditAction : BaseAction
    {
        public override void doSomething() { // perform an edit action here... }
    }
    
    public class SortAction : BaseAction
    {
        public override void doSomething() { // perform a sort action here... }
    }
    
    
    string action = "view"; // suppose user can pass either
                            // "view", "edit", or "sort" strings to you.
    BaseAction theAction = null;
    
    switch (action)
    {
        case "view":
            theAction = new ViewAction();
            break;
    
        case "edit":
            theAction = new EditAction();
            break;
    
        case "sort":
            theAction = new SortAction();
            break;
    }
    
    theAction.doSomething();
    

    So I don't need conditionals here, but I still need it to decide which BaseAction type to instantiate first. There's no way to completely get rid of the conditionals.

    0 讨论(0)
  • 2020-12-24 02:55

    There are several ways to translate an input string to an object of a given type and a conditional is definitely one of them. Depending on the implementation language it might also be possible to use a switch statement that allows to specify expected strings as indexes and create or fetch an object of the corresponding type. Still there is a better way of doing that.

    A lookup table can be used to map input strings to the required objects:

    action = table.lookup (action_name); // Retrieve an action by its name
    if (action == null) ...              // No matching action is found
    

    The initialization code would take care of creating the required objects, for example

    table ["edit"] = new EditAction ();
    table ["view"] = new ViewAction ();
    ...
    

    This is the basic scheme that can be extended to cover more details, such as additional arguments of the action objects, normalization of the action names before using them for table lookup, replacing a table with a plain array by using integers instead of strings to identify requested actions, etc.

    0 讨论(0)
提交回复
热议问题