I will change your code a bit to explain
Why flexibility?
Here is a snippet of code on your system that will print the username. What else, let the image be not only one, but also hundreds of code fragments that use UserDb directly, and they are distributed throughout your system.
public void printUserName(String userId) { UserDb db = getUserDb(); System.out.println(db.getUserName(userId)); }
Now, if we want to get user information from a text file? The only way to do this is to change all the code that UserDb uses in UserTextFile . This will take a lot of time, and it can easily introduce errors, because we can accidentally change what should not be.
We call these codes UserDb Coupling .
So, we have a UserManager to solve this problem.
public class UserManager { private UserDb db; public UserManager(UserDb db) { this db = db; } public String getUserName(String userId) {
If all codes in our system use UserManager as a way to get the username. When we want to switch to a text file, all we can do is change the code inside the UserManager .
But, in the real world of coding, UserManager may not be so simple, they may also have other responsibilities, such as checking input before the request. We can still introduce errors.
That is why we need another layer to remove this connection once for all.
Here is the interface
public interface iUserInfo { public String getUserName(int userId); public String getUserAge(string username); }
... and we make the UserManager dependent on iUserInfo
public class UserManager { private iUserInfo info; public UserManager(iUserInfo info) { this info = info; } public String getUserName(String userId) {
Now that we want to change UserDb to UserTextFile , all we do is write a new concrete iUserInfo class. UserManager will never notice, because the dose does not need implementation details.
The less we modified the code, the less chance of errors. This is the reason we want such flexibility.
This method is called Inversion of Control .
Factory method?
Factory method is one of the design patterns associated with creating an object. Check this question for more information.
Factory, Abstract Factory and Factory Method
The reason your profession mentions the Factory method is because this template is used to hide knowledge of creating objects, iUserInfo in this case, from another class, which means that this method / class is the only code associated with a particular class .
In this way, we can minimize the impact of changes to the iUserInfo implementation iUserInfo
Interface vs Abstract class?
The interface does all your specific work class without extending it. This is good because in Java you can only inherit one class.
On the other hand, an abstract class simplifies working with common code that is used between different implementations.
You can check this question in more detail Interface vs Base Class