I have a factory in my Java appplication. it looks like:
// Common Interface
interface Currency {
String getSymbol();
}
// Concrete Rupee Class code
class Rupee implements Currency {
@Override
public String getSymbol() {
return "Rs";
}
}
// Concrete SGD class Code
class SGDDollar implements Currency {
@Override
public String getSymbol() {
return "SGD";
}
}
// Concrete US Dollar code
class USDollar implements Currency {
@Override
public String getSymbol() {
return "USD";
}
}
And I have a FactoryClass:
class CurrencyFactory {
public static Currency createCurrency (String country) {
if (country. equalsIgnoreCase ("India")){
return new Rupee();
}else if(country. equalsIgnoreCase ("Singapore")){
return new SGDDollar();
}else if(country. equalsIgnoreCase ("US")){
return new USDollar();
}
throw new IllegalArgumentException("No such currency");
}
}
So if a country String, for example, is "India" it returns rupees. I need to implement that if a country String is "all" it returns all objects as rupees, sgddollars and us dollars. Is any example of such thing?
Try Something like this
Create a Class as AllCurrency
public class AllCurrency implements Currency{
private Rupee rupee;
private SGDDollar sgdDollar;
private USDollar useDoler;
public AllCurrency (Rupee rupee,SGDDollar sgDoler,USDollar usDoller){
this.rupee = rupee;
this.sgdDollar = sgDoler;
this.usDoller = usDoller
}
@Override
public String getSymbol() {
return "all";
}
// add getters and setters
}
And your factory
public static Currency createCurrency (String country) {
if (country. equalsIgnoreCase ("India")){
return new Rupee();
}else if(country. equalsIgnoreCase ("Singapore")){
return new SGDDollar();
}else if(country. equalsIgnoreCase ("US")){
return new USDollar();
}else if(country. equalsIgnoreCase ("all")){
return new AllCurency(new Rupee(),new SGDDollar(),new USDollar());
}
throw new IllegalArgumentException("No such currency");
}