1

In the company i work for we are developing an application that is used from various customers (other companies). The core of the application is the same but when it comes to some details every customer has its own requirements.

I think the way to deal with this is the strategy pattern by injecting the appropriate strategy to the component that needs to use it.

My question is, is there a way to know which strategy implementation to inject depending on the customer that is using the application, without avoiding "cases", or "if elses"?

How would you implement it in real life applications?

public IStrategy GetStrategy(string customerName) {
    switch(customerName) {
        case "customer1":
            return new Strategy1();
        case "customer2":
            return new Strategy2();
    }
}

EDIT: As the accepted answer of this question (Strategy Pattern with no 'switch' statements?) suggests, "Strategy isn't a magic anti-switch solution.".

Are there any other opinions about that?

Community
  • 1
  • 1
Vagelis Ouranos
  • 482
  • 3
  • 14
  • Do you have one custom strategy type per customer or generic strategy types that the customers can choose from ? – guillaume31 Sep 28 '15 at 10:14
  • In most cases there is one strategy per customer. – Vagelis Ouranos Sep 28 '15 at 11:06
  • Regarding your edit -- what the accepted answer says is very true but has little to do with your problem. Switch and Strategy are about decision making. Your problem is matching values of decision criteria (`"customer1"`) with decision consequences (`Strategy1`). There are dozens of technical means to do that : keys and values in a config file, a table in a database, a dictionary structure in memory, a REST resource by the name of the customer, you name it. – guillaume31 Sep 28 '15 at 21:11
  • The thing is, if you expect to get a new customer once in a while or allow existing ones to change their mind, the correspondence shouldn't be hardcoded but specified in an easily modifiable place (a config file, a DB). – guillaume31 Sep 28 '15 at 21:13

1 Answers1

1

What about passing a strategyName parameter instead of customerName? I mean, if you retrieve your customerName from database, add one column for strategy name (it must be strategy class name). Then, call GetStrategy method with that strategyName and use Activator.CreateInstance method to create strategy class instance.

How to use Activator.CreateInstance (MSDN)

Activator.CreateInstance Example

Community
  • 1
  • 1
Kemal Kefeli
  • 2,276
  • 2
  • 18
  • 22
  • With that approach i would have to know what strategy i want to use. The reason for the factory to exist is to create the strategy depending on the customer. – Vagelis Ouranos Sep 28 '15 at 12:17
  • 1
    @Khronos You could store the strategy type besides each customer in a configuration table in your database. – guillaume31 Sep 28 '15 at 12:20
  • Keeping this information in the database doesn't make much sense to me. It would be impossible to know which strategy each customer uses by just looking at the code. – Vagelis Ouranos Sep 28 '15 at 12:35
  • 2
    I think that type of data shouldn't be in the code. Because, X customer can use Strategy1 for today, but maybe he/she will use Strategy2 for tomorrow. In this case you have to rebuild your code and deploy it. If you use my suggestion you just have to update database. – Kemal Kefeli Sep 28 '15 at 12:45
  • Amen to that. Don't hardcode things that can change every day. – guillaume31 Sep 28 '15 at 20:57
  • I'm not sure if i will go this way (storing the data in db) but i sure will think about it since no one else has expressed another opinion. So, I will accept this answer since you have proposed something that could actually work for me. Thank you both. – Vagelis Ouranos Sep 29 '15 at 07:06