How to implement getPortfolioManager in Factory class?

I have read the FAQS but still not able to get how to implement the getPorfolioManager() function in Factory class.
I have created the object of the PortfolioManager interface using **Interface interfaceObject = new ClassImplementingInterface(argument);
Am I going right?

You need to create an object using the class, not the interface. You’ll need to use the class which implements that interface.

But the return type of the
public static PortfolioManager getPortfolioManager(RestTemplate restTemplate) {
is a Interface?
If I create a object of the implementing class .What do I return same Implementing class object?

Hey to answer your question , we are basically passing control over from the PortfolioManagerApplication to the PortfolioManagerFactory, now the PortfolioManagerFactory, obtains the parameter of RestTemplate, now rather than calling the methods of the Portfolio Manager Impl class, we use the Interface. One reason for this would be that , if the application code fails, in nomal case when you working on the portfolioManagerApplication file, the stacktrace would give away all the required details of your application code, but by using a Factory Pattern, you would limit the visibility of code, even in the case of failure

One example of this is given by in the Impl file, where you are returning list rather than List.
So when you call the getPortfolioManager() function in the PMApplication file , your control is now with the factory class from where you can execute the respective functions of impl class using the PortfolioManager Interface.

Sir,Can you please review my code and tell me where am I going wrong?.

May I know why you are returning an object of portfolioManagerimpl when the return type is portfoliomanager in Factory file?

Also you have logical errors in your code in impl file, can you correct them?

Sir,Can you point out the logical errors in Implication file.

This is something you have to on your own, Read the stacktrace, check the line number and the file name where the error occurs, understand from your logic as to why this error is occuring

See, you need to remember that when we dereference an interface object, it’ll point to the implementing class object that was returned. Try doing that and if you face an error in that, continue it on the thread.

1 Like

Try reading about the Factory Method, there is link given for your reference. It will be more than efficient.
–> Make an Interface.
–> Implement it.
–> Make an instance of a class.
Do this one by one at a time, you will get it.

1 Like

@jadgehimanshu1999 As the issue seem to be resolved and as you have also moved onto next modules, I am closing this topic.

For further queries you can create a new topic and continue there.