{"id":994,"date":"2010-06-13T12:59:22","date_gmt":"2010-06-13T11:59:22","guid":{"rendered":"http:\/\/thekua.com\/atwork\/?p=994"},"modified":"2010-06-13T13:01:09","modified_gmt":"2010-06-13T12:01:09","slug":"refactoring-convert-static-into-instance","status":"publish","type":"post","link":"https:\/\/thekua.com\/atwork\/2010\/06\/refactoring-convert-static-into-instance\/","title":{"rendered":"Refactoring: Convert static into instance"},"content":{"rendered":"<p><em>You want to ensure reusing functionality does not have any other side effects in other places that might be using it.<\/em><\/p>\n<p><strong>Make the static instance explicit by introduce a static instance encapsulating any static state and delegate items to it. Use the instance.<\/strong><\/p>\n<p>Imagine you had this sort of code (below) and there&#8217;s dozens of use throughout a codebase. Static imports make it easy for people to simply increment a count, but in order to use the Counter, you have no assurance nothing else will try to use it.  <\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">public class Counter {\r\n  static int totalCount;\r\n\r\n  public static void count() {\r\n    totalCount++;\r\n  }\r\n}\r\n<\/pre>\n<p>The following transformations will help you keep existing clients happy:<\/p>\n<ul>\n<li>Introduce new method (instance level)<\/li>\n<li>Introduce global instance<\/li>\n<li>Delegate static method to new method (instance level)<\/li>\n<li>Turn static state into instance state<\/li>\n<\/ul>\n<p>The final result is below (note that this code is not thread-safe and not designed for concurrent programs, but is there to demonstrate the result of the refactoring steps): <\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class Counter {\r\n   private static final Counter INSTANCE = new Counter();\r\n\r\n   private int totalCount;\r\n\r\n   public static void count() {\r\n     INSTANCE.recordCount();\r\n   }\r\n\r\n   public void recordCount() {\r\n     totalCount++;\r\n   }\r\n}<\/pre>\n<p>Now we had a bit more of a complex example, but this refactoring helped us move to the right direction of using instances where possible, instead of sharing global state. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>You want to ensure reusing functionality does not have any other side effects in other places that might be using it. Make the static instance explicit by introduce a static instance encapsulating any static state and delegate items to it. Use the instance. Imagine you had this sort of code (below) and there&#8217;s dozens of [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,4],"tags":[],"class_list":["post-994","post","type-post","status-publish","format-standard","hentry","category-agile","category-development","post-preview"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Refactoring: Convert static into instance - patkua@work<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/thekua.com\/atwork\/2010\/06\/refactoring-convert-static-into-instance\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Refactoring: Convert static into instance - patkua@work\" \/>\n<meta property=\"og:description\" content=\"You want to ensure reusing functionality does not have any other side effects in other places that might be using it. Make the static instance explicit by introduce a static instance encapsulating any static state and delegate items to it. Use the instance. Imagine you had this sort of code (below) and there&#8217;s dozens of [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/thekua.com\/atwork\/2010\/06\/refactoring-convert-static-into-instance\/\" \/>\n<meta property=\"og:site_name\" content=\"patkua@work\" \/>\n<meta property=\"article:published_time\" content=\"2010-06-13T11:59:22+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2010-06-13T12:01:09+00:00\" \/>\n<meta name=\"author\" content=\"Patrick\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@patkua\" \/>\n<meta name=\"twitter:site\" content=\"@patkua\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Patrick\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/2010\\\/06\\\/refactoring-convert-static-into-instance\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/2010\\\/06\\\/refactoring-convert-static-into-instance\\\/\"},\"author\":{\"name\":\"Patrick\",\"@id\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/#\\\/schema\\\/person\\\/c942203c9ed3ff9e21c6c49a996ea3ae\"},\"headline\":\"Refactoring: Convert static into instance\",\"datePublished\":\"2010-06-13T11:59:22+00:00\",\"dateModified\":\"2010-06-13T12:01:09+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/2010\\\/06\\\/refactoring-convert-static-into-instance\\\/\"},\"wordCount\":228,\"commentCount\":1,\"articleSection\":[\"Agile\",\"Development\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/thekua.com\\\/atwork\\\/2010\\\/06\\\/refactoring-convert-static-into-instance\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/2010\\\/06\\\/refactoring-convert-static-into-instance\\\/\",\"url\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/2010\\\/06\\\/refactoring-convert-static-into-instance\\\/\",\"name\":\"Refactoring: Convert static into instance - patkua@work\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/#website\"},\"datePublished\":\"2010-06-13T11:59:22+00:00\",\"dateModified\":\"2010-06-13T12:01:09+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/#\\\/schema\\\/person\\\/c942203c9ed3ff9e21c6c49a996ea3ae\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/2010\\\/06\\\/refactoring-convert-static-into-instance\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/thekua.com\\\/atwork\\\/2010\\\/06\\\/refactoring-convert-static-into-instance\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/2010\\\/06\\\/refactoring-convert-static-into-instance\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Refactoring: Convert static into instance\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/#website\",\"url\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/\",\"name\":\"patkua@work\",\"description\":\"The intersection of technology and leadership\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/#\\\/schema\\\/person\\\/c942203c9ed3ff9e21c6c49a996ea3ae\",\"name\":\"Patrick\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/2ef85a5f91c2aac13efb3ac90077b4db7f8883cafd6e60f2bcecba6b092d4011?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/2ef85a5f91c2aac13efb3ac90077b4db7f8883cafd6e60f2bcecba6b092d4011?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/2ef85a5f91c2aac13efb3ac90077b4db7f8883cafd6e60f2bcecba6b092d4011?s=96&d=mm&r=g\",\"caption\":\"Patrick\"},\"sameAs\":[\"http:\\\/\\\/thekua.com\\\/atwork\"],\"url\":\"https:\\\/\\\/thekua.com\\\/atwork\\\/author\\\/patrick\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Refactoring: Convert static into instance - patkua@work","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/thekua.com\/atwork\/2010\/06\/refactoring-convert-static-into-instance\/","og_locale":"en_US","og_type":"article","og_title":"Refactoring: Convert static into instance - patkua@work","og_description":"You want to ensure reusing functionality does not have any other side effects in other places that might be using it. Make the static instance explicit by introduce a static instance encapsulating any static state and delegate items to it. Use the instance. Imagine you had this sort of code (below) and there&#8217;s dozens of [&hellip;]","og_url":"https:\/\/thekua.com\/atwork\/2010\/06\/refactoring-convert-static-into-instance\/","og_site_name":"patkua@work","article_published_time":"2010-06-13T11:59:22+00:00","article_modified_time":"2010-06-13T12:01:09+00:00","author":"Patrick","twitter_card":"summary_large_image","twitter_creator":"@patkua","twitter_site":"@patkua","twitter_misc":{"Written by":"Patrick","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/thekua.com\/atwork\/2010\/06\/refactoring-convert-static-into-instance\/#article","isPartOf":{"@id":"https:\/\/thekua.com\/atwork\/2010\/06\/refactoring-convert-static-into-instance\/"},"author":{"name":"Patrick","@id":"https:\/\/thekua.com\/atwork\/#\/schema\/person\/c942203c9ed3ff9e21c6c49a996ea3ae"},"headline":"Refactoring: Convert static into instance","datePublished":"2010-06-13T11:59:22+00:00","dateModified":"2010-06-13T12:01:09+00:00","mainEntityOfPage":{"@id":"https:\/\/thekua.com\/atwork\/2010\/06\/refactoring-convert-static-into-instance\/"},"wordCount":228,"commentCount":1,"articleSection":["Agile","Development"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/thekua.com\/atwork\/2010\/06\/refactoring-convert-static-into-instance\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/thekua.com\/atwork\/2010\/06\/refactoring-convert-static-into-instance\/","url":"https:\/\/thekua.com\/atwork\/2010\/06\/refactoring-convert-static-into-instance\/","name":"Refactoring: Convert static into instance - patkua@work","isPartOf":{"@id":"https:\/\/thekua.com\/atwork\/#website"},"datePublished":"2010-06-13T11:59:22+00:00","dateModified":"2010-06-13T12:01:09+00:00","author":{"@id":"https:\/\/thekua.com\/atwork\/#\/schema\/person\/c942203c9ed3ff9e21c6c49a996ea3ae"},"breadcrumb":{"@id":"https:\/\/thekua.com\/atwork\/2010\/06\/refactoring-convert-static-into-instance\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/thekua.com\/atwork\/2010\/06\/refactoring-convert-static-into-instance\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/thekua.com\/atwork\/2010\/06\/refactoring-convert-static-into-instance\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/thekua.com\/atwork\/"},{"@type":"ListItem","position":2,"name":"Refactoring: Convert static into instance"}]},{"@type":"WebSite","@id":"https:\/\/thekua.com\/atwork\/#website","url":"https:\/\/thekua.com\/atwork\/","name":"patkua@work","description":"The intersection of technology and leadership","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/thekua.com\/atwork\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/thekua.com\/atwork\/#\/schema\/person\/c942203c9ed3ff9e21c6c49a996ea3ae","name":"Patrick","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/2ef85a5f91c2aac13efb3ac90077b4db7f8883cafd6e60f2bcecba6b092d4011?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/2ef85a5f91c2aac13efb3ac90077b4db7f8883cafd6e60f2bcecba6b092d4011?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/2ef85a5f91c2aac13efb3ac90077b4db7f8883cafd6e60f2bcecba6b092d4011?s=96&d=mm&r=g","caption":"Patrick"},"sameAs":["http:\/\/thekua.com\/atwork"],"url":"https:\/\/thekua.com\/atwork\/author\/patrick\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/thekua.com\/atwork\/wp-json\/wp\/v2\/posts\/994","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thekua.com\/atwork\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thekua.com\/atwork\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thekua.com\/atwork\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/thekua.com\/atwork\/wp-json\/wp\/v2\/comments?post=994"}],"version-history":[{"count":6,"href":"https:\/\/thekua.com\/atwork\/wp-json\/wp\/v2\/posts\/994\/revisions"}],"predecessor-version":[{"id":999,"href":"https:\/\/thekua.com\/atwork\/wp-json\/wp\/v2\/posts\/994\/revisions\/999"}],"wp:attachment":[{"href":"https:\/\/thekua.com\/atwork\/wp-json\/wp\/v2\/media?parent=994"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thekua.com\/atwork\/wp-json\/wp\/v2\/categories?post=994"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thekua.com\/atwork\/wp-json\/wp\/v2\/tags?post=994"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}