I am creating a certain class using
MyClass class1 = new MyClass(ClassA.StaticSet1, ClassA.StaticCoef1);
MyClass class2 = new MyClass(ClassB.StaticSet1, ClassB.StaticCoef1);
so I wanted to gather all these static values in one class and call them using something like
MyClass class1 = new MyClass(TopClass.Obj1);
MyClass class2 = new MyClass(TopClass.Obj2);
where Obj1 and Obj2 are static entities containing the abovementioned pairs of values.
the closest thing I could do was creating static classes inside TopClass and extending one base class
so I got this ugly implementation
Public class TopClass{
public static class Base{
public String set[];
public double coef[];
public Base(s, c){
set = s;
coef = c;
}
}
public static class Obj1 extends Base{
public static String set[] = {"a","b","C"};
public static double coef[]= {1,2,3};
public Obj1(){
super(set, coef);
}
}
public static class Obj2 extends Base{
public static String set[] = {"x","y","z"};
public static double coef[]= {11,12,13};
public Obj2(){
super(set, coef);
}
}
}
then I call them with
Myclass class1 = new MyClass((TopClass.Base)(new TopClass.Obj1());
Myclass class2 = new MyClass((TopClass.Base)(new TopClass.Obj2());
but this wasn’t what I exactly wanted because the class became cumbersome especially that I will be creating many of these entries.
any insight would be much appreciated 🙂
thanks,
Hani
This would be a great place to use a Factory pattern. Maybe something like:
and then if you really want them to be singletons, you can always do something like: