{"id":4680,"date":"2014-03-30T14:37:32","date_gmt":"2014-03-30T14:37:32","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/which-design-pattern-i-should-use-closed-collection-of-common-programming-errors\/"},"modified":"2014-03-30T14:37:32","modified_gmt":"2014-03-30T14:37:32","slug":"which-design-pattern-i-should-use-closed-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/which-design-pattern-i-should-use-closed-collection-of-common-programming-errors\/","title":{"rendered":"Which design pattern I should use? [closed]-Collection of common programming errors"},"content":{"rendered":"<p>I personally will go for decorator instead of strategy. IMHO, strategy are used more for configuration \/ logic flow, not situational-handling (in this case, connection). This is opinion-based though.<\/p>\n<p>Moreover, when doing something like this:<\/p>\n<pre><code>if(connectionAvailable()){\n\/\/ insert 1\n} else {\n\/\/ insert 2\n}\n<\/code><\/pre>\n<p>Can has race condition between <code>connectionAvailable<\/code> and insertion. It may be little though. This is my suggested design:<\/p>\n<pre><code>interface IFormSubmitter {\n    public void submitForm(FormData data);\n}\n\npublic class SqlLiteFormSubmitter : IFormSubmitter{\n    public void submitForm(FormData data){\n        \/\/insert into sqlite\n    }\n}\n\npublic class DatabaseFormSubmitter : IFormSubmitter{\n    public class DatabaseFormSubmitter(IFormSubmitter reservedSubmitter){\n        \/\/ property assignment\n    }\n    public void submitForm(FormData data){\n        try{\n            \/\/ insert into database\n        }\n        catch(SQLException ex){\n            if( isConnectionError(ex) ){\n                reservedSubmitter.submitForm(data);\n            }\n            else{\n                throw e;\n            }\n        }\n    }\n}\n<\/code><\/pre>\n<p>The hard part is, you need to define whether your exception thrown is based on connection or not, based from caught <code>exception<\/code> object.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I personally will go for decorator instead of strategy. IMHO, strategy are used more for configuration \/ logic flow, not situational-handling (in this case, connection). This is opinion-based though. Moreover, when doing something like this: if(connectionAvailable()){ \/\/ insert 1 } else { \/\/ insert 2 } Can has race condition between connectionAvailable and insertion. It [&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-4680","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4680","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=4680"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4680\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4680"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4680"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4680"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}