none
Strategy Pattern RRS feed

  • Question

  • Hi,

    Let's say I have Multiple strategies. How do you usually choose the right strategy to be executed?

    Something with tons of If/else?? that returns the strategy?

    Kinda confused

    Thanks
    Monday, November 24, 2008 9:50 PM

Answers

  • I would use an micro container like spring/unity/ninject instead of writing a huge if/else statement.

     

    { Gaja; }

    http://gajakannan.com/netarch.aspx

     

    Monday, November 24, 2008 10:51 PM
  • There are 3 choices that spring to mind. 

     

    1. Use conditional code (use factories and constructor injection to manage complexity).

    2. Roll your own dependancy injection with configuration file and instantiate dynamically.

    3. Use a dependancy injection framework like enterprise library/unity, spring or ninject.

     

    The choice is about managing code complexity so it is hard to know which is most appropriate without knowing the project. 

     

    I assume you haven't used dependancy injection - otherwise you wouldn't be asking the question :-) - so it may be hard to justify using a technology new to you on an otherwise simple project.  Likewise, dynamic instantiation via config files quickly becomes unmanageable.

     

    There are also non technical factors to consider.  For instance, has anyone on the team or in the organisation used any of these tools?  Would a container like unity make the code hard to maintain given the skillset within your organisation.

     

    Hope this helps.

    Tuesday, November 25, 2008 9:36 AM
  • Hi,

     

    Let's take a scenario. We have 10 stretegies and also in future these is probabality of adding more. But all these are satisfying a defined interface.

     

    Following are the options for the Context(class that consumes startegies) to decide which startegy class should be used:

     

    1. Create enumeration for all strategies. Pass enumeration as a parameter to Context and Context create instance

    of right staretgy by writing long switch OR if-else statements.

    Let's analyse this:

    Is it maintainable = NO

    Code is clean = NO

     

    2. Put the list of strategies in XML file. Write a container class that read xml and provide the functionality to create an instance of any startegy class. This container will create instance against the name of the staregy that client will pass. Now Context is in ease. Client knows which startegy it want to use. By passing name it will ask container to give an instance of strategy and pass that instance to Context. Context will consume that instance. No if else, no switch.

     

    Is it maintable = Yes, add new strategies in xml and cleint will start using those. No change on server side.

    Code is clean =  Yes

     

     

    Hope it will help.

     

    Thanks,

    Attiq

     

     

    Tuesday, November 25, 2008 9:46 PM

All replies

  • I would use an micro container like spring/unity/ninject instead of writing a huge if/else statement.

     

    { Gaja; }

    http://gajakannan.com/netarch.aspx

     

    Monday, November 24, 2008 10:51 PM
  • There are 3 choices that spring to mind. 

     

    1. Use conditional code (use factories and constructor injection to manage complexity).

    2. Roll your own dependancy injection with configuration file and instantiate dynamically.

    3. Use a dependancy injection framework like enterprise library/unity, spring or ninject.

     

    The choice is about managing code complexity so it is hard to know which is most appropriate without knowing the project. 

     

    I assume you haven't used dependancy injection - otherwise you wouldn't be asking the question :-) - so it may be hard to justify using a technology new to you on an otherwise simple project.  Likewise, dynamic instantiation via config files quickly becomes unmanageable.

     

    There are also non technical factors to consider.  For instance, has anyone on the team or in the organisation used any of these tools?  Would a container like unity make the code hard to maintain given the skillset within your organisation.

     

    Hope this helps.

    Tuesday, November 25, 2008 9:36 AM
  • Can I have an example of this dependency injection?

    We are using it with Enterprise Library and SCSF.
    Tuesday, November 25, 2008 2:55 PM
  • Hi,

     

    Let's take a scenario. We have 10 stretegies and also in future these is probabality of adding more. But all these are satisfying a defined interface.

     

    Following are the options for the Context(class that consumes startegies) to decide which startegy class should be used:

     

    1. Create enumeration for all strategies. Pass enumeration as a parameter to Context and Context create instance

    of right staretgy by writing long switch OR if-else statements.

    Let's analyse this:

    Is it maintainable = NO

    Code is clean = NO

     

    2. Put the list of strategies in XML file. Write a container class that read xml and provide the functionality to create an instance of any startegy class. This container will create instance against the name of the staregy that client will pass. Now Context is in ease. Client knows which startegy it want to use. By passing name it will ask container to give an instance of strategy and pass that instance to Context. Context will consume that instance. No if else, no switch.

     

    Is it maintable = Yes, add new strategies in xml and cleint will start using those. No change on server side.

    Code is clean =  Yes

     

     

    Hope it will help.

     

    Thanks,

    Attiq

     

     

    Tuesday, November 25, 2008 9:46 PM