Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Okay, there's nothing specifically wrong under the LoD about passing an implementation object, a "plugin" in the constructor. What's important is that the interface of the class doesn't tell you much <em>about</em> that implementation.</p> <p>If your interface to WebGetTask depends on the exact implementation of HttpService, <em>that</em> violates the Law of Demeter.</p> <p>The trick here is to think about the interface signature of WebGetTask. The name itself suggests that you're not quite following the Law of Demeter — or principle of least knowledge — because you're defining a class that (1) is defined as being specific for the web, and (2) is a verb instead of a noun.</p> <p>Now, neither of those is necessarily wrong, but they are both "OO smells" if you will, signs that you may not be thinking object-ly enough.</p> <p>So let's try "refactoring" the design. First thing, think about a GetTask that has no "web" associated with it. You can then, either at construction time or at some later time build a service object and pass it in. If it's HttpService, that's fine, but the user of your class doesn't need any information about what's under the covers. </p> <p>Second thing, let's make it a noun. Call it TaskFactory — your intuition was leading you right there — with a ctor that takes an IOService, which I've just invented as being the abstract interface implemented by HttpService.</p> <p>Now, you have (this is sort of Java/C++ pseudocode, don't get excited about syntax details): </p> <pre><code> class Task { ... } class TaskFactory { public TaskFactory(IOServer svc){...} public Task get(){...} } </code></pre> <p>and you use it by writing</p> <pre><code> TaskFactory fac = new TaskFactory(new HttpService()); Task tsk = fac.get(); </code></pre> <p>Now, we kow the minimum about the innards of TaskFactory, the IO service, and for that matter Tasks.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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