简体   繁体   中英

Java class with pairs of static array values

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

i would encapsulates the Object1 and object2, the why is to make sure that they are available to use and access, at least they are not null. see below:

public static TopClass(){
    private static Object obj01 = null;
    private static Object obj02 = null;

    public Object getObj01(){
        if(obj01 == null){
            obj01 = new Object();
        }

        return (obj01);
    }

    public Object getObj02(){
        if(obj02 == null){
            obj02 = new Object();
        }

        return (obj02);
    }
}

or in your case the objects are in array tipe [],.

This would be a great place to use a Factory pattern. Maybe something like:

public class SetCoefProvider {
  private String[] set;
  private double[] coef;

  public SetCoefProvider(String[] set, double[] coef) {
    this.set  = set;
    this.coef = coef;
  }

  public String[] getSet() {
    return set;
  }
  public double[] getCoef() {
    return coef;
  }
}

public class SetCoefProviderFactory {
  public static SetCoefProvider createObj1Provider() {
    return new SetCoefProvider(new String[] {"a", "b", "c"}, new double[] {1,2,3});
  }
  public static SetCoefProvider createObj2Provider() {
    return new SetCoefProvider(new String[] {"x", "y", "z"}, new double[] {11,12,13});
  }
}

and then if you really want them to be singletons, you can always do something like:

public class SingletonSetCoefProviders {
  private static SetCoefProvider obj1Provider, obj2Provider;

  static {
    obj1Provider = SetCoefProviderFactory.createObj1Provider();
    obj2Provider = SetCoefProviderFactory.createObj2Provider();
  }

  public static SetCoefProvider getObj1Provider() {
    return obj1Provider;
  }
  public static SetCoefProvider getObj2Provider() {
    return obj2Provider;
  }
}

I will be creating many of these entries. any insight would be much appreciated :)

The idea is that with statics, you don't want to make many of them, that's the whole point of a static thing. Rethink and/or re-ask with more context about your goals, what you're intending to accomplish isn't clear.

i don't get the static part. why not do something like:

import java.util.*;
interface Foo {
    String[] set();
    double[] coef();
}
class FooImpl1 implements Foo {
    @Override public String[] set() {
        return set;
    }
    @Override public double[] coef() {
        return coef;
    }
    String set[]={"a","b","C"};
    double coef[]={1,2,3};
}
class FooImpl2 implements Foo {
    @Override public String[] set() {
        return set;
    }
    @Override public double[] coef() {
        return coef;
    }
    String set[] = {"x","y","z"};
    double coef[]= {11,12,13};
}
interface Bar {
    Foo foo1=new FooImpl1();
    Foo foo2=new FooImpl2();
}
public class So9577640 {
    public static void main(String[] args) {
        Foo foo1=new FooImpl1();
        System.out.println(Arrays.asList(foo1.set()));
        Foo foo2=new FooImpl2();
        System.out.println(Arrays.asList(foo2.set()));
        System.out.println(Arrays.asList(Bar.foo1.set()));
        System.out.println(Arrays.asList(Bar.foo2.set()));
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM