What's the difference between the Service Locator and the Factory Design pattern?

前端 未结 3 1146
执念已碎
执念已碎 2021-02-07 10:55

I\'m using unity and I\'m creating a class that wrapps it and I dont\' know how to call it, service locator or factory, both encapsulate the creation of the objects, so.... what

3条回答
  •  一整个雨季
    2021-02-07 11:47

    A factory creates objects for you, when requested.

    Service locator returns objects that may already exist, that is services that may already exist somewhere for you.

    Just think about the meaning of the names:

    • Factory: is a place where objects are created.
    • Service: is something that can do something for you as a service.
    • Service locator: is something that can find something that can perform a service.

提交回复
热议问题