{"id":4461,"date":"2014-03-30T11:14:47","date_gmt":"2014-03-30T11:14:47","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/programming-to-an-interface-how-to-decide-where-its-needed-collection-of-common-programming-errors\/"},"modified":"2014-03-30T11:14:47","modified_gmt":"2014-03-30T11:14:47","slug":"programming-to-an-interface-how-to-decide-where-its-needed-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/programming-to-an-interface-how-to-decide-where-its-needed-collection-of-common-programming-errors\/","title":{"rendered":"Programming to an interface. How to decide where its needed?-Collection of common programming errors"},"content":{"rendered":"<p>Take a browse through the book <strong>Head-First Design Patterns<\/strong>&#8230; you&#8217;ll see good arguments for using interfaces that have nothing to do with TDD or polymorphism.<\/p>\n<p>Interfaces allow you to change an object&#8217;s behavior at runtime&#8230; Think of places where you&#8217;d need a handler for a particular behavior, but might not know what behavior is needed until runtime. In the case of computing expenses for employees&#8230; Managers might have a higher allowance for &#8216;entertainment&#8217; expenses than a regular employee.<\/p>\n<p>If your Employee object has a reference to an IExpenseCalculator interface, you can assign it a manager&#8217;s calculator or an employee&#8217;s calculator at runtime. Calling Employee.GetExpenses() will give you a differently calculated result for a manager than for a regular employee. Internally, the code would look like this:<\/p>\n<pre><code>public class Employee {\n  private IExpenseCalculator expenses;\n\n  public ExpenseSheet GetExpenses() {\n    return expenses.CalcExpenses();\n  }\n}\n<\/code><\/pre>\n<p>This example assumes that &#8216;expenses&#8217; is exposed as a property, and that IExpenseCalculator has a method for CalcExpenses().<\/p>\n<p>Interfaces are also closely tied to the concept of object factories&#8230; think database layer. When you&#8217;ve configured your data layer as a factory, you can create objects to connect to Sql Server, Oracle, or MySql dynamically, at runtime, based on configuration settings. But the client needs a concrete handle to the database layer object&#8230; enter Interfaces.<\/p>\n<p>Interfaces are a powerful tool, one that is often misused. Using them properly will take a shift in your way of thinking, but can help your application structure tremendously.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Take a browse through the book Head-First Design Patterns&#8230; you&#8217;ll see good arguments for using interfaces that have nothing to do with TDD or polymorphism. Interfaces allow you to change an object&#8217;s behavior at runtime&#8230; Think of places where you&#8217;d need a handler for a particular behavior, but might not know what behavior is needed [&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-4461","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4461","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=4461"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4461\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4461"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4461"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4461"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}