{"id":4261,"date":"2014-03-30T09:33:42","date_gmt":"2014-03-30T09:33:42","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/refactoring-a-static-class-to-separate-its-interface-from-implementation-collection-of-common-programming-errors\/"},"modified":"2014-03-30T09:33:42","modified_gmt":"2014-03-30T09:33:42","slug":"refactoring-a-static-class-to-separate-its-interface-from-implementation-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/refactoring-a-static-class-to-separate-its-interface-from-implementation-collection-of-common-programming-errors\/","title":{"rendered":"Refactoring a static class to separate its interface from implementation-Collection of common programming errors"},"content":{"rendered":"<p>Static Classes can be transform into Singleton Objects.<\/p>\n<p>Singleton Objects support interfaces.<\/p>\n<p>Interfaces can be used for different implementations.<\/p>\n<p><strong>(1) Definition of Problem.<\/strong><\/p>\n<p>Suppouse you have a class that have static members.<\/p>\n<p>&#8212;<\/p>\n<p>StringsClass.cs<\/p>\n<p>&#8212;<\/p>\n<pre><code>namespace Libraries\n{\n  public static class StringsClass\n  {\n\n    public static string UppercaseCopy(string Value)\n    {\n      string Result = \"\";\n\n      \/\/ code where \"Value\" is converted to uppercase,\n      \/\/ and output stored in \"Result\"\n\n    return Result;\n    } \/\/ string UppercaseCopy(...)\n\n    public static string LowercaseCopy(string Value)\n    {\n      string Result = \"\";\n\n      \/\/ code where \"Value\" is converted to lowercase,\n      \/\/ and output stored in \"Result\"\n\n      return Result;\n    } \/\/ string LowercaseCopy(...)\n\n    public static string ReverseCopy(string Value)\n    {\n      string Result = \"\";\n\n      \/\/ code where \"Value\" is reversed,\n      \/\/ and output stored in \"Result\"\n\n      return Result;\n    } \/\/ string ReverseCopy(...)  \n\n  } \/\/ class StringsClass\n\n} \/\/ namespace Libraries\n<\/code><\/pre>\n<p>&#8212;<\/p>\n<p>And, several code that uses that static elements, from that class.<\/p>\n<p>&#8212;<\/p>\n<p>StringsLibraryUser.cs<\/p>\n<p>&#8212;<\/p>\n<pre><code>using Libraries;\n\nnamespace MyApp\n{\n  public class AnyClass\n  {\n    public void AnyMethod()\n    {\n      string Example = \"HELLO EARTH\";\n      string AnotherExample = StringsClass.LowercaseCopy(Example);\n    } \/\/ void AnyMethod(...)  \n\n  } \/\/ class AnyClass\n\n} \/\/ namespace MyApp\n<\/code><\/pre>\n<p>&#8212;<\/p>\n<p><strong>(2) Transform, first, the class, into a non static class.<\/strong><\/p>\n<p>&#8212;<\/p>\n<p>StringsClass.cs<\/p>\n<p>&#8212;<\/p>\n<pre><code>namespace Libraries\n{\n  public class StringsClass\n  {\n\n    public string UppercaseCopy(string Value)\n    {\n      string Result = \"\";\n\n      \/\/ code where \"Value\" is converted to uppercase,\n      \/\/ and output stored in \"Result\"\n\n    return Result;\n    } \/\/ string UppercaseCopy(...)\n\n    public string LowercaseCopy(string Value)\n    {\n      string Result = \"\";\n\n      \/\/ code where \"Value\" is converted to lowercase,\n      \/\/ and output stored in \"Result\"\n\n      return Result;\n    } \/\/ string LowercaseCopy(...)\n\n    public string ReverseCopy(string Value)\n    {\n      string Result = \"\";\n\n      \/\/ code where \"Value\" is reversed,\n      \/\/ and output stored in \"Result\"\n\n      return Result;\n    } \/\/ string ReverseCopy(...)  \n\n  } \/\/ class StringsClass\n\n} \/\/ namespace Libraries\n<\/code><\/pre>\n<p>&#8212;<\/p>\n<p><strong>(3) Add code the allow class handle a single object.<\/strong><\/p>\n<p>&#8212;<\/p>\n<p>StringsClass.cs<\/p>\n<p>&#8212;<\/p>\n<pre><code>namespace Libraries\n{\n  public class StringsClass\n  {\n    private static Singleton instance = null;\n\n    private Singleton()\n    {\n      \/\/ ...\n    }\n\n    public static synchronized Singleton getInstance()\n    {\n        if (instance == null) {\n            instance = new Singleton();\n        }\n        return instance;\n    }\n\n    public string UppercaseCopy(string Value)\n    {\n      string Result = \"\";\n\n      \/\/ code where \"Value\" is converted to uppercase,\n      \/\/ and output stored in \"Result\"\n\n    return Result;\n    } \/\/ string UppercaseCopy(...)\n\n    public string LowercaseCopy(string Value)\n    {\n      string Result = \"\";\n\n      \/\/ code where \"Value\" is converted to lowercase,\n      \/\/ and output stored in \"Result\"\n\n      return Result;\n    } \/\/ string LowercaseCopy(...)\n\n    public string ReverseCopy(string Value)\n    {\n      string Result = \"\";\n\n      \/\/ code where \"Value\" is reversed,\n      \/\/ and output stored in \"Result\"\n\n      return Result;\n    } \/\/ string ReverseCopy(...)  \n\n  } \/\/ class StringsClass\n\n} \/\/ namespace Libraries\n<\/code><\/pre>\n<p>&#8212;<\/p>\n<p><strong>(4) Code that calls the class, should add the reference for the singleton.<\/strong><\/p>\n<p>&#8212;<\/p>\n<p>StringsLibraryUser.cs<\/p>\n<p>&#8212;<\/p>\n<pre><code>using Libraries;\n\nnamespace MyApp\n{\n  public class AnyClass\n  {\n    public void AnyMethod()\n    {\n      string Example = \"HELLO EARTH\";\n      string AnotherExample = StringsClass.getInstance().LowercaseCopy(Example);\n    } \/\/ void AnyMethod(...)  \n\n  } \/\/ class AnyClass\n\n} \/\/ namespace MyApp\n<\/code><\/pre>\n<p>&#8212;<\/p>\n<p><strong>(5) Define an interface, with similar declarations to the previous static class, and allow the singleton, to implement that interface. Omit the singletons members, in the interface declaration<\/strong><\/p>\n<p>&#8212;<\/p>\n<p>StringsClass.cs<\/p>\n<p>&#8212;<\/p>\n<pre><code>namespace Libraries\n{\n  public interface StringsInterface\n  {\n    string UppercaseCopy(string Value);  \n    string LowercaseCopy(string Value);  \n    string ReverseCopy(string Value);   \n  } \/\/ interface StringsInterface\n\n  public class StringsClass: StringsInterface\n  {\n    private static Singleton instance = null;\n\n    private Singleton()\n    {\n      \/\/ ...\n    }\n\n    public static synchronized Singleton getInstance()\n    {\n        if (instance == null) {\n            instance = new Singleton();\n        }\n        return instance;\n    }\n\n    public string UppercaseCopy(string Value)\n    {\n      string Result = \"\";\n\n      \/\/ code where \"Value\" is converted to uppercase,\n      \/\/ and output stored in \"Result\"\n\n    return Result;\n    } \/\/ string UppercaseCopy(...)\n\n    public string LowercaseCopy(string Value)\n    {\n      string Result = \"\";\n\n      \/\/ code where \"Value\" is converted to lowercase,\n      \/\/ and output stored in \"Result\"\n\n      return Result;\n    } \/\/ string LowercaseCopy(...)\n\n    public string ReverseCopy(string Value)\n    {\n      string Result = \"\";\n\n      \/\/ code where \"Value\" is reversed,\n      \/\/ and output stored in \"Result\"\n\n      return Result;\n    } \/\/ string ReverseCopy(...)  \n\n  } \/\/ class StringsClass\n\n} \/\/ namespace Libraries\n<\/code><\/pre>\n<p>&#8212;<\/p>\n<p><strong>(6) In the code, where your are using your singleton, the previous class that contained static methods, replace the singleton for an interface.<\/strong><\/p>\n<p>&#8212;<\/p>\n<p>StringsLibraryUser.cs<\/p>\n<p>&#8212;<\/p>\n<pre><code>using Libraries;\n\nnamespace MyApp\n{\n  public class AnyClass\n  {\n    public StringsInterface StringsHelper = StringsClass.getInstance().LowercaseCopy(Example);\n\n    public void AnyMethod()\n    {\n      string Example = \"HELLO EARTH\";\n      string AnotherExample = StringsHelper;\n    } \/\/ void AnyMethod(...)  \n\n  } \/\/ class AnyClass\n\n} \/\/ namespace MyApp\n<\/code><\/pre>\n<p>&#8212;<\/p>\n<p>Now, you can add other classes that support the same declarations, with different implementation.<\/p>\n<p>Cheers.<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Static Classes can be transform into Singleton Objects. Singleton Objects support interfaces. Interfaces can be used for different implementations. (1) Definition of Problem. Suppouse you have a class that have static members. &#8212; StringsClass.cs &#8212; namespace Libraries { public static class StringsClass { public static string UppercaseCopy(string Value) { string Result = &#8220;&#8221;; \/\/ code [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4261","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4261","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=4261"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4261\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4261"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4261"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4261"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}