vendredi 1 février 2019

Make a class as Singleton instead of Inject that class every time when using it

I have a class which is used as a common class in my project. That class has the access to some other core classes which were inject in that class, through which I can call some APIs. So when I need to call an API in a class, I usually Inject this common class and use the specific API. But when I inject the common class in many other classes, all the core classes also get injected even I need to use one or two of that core classes. This is my common class

public class ApiService {

private StuResource stuResource;
private VendResource vendResource;
private EduResource eduResource;
private RKEResource rKEResource;
private AleResource aleResource;
private GloResource gloResource;


@Inject
public ApiService(StuResource stuResource, VendResource vendResource,
     EduResource eduResource, RKEResource rKEResource,
     AleResource aleResource, GloResource gloResource) {
  this.stuResource  = stuResource;
  this.vendResource = vendResource;
  this.eduResource = eduResource;
  this.rKEResource = rKEResource;
  this.aleResource = aleResource;
  this.gloResource = gloResource;
}

{Methods for call APIs}

}

So I plan to use singleton patter, so that I can create an object of the common class one time and can use that object everywhere in other classes. Help me in this implementation. How can I implement singleton pattern in this situation.

Aucun commentaire:

Enregistrer un commentaire