mercredi 30 mai 2018

How to dynamically call a function for the strategy pattern?

I am working on a webapplication in .NET Core with Razor Pages (MVVM) with a form where the user can give me 4 options.

Each of those options perform the same action, but just a little bit different in terms of execution - that's why I'd like to implement the strategy pattern.

Is there a way to dynamically generate the function name in some way? It might be a silly question, but I just understand the basics.

// option = A
// option = B
// option = C
// option = D

public async Task<IActionResult> OnPostAsync()
{

    ...

    var option = Input.Option // from model
    if(option == "A") {
        A.DoAlgorithm(input)
    } else if(option = "B") {
        B.DoAlgorithm(ïnput)
    } else if(option = "c") {
        C.DoAlgorithm(input) 
    } else {
        D.DoAlgorithm(input) 
    }

    ...
}

It feels like I am missing the point here of this pattern if I would do this, so my question is: is there some way to dynamically call a function based on the input option? If I am using the wrong pattern for this, please do correct me.

Aucun commentaire:

Enregistrer un commentaire