1

I have been looking for Strategy Pattern and I saw This link which the guy has explained this pattern very well.

But as far as I know (maybe right or wrong) you shouldn't make a new class in another class (for the sake of being loosely coupled).

this.motor = new Motor(this)

Is there a better kind of implementation for that to not violate the principles (like IoC)?

Community
  • 1
  • 1
Mehrdad Kamelzadeh
  • 1,764
  • 2
  • 21
  • 39
  • Usually you implement design patterns as they are defined. And as far as i know they look the same except the syntax in any language. –  Jun 11 '13 at 15:21

4 Answers4

2

It would produce a more maintainable code to define both your strategy and context as interfaces:

interface IStrategy<T> where T : IContext
{
    T Context { get; }

    void Execute();
}

// this cab have other structures too depending on your common logic
// like being generic
interface IContext
{
}

I, myself prefer constructor injection. But in this case property injection is needed because one may need to change the strategy at run time.

Now you can implement/inject your concrete types.

Kaveh Shahbazian
  • 13,088
  • 13
  • 80
  • 139
  • Strategy Pattern is for changing the behaviour in run-time. How can I change it in runtime if I use Construction Injection in which I have to inject the concrete class in the Composition Root? Am I right? – Mehrdad Kamelzadeh Jun 11 '13 at 15:32
  • You are right and I've mentioned "But in this case property injection makes the code more clear" from my experience with this pattern. But thanks for remembering me why it was all about really! – Kaveh Shahbazian Jun 11 '13 at 17:08
  • Thanks @MehrdadKamelzadeh ; I have modified the post. – Kaveh Shahbazian Jun 11 '13 at 17:10
0

You can use Constructor Injection.

public class MyClass{

public MyClass(Motor motor){
       this.motor = motor;
   }

}

Then, it's up to your IOC container to supply the needed dependency.

Oscar
  • 13,594
  • 8
  • 47
  • 75
  • Strategy Pattern is for changing the behaviour in run-time. How can I change it in runtime if I use Construction Injection in which I have to inject the concrete class in the Composition Root? Am I right? – Mehrdad Kamelzadeh Jun 11 '13 at 15:33
  • The constructor still executes at runtime, you're still injecting the dependency at runtime. MyClass doesn't know what type of Motor it will receive when it's built, and it doesn't care. – Adrian Jun 11 '13 at 15:47
0

Sure, there are many possibilities. What about a strategy factory?

this.motor = MotorFactory.newMotor(MotorFactory.BOOST);

or simply a mutator method for injection (assuming IMotor is the abstract interface for motors:)

void setMotor(IMotor m) {
    this.motor = m;
}
Ernest Friedman-Hill
  • 80,601
  • 10
  • 150
  • 186
  • Will you please tell me how does MotorFactory look like? I mean how is its implementation? would you mind if I ask you to write the MotorFactory Class here? – Mehrdad Kamelzadeh Jun 11 '13 at 15:40
0

u can use "dynamic" in c# instead like this:

Method(dynamic input)

Method(DTO1 input) Method(DTO2 input) Method(DTO3 input)

mostafa kazemi
  • 514
  • 6
  • 7