Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I'll use Java as an example. Let's say we have a class that looks like this:</p> <pre><code>public class ABC { public void doDiskAccess() {...} } </code></pre> <p>When I call the class, I'll need to do something like this:</p> <pre><code>ABC abc = new ABC(); abc. doDiskAccess(); </code></pre> <p>So far, so good. Now let's say I have another class that looks like this:</p> <pre><code>public class XYZ { public void doNetworkAccess() {...} } </code></pre> <p>It looks exactly the same as ABC, but let's say it works over the network instead of on disk. So now let's write a program like this:</p> <pre><code>if(config.isNetwork()) new XYZ().doNetworkAccess(); else new ABC().doDiskAccess(); </code></pre> <p>That works, but it's a bit unwieldy. I could simplify this with an interface like this:</p> <pre><code>public interface Runnable { public void run(); } public class ABC implements Runnable { public void run() {...} } public class XYZ implements Runnable { public void run() {...} } </code></pre> <p>Now my code can look like this:</p> <pre><code>Runnable obj = config.isNetwork() ? new XYZ() : new ABC(); obj.run(); </code></pre> <p>See how much cleaner and simpler to understand that is? We've just understood the first basic tenet of loose coupling: abstraction. The key from here is to ensure that ABC and XYZ do not depend on any methods or variables of the classes that call them. That allows ABC and XYZ to be completely independent APIs. Or in other words, they are "decoupled" or "loosely coupled" from the parent classes.</p> <p>But what if we need communication between the two? Well, then we can use further abstractions like an <a href="http://en.wikipedia.org/wiki/Event_model" rel="noreferrer">Event Model</a> to ensure that the parent code never needs to couple with the APIs you have created.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload