Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Well, first off, what you have there is not an instance of a Strategy pattern. The <a href="http://en.wikipedia.org/wiki/Strategy_pattern" rel="noreferrer">Strategy Pattern</a> allows for the dynamic specification of a method for getting things done. What you have here is really more of a standard interface design, where you allocate responsibilities and abilities by interface inheritance.</p> <p>Edit: Let's use an example. Let's say that you have a group of Workers; you also have a set of Tasks. Each Worker can perform a Task. These Tasks can consist if several things, such as DoFoo() and DoBar(). Each Worker does not know what Task they will perform; they just know when they show up that they will do a Task.</p> <p>So we'll want to model Workers as having a Task that they will perform. Since the Tasks vary widely, we'll implement the Task as an interface.</p> <p>So we'll have:</p> <pre><code>public class Worker { public Task myTask; public Worker(Task task) { myTask = task; } public void DoWork() { myTask.DoTask(); } } } Interface Task { void DoTask(); } public class Task1 : Task { public void DoTask() { // Do whatever Task1 will do } } public class Task2 : Task { public void DoTask() { // Do whatever Task2 will do } } public class Job { public List&lt;Worker&gt; workers; public void Job() { workers.Add(new Worker(new Task1())); workers.Add(new Worker(new Task2())); } public void DoJob() { foreach (Worker worker in workers) { worker.DoWork(); } } public void ChangeJobsToTask1() { foreach (Worker worker in workers) { worker.myTask = new Task1(); } } public void ChangeJobsToTask2() { foreach (Worker worker in workers) { worker.myTask = new Task2(); } } } </code></pre> <p>So what happens is that when we instantiate a <code>Job</code>, the <code>Job</code> creates two <code>Worker</code>s. The first <code>Worker</code> has a <code>Task1</code> task; the second <code>Worker</code> has a <code>Task2</code> task. To make the <code>Worker</code>s do their <code>Task</code>s, we call the <code>DoJob()</code> method on the <code>Job</code> class, which just calls the <code>DoWork()</code> method on each of the <code>Worker</code>s, which in turn calls the <code>DoTask()</code> method on each of the <code>Task</code>s that the <code>Worker</code>s were set with. </p> <p>If we want to change the <code>Worker</code>s to all do <code>Task1</code>, we call the <code>ChangeJobsToTask1()</code> method, which sets the <code>Task</code> to <code>Task1</code> for all of the <code>Worker</code> objects contained by the <code>Job</code>; if, at that point, we call the <code>DoJob()</code> on the <code>Job</code> object, all the <code>Worker</code>s will perform the <code>Task1</code> task. Similarly, if we want to change the <code>Task</code>s to <code>Task2</code>, just call the <code>ChangeJobsToTask2()</code> method; all the <code>Worker</code>s will then execute the <code>Task2.DoTask()</code> when their <code>DoWork()</code> method is called.</p> <p>The important point of abstraction here is that the <code>Worker</code>s expose a <code>DoWork()</code> method, but they do not necessarily know what work it is that is being done. That is, the <code>Task</code>s for the <code>Worker</code>s are interchangeable; the <code>Worker</code>s just know that they're going to do a <code>Task</code>, but the specifics of what it is are unimportant to the <code>Worker</code>s.</p>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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