Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Design Pattern Nomenclature & Clarification: Provider, Service, Broker

Can someone define for me the conceptual difference is between a Provider, Service and Broker?

I regularly write MVC apps and offload much of the business logic to other classes. Nothing fancy, just pass in parameters and receive back POCO instance(s).

What is a correct label to give those classes performing the heavy lifting for my controller(s)?

like image 999
Shawn Avatar asked Sep 29 '11 15:09

Shawn


People also ask

What is pattern name in design pattern?

A design pattern systematically names, motivates, and explains a general design that addresses a recurring design problem in object-oriented systems. It describes the problem, the solution, when to apply the solution, and its consequences. It also gives implementation hints and examples.


1 Answers

A Provider is really just another name for the Strategy Pattern

Typically when someone mentions the use of a provider they are talking about some abstract contract to which many implementations could exist.

//As an abstract base class
public void SetupRoles(RoleProvider provider){}

//As an interface
public void SetupRoles(IRoleProvider provider){}

//As a delegate
public void SetupRoles(Action<String> addRole){}

A Service is usually meant to indicate a stateless object that has only methods on it. A service could be used as a Strategy, but doesn't necessarily have to be.

//Plain old service... doesn't even need the web
// CRAZY TALK MAN!!!
public static class RoleService
{
    public static void SetupRoles(){};
    public static String[] GetRoles(){};
}

A Broker is really just responsible for well... brokering. It is designed to move messages between services and objects, orchestrating the interactions between services in order to keep them isolated.

public class Broker
{
    public void SendImportantMessage(Message msg)
    {
        //Do some important processing here
        // Maybe some validation
        NotifySomeOtherServiceOrClassOrMaybeBobFromAccounting(msg);
    }
}
like image 60
Josh Avatar answered Sep 20 '22 16:09

Josh