Java:避免构造函数中的参数太多

发布于 2025-01-26 14:12:54 字数 811 浏览 1 评论 0原文

我有多个服务类serviceTest1serviceTest2sevicetest3

我有一个出厂类userfactory,它创建了一个新的<<代码> user 在每个请求上的

userFactory上都有一个方法createuser()

,该 当前每个请求都会调用createuser看起来像这样的

public User createUser(){

   return new User(new ServiceTest1(), new ServiceTest2(), new SeviceTest3());
}
  1. them thew thew thew thew

    代码>服务类是无状态的,所以我想避免在每个请求上创建一个新实例,

  2. 将来可能会有更多sevice类,我想避免用太多参数

    将构造函数混乱

这是解决此问题的好习惯是什么?

如果我让这些Service类是Singleton类,那会很好吗?并拥有一个名为serviceFactory的新类,它提供了这些Singleton服务

,然后,我可以将ServiceFactory实例放在constructor new User(serviceFactoy.getInstance())>

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())

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

单身情人 2025-02-02 14:12:55

我喜欢serviceFactory的想法,但我绝对不会使用单身人士。您可以将接口用作参数类型。然后可以通过您的userFactory或其中成员来实现此界面。

您的课程很容易通过模拟测试。服务是否重复使用(将来可能会改变),取决于接口的实现:

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

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

public interface ServiceRegistry {

  ServiceTest1 getServiceTest1()
  ...
}
  

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()
  ...
}
  
情魔剑神 2025-02-02 14:12:54

user类中创建私有静态最终ServiceTest

由于服务是无状态的,只需将它们放入用户,因此即使您有新服务,也不会有太多参数。另外,您不会在每个请求上创建服务实例。

public class User {
  
    private static final ServiceTest1 serviceTest1 = new ServiceTest1();
    private static final ServiceTest2 serviceTest2 = new ServiceTest2();
    // ...  
}

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();
    // ...  
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文