mercredi 1 mars 2023

Implement Factory Pattern In GoogleGuice

I need to implement factory design pattern in GoogleGuice 4.2 (Java)

Requirement

I have operating system interface

public interface OperatingSystem {
String getName() ;
} 

My concrete classes

public class Windows implements OperatingSystem {
String getName() {
return "Product of Microsoft";
}
public class Android implements OperatingSystem {
String getName() {
return "OpenSource";
}
public class IOS implements OperatingSystem {
String getName() {
return "Product of Apple";
}

NOTE : I have unique identifier for associated to each case, i.e.,

  • If my input is Apple, I need to instance of IOS for my OperatingSystem.getname() Similarly for Windows, I need instance of Windows class For OpenSource Input, I need instance of Android

I have a OSActivity.java class, This class has a string variable and OperatingSystem. Based on the value of this string variable ( which is the Unique Identifier) how can I get the appropriate implementation of OPeratingSystem through dependency injection

Ask In my OSActivity.java class, I need to perform dependency injection @Inject on OperatingSystem class based on the UniqueIdentifier string variable value. Is it possible ?

Aucun commentaire:

Enregistrer un commentaire