ShareX/ShareX — pull request #386 by daylightt (2014-11-28 18:54:29+00:00)
Title: Added Stragety Pattern to "Screenshot.cs" in ScreenShotLib.

Hello developers of ShareX.

Here is a pull request so you may incorporate my refactoring into your branch of ShareX. This request consist of me implementing the [Strategy Pattern] (http://sourcemaking.com/design_patterns/strategy) into the Screenshot.cs class in the ScreenShotLib.

Before you had a partial static Screenshot.cs classes which had static methods defining the capture logic for the various capture types (rectangle, full-screen, window. etc). Although this process had worked in your project, I believe it was not the most efficient way of doing so. Classes which have static methods, should only hold constant variables, but in your case, the Screenshot.cs class had both static methods (capture logic) as well as static states/variables (ex: CaptureShadow, CaptureCursor, etc), this is a [code smell] (http://www.refactoring.com/catalog/replaceStaticVariableWithParameter.html) and should be avoided.

As a solution, I have implemented the strategy pattern to deal with this code smell as well as optimize the code for future maintainability. In doing so, I had also reduce coupling of the capture logic and image settings/returning the image. What I mean by this, In the previous Screenshot.cs class, the static methods were responsible for setting the user settings for capture, holding the capture logic and finally returning the captured Image. By implementing the strategy pattern, it separates the user settings and capture logic. This allows for easy change to the future user settings as well as future capture logic.

Brief description of change: created an abstract class iScreenShot which holds the user settings and am association to the capture logic (iCaptureType). The classes which extend the iScreenShot class will assing a runtime type to the iCaptureType, ensuring the right capture logic will be called. Created an interface, iCaptureType, where the classes which implement this interface will define the capture logic (taken directly from Screenshot.cs).

I hope you find this refactoring useful, I have kept and not made any changes to the Screenshot.cs class in the case you decide not to incorporate the strategy pattern into the final product of ShareX.

Thanks!
Stephen S.