Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm not a PHP developer, so I'm not going to attempt to show any code, but here's what I would do. I would implement the <a href="http://en.wikipedia.org/wiki/Strategy_pattern" rel="nofollow">Strategy Pattern</a> and create an IServiceProvider interface. That interface could have a GetService() method. Then you would create four new objects: LocationService, ProductService, UserService, and DefaultService, all of which would implement the IServiceProvider interface. </p> <p>Now, in your factory, the constructor would take in a IServiceProvider and have one public GetService() method. When the method is called, it will use the strategy of the injected IServiceProvider. This improves extensability as you won't have to open the Factory everytime you have a new service, you would just create a new class that implements IServiceProvider.</p> <p>I decided quick to mock this up quick in C# so you would have an example. I understand this isn't the language you are using, but maybe it will help clarify what I'm saying. Code shown below.</p> <pre><code>public interface IServiceProvider { Service GetService(); } public class UserServiceProvider : IServiceProvider { public Service GetService() { //perform code to get &amp; return the service } } public class StaticPageTemplateServiceProvider : IServiceProvider { public Service GetService() { //perform code to get &amp; return the service } } public class DynamicPageTemplateServiceProvider : IServiceProvider { public Service GetService() { //perform code to get &amp; return the service } } public class DefaultServiceProvider : IServiceProvider { public Service GetService() { //perform code to get &amp; return the service } } public class ServiceFactory { public ServiceFactory(IServiceProvider serviceProvider) { provider = serviceProvider; } private IServiceProvider provider; public Service GetService() { return provider.GetService(); } } </code></pre>
    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. 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