Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Strategy Design Pattern by Delegate vs OOP

I wonder what's the pros/cons of using delegate vs OOP when implementing strategy design pattern?

Which one do you recommend to use? or what kind of problem does delegate solve? and why should we use OOP if OOP is better?

Thanks!

-tep

like image 558
tep Avatar asked Jun 11 '09 22:06

tep


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.


4 Answers

Both techniques can be powerful and valuable - here are some of my opinions about when to use which.

Use an Interface/Implementation approach when the strategy:

  1. maintains state
  2. needs configuration
  3. uses dependency injection
  4. needs to be configured by an IoC container (think ConnectionProvider)
  5. combines multiple responsibilities (think DataAdapter from ADO.NET)
  6. is too complex or long as a single method
  7. is likely to be subclassed to create new strategies
  8. needs to return state information to the caller
  9. needs to access internals of the object is applies to
  10. Would require too many direct parameters

Otherwise, tend to use delegates based on Func<> or Action<>, especially if

  1. There are likely to be a very large variety of strategies (think sort expressions)
  2. The strategy is best expressed as as lambda
  3. There's an existing method you want to leverage
like image 115
LBushkin Avatar answered Nov 09 '22 16:11

LBushkin


In favour of delegates:

  • Delegates are easier to implement in a light-weight way using lambda expressions and dynamic methods
  • Delegates can be created from "normal" methods with the right signature
  • Delegates being multi-cast can be useful at times (though relatively rarely outside eventing)

In favour of interfaces:

  • An object can implement an interface and still do other things: a delegate is just a delegate
  • An interface can have multiple methods; a delegate just has the one

Could go either way:

  • With interfaces you end up with two names: the interface and the method. With delegates you just have the one. Often I find that single-method interfaces either repeat the same name twice (with variations) or the method name is very bland

Personally I'm a big fan of delegates for their flexibility, but it really depends on the situation.

like image 38
Jon Skeet Avatar answered Nov 09 '22 15:11

Jon Skeet


In my opinion, if you use delegates then you're not actually implementing the Strategy pattern. You're actually implementing something more akin to the Observer pattern. The whole point of design patterns is that when you say "I've used the Strategy pattern here," everyone has a lot of context on what you've done. When you start saying things like "I've used the Strategy pattern except with my own personal modifications," then things get dicey.

But, if I understand what you're trying to say, one of the nice things about the Strategy pattern that isn't so clear with delegates is you can have a hierarchy of objects that implement a strategy.

Let's say that I'm testing some piece of software. I want to test it using the mouse and using the keyboard. So I'll implement a Strategy pattern to plug in the interface method to use for each test case ... so I can write the test case once and run it completely using the MouseStrategy and KeyboardStrategy. From there I can implement specializations such as MouseExceptForDialogsStrategy, a specialization of MouseStrategy. This sort of hierarchy, how to extend it and override it is easily understood by anyone familiar with OOP concepts ... whereas how to achieve and extend the same with delegates is much more complicated and very much more obscure.

As with many things ... it is not a question of "can you do it?", but "should you do it?".

like image 36
Lee Avatar answered Nov 09 '22 14:11

Lee


I like to use an interface to abstract my strategy. My concrete implementations then have a visible file for each strategy. When working with a Class instead of a method it gives you more flexibility. I can use Rhino mocks to mock out the strategy to test around it. I can also easy use DI frameworks such as Ninject to bind the strategy application wide very easily. I use Delegates to extract the implementation mostly in WinForm Dialogs.

like image 2
Aaron Avatar answered Nov 09 '22 15:11

Aaron