title: Reducing coupling simple example needed for beginner
body: <p>Just out of college and am coming across some code where I need to reduce coupling. But I don’t understand fully all the concepts and would like a simple example to help me. To get you started I have a person class with a single field, name. I have a method within that class to concatenate some text.</p>
  
  <p>I know this is a silly example, and most people would never consider reducing coupling in situations as simple as this but I only want a simple example to help me fully understand the code and concepts together.</p>
  
  <p>In the code behind the main window I put a text box, and a button. When the window loads it shows the current value of the person x name field. When the button is clicked, the x.PersonAddText method is called. Currently this example has coupling calculated at 8. With a 3 for the button click event and a 3 for the window loaded event.</p>
  
  <p>Is there any way, using this example we can get it down to less than this for either or both of them.</p>
  
  <p>Below is all my code:</p>
  
  <p>My Person Class:</p>
  
  <pre><code>public class Person
  {
      //Fields
      private string name;
  
      //Properties
      public string Name
      {
          get { return name; }
          set { name = value; }
      }
  
      //Constructors
      public Person()
      {
          name = "joe";
      }
  
      //Methods
      public string PersonAddText(string text)
      {
          return name += " - " + text;
      }
  
      //Interfaces (or additional code below here please to aid understanding)
  }
  </code></pre>
  
  <p>My Code Behind:</p>
  
  <pre><code>    Person x = new Person();
  
      public MainWindow()
      {
          InitializeComponent();
      }
  
      private void Window_Loaded(object sender, RoutedEventArgs e)
      {
          txtname.Text = x.Name;
      }
  
      private void button1_Click(object sender, RoutedEventArgs e)
      {
          txtname.Text = x.PersonAddText(txtname.Text);
          txtname.Text = x.Name;
      }
  </code></pre>
  
  <p>My Simple XAML:</p>
  
  <pre><code>&lt;Grid&gt;
      &lt;TextBox Name="txtname" Margin="12,12,12,0" Height="23" VerticalAlignment="Top" /&gt;
      &lt;Button Content="Add Text" Margin="12,41,12,0" Name="button1" VerticalAlignment="Top" Click="button1_Click" /&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p>I am having great difficulty understanding the tutorials around the internet explaining this. From what I see there are 3 ways to do this (it would be nice if possible, to have my code above converted to an example of all three):</p>
  
  <ul>
  <li>Service Locator</li>
  <li>Dependency Injection</li>
  <li>Inversion of control (IoC)</li>
  </ul>
  
  <p>The <a href="http://grantpalin.com/2009/05/20/reducing-code-coupling-introduction/" rel="noreferrer">article</a> explaining the stuff i have read is excellent, but the examples are not relevant to me as he is using VB and ASP.Net with database connection strings. This is totally opposite what I need, and I don’t want to be thinking about how to translate the code, while learning the concepts, and thinking also about how to apply it to something relevant. While the example is good, it’s just too much, and I would really appreciate any extra help.</p>
  
  <p>Edit History: Corrected spellings. Added the following to clarify my question:</p>
  
  <p>I understand the theory behind coupling and coheasion and why you should reduce one and increase the other. But we never got to code any examples in college. Also, while not covered in college, I do understand interfaces. However, I dont understand how to use them to reduce coupling.</p>
  
  <p>Added a link to <a href="http://grantpalin.com/2009/05/20/reducing-code-coupling-introduction/" rel="noreferrer">the article I refrenced above</a>.</p>
  
  <p>Edit 2: So far what I have now got is the following:</p>
  
  <pre><code>public interface IPerson 
  { 
      string Name { get; set; } 
      string PersonAddText(string text); 
  } 
  
  public class Person : IPerson 
  {
      //The code from the person class above
  }
  </code></pre>
  
  <p>How do I now use this in the mainwindow code behind? I am guessing I should replace</p>
  
  <pre><code>Person x = new Person();
  </code></pre>
  
  <p>with</p>
  
  <pre><code>IPerson x = new Person(); 
  </code></pre>
  
  <p>Is this correct, and if so, is there anything else I need to do. The reason I ask is because I still dont see any reduction in code coupling figures reported by visual studio (infact, it increases it by 1 on the main window code behind).</p>