vendredi 18 septembre 2015

Can the state pattern be utilized to substitute types for state?

I just learned about the State Design Pattern and I am wondering if it is appropriate to apply the pattern to "types". The situation is this: We have several different types of data imports, and depending which type the user selects, depends on how we import data.

My understanding of the state pattern is that it eliminates the need to create complex switch and/or if statements utilizing state. Like my import, I would also like to eliminate switch/if statements depending on type. Unlike state though, once the file is imported I will never change it's state or type to another state or type.

I want to eliminate the below code so when we have to add more types it will be easier to maintain:

    //Prompt user to select import type then go below

     switch (typeof(MyDataToImportType))
        {
            case FileLatePaymentsImport:
                //Import File1
                break;
            case DatabaseReportsImport:
                //connect to db
                break;
            default :
                break;
        }

Aucun commentaire:

Enregistrer un commentaire