0

I am wonder, you have some part of code you need to do in, for example, every method. I know everyone have another "style" of programming and everyone prefer another ways how to do something.
But for example i am writing now some adapter on some class (settings of web camera) where I can set for example color mode, exposure, gain, etc. In EVERY method (settings change) i need:
1) stop camera
2) set parameter !! this is only one step where will be another code !!
3) create camera instance again
4) start camera

I see in here 2 ways how to do that:
1) probably "ugly" written copy&paste code

StopCam();
_wCamDev.Gain=20; // COMMANDS TO SET DEFINED PARAMETER, FOR EXAMPLE GAIN
CreateCam();
StartCam();

this code you can have in every method and change only the second row (_wCamDev.Gain=20;). It would be pretty sure what this code is doing for another programmers, easlier for understanding after 2 years what those sequences are doing. But on the another hand, this code is not written right (atleast for me) because of multiple copy&paste code.
2) You can use something like "generic function in parameter" and you can have something like:

public void refreshCam(Func<T,out>){
   StopCam();
   CHANGE_PARAMS_BY_GEN_FUNCTION;
   CreateCam();
   StartCam();
}

The generic called function will set the parameter and you can have a little bit harder code for understanding, but no multiple sequences (copy&paste code) and you can easy in every method call this one refreshCam() with your params and you can specify them in other methods.

What "code" can you prefer? Why? Is there some another (better) way how to do that? Is it even possible to send generic function by parameter? I think it is the most important by adapter design-patern how to write it right.

tomdelahaba
  • 948
  • 3
  • 13
  • 26
  • 2nd one, for all the reasons in the world. First being that you'll have only one place to modify the day you'll need to upgrade your refresh sequence. – xlecoustillier Jul 20 '13 at 19:46
  • Yea thats good reason for the 2nd one. And now is that possible to write that generic function over param? because i found only function by param. i know a little bit how the generic works (i never did it in C# and in my life i was working with it 3years later - i never needed it). So i am not sure if its possible. – tomdelahaba Jul 20 '13 at 19:49
  • This should answer http://msdn.microsoft.com/en-us/library/bb549151.aspx – xlecoustillier Jul 20 '13 at 19:55
  • check this out on generic programming. without a doubt, modularize your code. high level languages like C# are written because we want abstract, generic code. http://www.boost.org/community/generic_programming.html – franklin Jul 20 '13 at 19:55
  • thanks for all comments, X.L.Ant answered me fast all what i wanted. But in the answers, i am still interesting in another interesting or horrible (deterrent) solutions as well :D :] – tomdelahaba Jul 20 '13 at 21:12

1 Answers1

0

You might consider implementing the Template Method Pattern. You create a base class that does all the general work (in some DoIt method) and delegates the setting of the gain to a abstract method (SetGain) that needs to be implemented by all the subclasses.

This might look a bit like overkill, but if you choose the names of the subclasses well, it might be more clear what the code is supposed to do.
In a modern language like C# this pattern could very well be implemented using a lambda or a delegate or Func<> as @X.L.Ant suggested.

Richard
  • 627
  • 7
  • 14