2

I have multiple service classes ServiceTest1, ServiceTest2, SeviceTest3

I have a factory class UserFactory, which creates a new User instance on every request

UserFactory has a method createUser() which gets called for every request

Currently, createUser looks something like this

public User createUser(){

   return new User(new ServiceTest1(), new ServiceTest2(), new SeviceTest3());
}
  1. These Service classes are stateless though, so I want to avoid creating a new instance on every request,

  2. There might be more Sevice classes in future, I want to avoid cluttering the constructor with too many parameters

What would be the good practice to fix this?

Is it good if I let these Service classes be singleton classes? and have a new class named ServiceFactory, which provides these singleton services

and then, I can just place the ServiceFactory instance in the constructor new User(ServiceFactoy.getInstance())

2
  • Can't you have an instance for the session or something similar in that direction? Are services for initializing a type User necessary? Commented May 4, 2022 at 17:54
  • @akuzminykh What do you mean by instance for the session exactly? It's not necessary, but I need to access it in User class Commented May 4, 2022 at 18:02

2 Answers 2

0

How about creating private static final ServiceTest in User class ?

Since the Services are stateless, just put them in to User, so you won't have too many parameters even if you have a new Service. Also, you won't create Service instance on every request.

public class User {
  
    private static final ServiceTest1 serviceTest1 = new ServiceTest1();
    private static final ServiceTest2 serviceTest2 = new ServiceTest2();
    // ...  
}
Sign up to request clarification or add additional context in comments.

2 Comments

This will work, but wouldn't it be difficult for testing?
It depends. If you have a lot of logic in User class, then it would be.
0

I like the idea with the ServiceFactory, but I would definitely not use a singleton. You could use an interface as parameter type. This interface can then be implemented by your UserFactory or a member of it.

Your classes can easily be tested with mocks. Whether a service is reused or not (may change in future), is up to the implementation of the interface:

public class UserFactory { 
  ...
  public UserFactory(ServiceRegistry registry) {
    this.registry = registry;

  public User createUser() { 
    return new User(registry);
  }
  ...
}

public interface ServiceRegistry {

  ServiceTest1 getServiceTest1()
  ...
}
  

3 Comments

This looks good, but the constructor parameters of UserFactory is something that I cant edit! Also if I don't use Singletons, how am I going to achieve just single instance of Services throughout lifetime of the application
This is a part of library, where UserFactory is invoked internally by that library
If the library should instantiate UserFactory only once, you could add a default constructor and call the other constructor with a ServiceRegistry implementation, that creates one instance of every service. The library uses the default constructor and in your tests you can use the other constructor. If the library should instantiate the factory more than once, you could read the registry from a static field.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.