Skip to main content
spelling cleanup
Source Link
gnat
  • 20.9k
  • 29
  • 115
  • 295

Can the Strategy pattern be implemented without significant branching?

Yes, by using a hashmap/dictionary where every StrategyimplementationStrategy implementation registers at. The factorymethodfactory method will become somthingsomething like

Class strategyType = allStrategies[orderType];
return runtime.create(strategyType);

Every strategy implementation must register a the the factory with its orderType and some information how to create a class.

factory.register(NEW_ORDER, NewOrder.class);

You can use the static constructor for registration, if your language supports this.

The register method does nothing more than adding a new value to the hashmap:

void register(OrderType orderType, Class class)
{
   allStrategies[orderType] = class;
}

[update 2012-05-04] 
This soulutionsolution is much more complex than the original "switch solution" which i would prefer most of the time.

However in an environment where strategies changes often (i.e. price calculation depending on customer, time, ....) this hashmap solution combined with an IoC-Container could be good solution.

Can the Strategy pattern be implemented without significant branching?

Yes, by using a hashmap/dictionary where every Strategyimplementation registers at. The factorymethod will become somthing like

Class strategyType = allStrategies[orderType];
return runtime.create(strategyType);

Every strategy implementation must register a the the factory with its orderType and some information how to create a class.

factory.register(NEW_ORDER, NewOrder.class);

You can use the static constructor for registration, if your language supports this.

The register method does nothing more than adding a new value to the hashmap:

void register(OrderType orderType, Class class)
{
   allStrategies[orderType] = class;
}

[update 2012-05-04] This soulution is much more complex than the original "switch solution" which i would prefer most of the time.

However in an environment where strategies changes often (i.e. price calculation depending on customer, time, ....) this hashmap solution combined with an IoC-Container could be good solution

Can the Strategy pattern be implemented without significant branching?

Yes, by using a hashmap/dictionary where every Strategy implementation registers at. The factory method will become something like

Class strategyType = allStrategies[orderType];
return runtime.create(strategyType);

Every strategy implementation must register a the the factory with its orderType and some information how to create a class.

factory.register(NEW_ORDER, NewOrder.class);

You can use the static constructor for registration, if your language supports this.

The register method does nothing more than adding a new value to the hashmap:

void register(OrderType orderType, Class class)
{
   allStrategies[orderType] = class;
}

[update 2012-05-04] 
This solution is much more complex than the original "switch solution" which i would prefer most of the time.

However in an environment where strategies changes often (i.e. price calculation depending on customer, time, ....) this hashmap solution combined with an IoC-Container could be good solution.

added 345 characters in body
Source Link
k3b
  • 7.5k
  • 1
  • 20
  • 31

Can the Strategy pattern be implemented without significant branching?

Yes, by using a hashmap/dictionary where every Strategyimplementation registers at. The factorymethod will become somthing like

Class strategyType = allStrategies[orderType];
return runtime.create(strategyType);

Every strategy implementation must register a the the factory with its orderType and some information how to create a class.

factory.register(NEW_ORDER, NewOrder.class);

You can use the static constructor for registration, if your language supports this.

The register method does nothing more than adding a new value to the hashmap:

void register(OrderType orderType, Class class)
{
   allStrategies[orderType] = class;
}

[update 2012-05-04] This soulution is much more complex than the original "switch solution" which i would prefer most of the time.

However in an environment where strategies changes often (i.e. price calculation depending on customer, time, ....) this hashmap solution combined with an IoC-Container could be good solution

Can the Strategy pattern be implemented without significant branching?

Yes, by using a hashmap where every Strategyimplementation registers at. The factorymethod will become somthing like

Class strategyType = allStrategies[orderType];
return runtime.create(strategyType);

Every strategy implementation must register a the the factory with its orderType and some information how to create a class.

factory.register(NEW_ORDER, NewOrder.class);

You can use the static constructor for registration, if your language supports this.

The register method does nothing more than adding a new value to the hashmap:

register(OrderType orderType, Class class)
{
   allStrategies[orderType] = class;
}

Can the Strategy pattern be implemented without significant branching?

Yes, by using a hashmap/dictionary where every Strategyimplementation registers at. The factorymethod will become somthing like

Class strategyType = allStrategies[orderType];
return runtime.create(strategyType);

Every strategy implementation must register a the the factory with its orderType and some information how to create a class.

factory.register(NEW_ORDER, NewOrder.class);

You can use the static constructor for registration, if your language supports this.

The register method does nothing more than adding a new value to the hashmap:

void register(OrderType orderType, Class class)
{
   allStrategies[orderType] = class;
}

[update 2012-05-04] This soulution is much more complex than the original "switch solution" which i would prefer most of the time.

However in an environment where strategies changes often (i.e. price calculation depending on customer, time, ....) this hashmap solution combined with an IoC-Container could be good solution

spelling, formatting
Source Link
gnat
  • 20.9k
  • 29
  • 115
  • 295
    > Can the Strategy pattern be implemented without significant branching?

Can the Strategy pattern be implemented without significant branching?

yesYes, by using a hashmap where every Strategyimplementation registers at. The factorymethod will become somthing like

Class strategyType = allStrategies[orderType];
return runtime.create(strategyType);

evereyEvery strategy implementation must register a the the factory with its orderType and some information how to create a class.

factory.register(NEW_ORDER, NewOrder.class);

youYou can use the static constructor for registration, if your language supports this.

theThe register method does nothing more than adding a new value to the hasmaphashmap:

register(OrderType orderType, Class class)
{
   allStrategies[orderType] = class;
}
    > Can the Strategy pattern be implemented without significant branching?

yes, by using a hashmap where every Strategyimplementation registers at. The factorymethod will become somthing like

Class strategyType = allStrategies[orderType];
return runtime.create(strategyType);

everey strategy implementation must register a the the factory with its orderType and some information how to create a class.

factory.register(NEW_ORDER, NewOrder.class);

you can use the static constructor for registration, if your language supports this.

the register method does nothing more than adding a new value to the hasmap

register(OrderType orderType, Class class)
{
   allStrategies[orderType] = class;
}

Can the Strategy pattern be implemented without significant branching?

Yes, by using a hashmap where every Strategyimplementation registers at. The factorymethod will become somthing like

Class strategyType = allStrategies[orderType];
return runtime.create(strategyType);

Every strategy implementation must register a the the factory with its orderType and some information how to create a class.

factory.register(NEW_ORDER, NewOrder.class);

You can use the static constructor for registration, if your language supports this.

The register method does nothing more than adding a new value to the hashmap:

register(OrderType orderType, Class class)
{
   allStrategies[orderType] = class;
}
Source Link
k3b
  • 7.5k
  • 1
  • 20
  • 31
Loading