Stefan Radulian
Stefan Radulian

Reputation: 1436

Is this a pattern and if yes, what is its name?

How do I name this "WorkDistribtor"? This looks like a pattern but I cant's say what it is.

interface IWorker
{
    void Do();
}

class WorkerOne: IWorker 
{
    public void Do() {...}
}

class WorkerTwo: IWorker 
{
    public void Do() {...}
}

// ...

Instead of using one specific worker to Do() the job I am introducing an indirection that decides which actual worker should do the job. This indirection implements the same interface as a Worker but doesn't actually do the work but delegating it.

class WorkDistributor: IWorker
{
    private _workerOne = new WorkerOne();
    private _workerTwo = new WorkerTwo();
    //...

    //delegate work to the different workers
    //depending on some conditions
    public void Do()
    {
      if (whateverCondition)
      {
          _workerOne.Do();
      }

      if (anotherCondition)
      {
          _workerTwo.Do();
      }
    }

}

Upvotes: 1

Views: 86

Answers (4)

Mohamed.Radwan -MVP
Mohamed.Radwan -MVP

Reputation: 2744

I know it as abstract factory pattern

Abstract Factory on Wikipedia

Upvotes: 0

TehBoyan
TehBoyan

Reputation: 6900

It looks most likely like the Strategy pattern (also known as the policy pattern):

http://en.wikipedia.org/wiki/Strategy_pattern

where you actually allow each specific implementation to define different behavior depending on the context, and algorithms can be selected at runtime using your selector class(the one that defines the actual context).

Upvotes: 1

Michael Wiles
Michael Wiles

Reputation: 21194

What about dubbing it the "Indirection" pattern? ;-)

Upvotes: 0

Abdul Munim
Abdul Munim

Reputation: 19217

This is universally called Interface pattern :P

Upvotes: 0

Related Questions