How to switch between implementations of my SomethingManager class?

I have an application that has an OrgManager interface that has an OrgManagerImpl not yet created

OrgManagerImpl should go and talk with some third-party REST API things that we don’t have access to yet.

So, while I am testing the web interface, I wrote mock OrgManagerImpl, which implements OrgManager, but in real time I will need to replace this implementation.

However, now I am in a situation where I want the servlet to choose an implementation of OrgManager, depending on some condition.

Also, when I call myOrgManager.findCompany ("SomeCompanyId"), I want myOrgManager to go and look at all the possible implementations to find a company and make a big list.

I would also like when I do myOrgManager.createCompany (company) to prefer one implementation over others.

It seems like I'm heading for the UserManagerManager object ... but I feel like it is probably missing some kind of Java design point or object oriented design that I should be aware of.

Can anyone suggest a template or strategy that I should use instead?

thanks

+4
source share
2 answers

Spring DI is very useful here.

You will switch to another implementation just by changing xml

+2
source

If you use Spring, then your implementation can be configured and introduced. The type of execution (i.e. implementation) can be easily changed.

Alternatively, if you are not using Spring, then you may need some kind of factory template where the factory decides which implementation you will give. Your factory may use some external configuration (for example, a configuration file, an environment variable, etc.) to make an implementation decision.

+2
source

Source: https://habr.com/ru/post/1337972/


All Articles