-2

I saw some piece of code looks like

public class A {

 public void doSomething(B b) {
    b.addSometing("queue1", getDefault());
    b.addSometing("queue2", getDefault());
    b.addSometing("queue3", getDefault());
}

private C getDefault() {
    C c = new C();
    c.setAutoCreate(true);
    c.setExired(false);
    c.setDelay(3500);

    return c;
}}

if We put C c var. (which is default for all objects of class A ) for every object of class A , we just use a lot of memory for large of objects of class A, maybe better to make C c static ? We will create only one instance of class C for whole class and use it to every object of class A . If we do so ,after that code will like like

public class A {
private static C c = new C();

static {
    c.setAutoCreate(true);
    c.setExired(false);
    c.setDelay(3500);
}

public void doSomething(B b) {
    b.addSometing("queue1", c);
    b.addSometing("queue2", c);
    b.addSometing("queue3", c);
}

}

I think it's better way , perhaps I'm wrong . Please give me advise .

Prabhat
  • 338
  • 4
  • 20
Daulet Cheri
  • 57
  • 1
  • 1
  • 5
  • The last one doesn´t make that much sense to me, if you add the same instance of `C` to a map for different keys, why consider going with a `Map` in the first place when you only do have one value for multiple keys? – SomeJavaGuy Jul 11 '16 at 12:09
  • 3
    _If_ you use a static instance for defaults it should be _immutable_, otherwise you could get side effects. `setAutocreate(true)` etc. doesn't look immutable though. – Thomas Jul 11 '16 at 12:10
  • If you make any change to c object in 'queue1' then objects in 'queue2' and 'queue3' will also change. If this is acceptable, then your static solutions seems ok. – Jeet Jul 11 '16 at 12:11
  • To avoid static instances you could try to use a factory which keeps track of the default instance (which must be immutable in that case) or use some DI framework like CDI - again you'll have to make sure your classes are immutable if they are to be shared without side effects. – Thomas Jul 11 '16 at 12:12
  • Adding on to what @Thomas said, you need to be 100% sure that no object will change the state of `c` during the lifetime of your program. Also, think about the 'state' and the data that `c` is storing. Whether the process that is retrieving and using `c` needs its own copy of data or can it work by sharing data with other instances (without ever modifying it) - should give you a good idea. – MojoJojo Jul 11 '16 at 12:28

1 Answers1

2

the answer to that question depends on the logic of the application and/or what A is supposed to do with the C instance. If just once instance of a particular object is required, I would suggest to use the Singleton pattern in Java https://en.wikipedia.org/wiki/Singleton_pattern .

However, if an instance of class A is changing its C member, it would be a bad idea to use the above-mentioned approach, because by changing the C member on one A object, you could interfere the computation that is done with, or on another A object.

If the C member contains configuration options or data that is used by all objects (as illustrated in the example above) and, hence, is not subject to change, You could use the singleton pattern to make it accessible for all A instances -- In my opinion that's fine.

Best, Julian

Julian
  • 1,694
  • 22
  • 29