如何在域驱动设计中将Intefaces与工厂模式一起使用?

时间:2022-08-31 12:41:15

Does it make sense to use interfaces for your domain object factories by default, or should interfaces be reserved for the factory classes only when you need them?

默认情况下为域对象工厂使用接口是否有意义,或者只有在需要时才为工厂类保留接口?

public IUserFactory
{
    User CreateNewUser();
}

public UserFactory : IUserFactory
{
    public User CreateNewUser()
    {
        return new User();
    }
}

5 个解决方案

#1


Here's a translation of the same problem into Java.

这是将相同问题转换为Java的过程。

Original example

public interface UserFactoryIF
{
   User createNewUser();
}

Then the implementation of the Factory

然后执行工厂

public class UserFactory implements UserFactoryIF
{
     public User createNewUser()
     {
         // whatever special logic it takes to make a User
         // below is simplification
         return new User();
     }
}

I don't see any special benefit to defining an interface for the factory, since you're defining a single interface for a centralized producer. Typically I find that I need to produce many different implementations of the same kind of product, and my factory needs to consume many different kinds of parameters. That is, we might have:

我没有看到为工厂定义接口有任何特殊好处,因为您为集中式生产者定义了单个接口。通常我发现我需要生成同一类产品的许多不同实现,我的工厂需要使用许多不同类型的参数。也就是说,我们可能会:

public interface User
{
    public String getName();
    public long getId();
    public long getUUID();
    // more biz methods on the User
}

The factory would look like this:

工厂看起来像这样:

public class UserFactory {

    public static User createUserFrom(Person person) {
        // ...
        return new UserImpl( ... );
    }

    public static user createUserFrom(AmazonUser amazonUser) {
         // ... special logic for AmazonWS user
        return new UserImpl( ... );          
    }

    private static class UserImpl implements User {
       // encapsulated impl with validation semantics to
       // insure no one else in the production code can impl
       // this naively with side effects
    }
}

Hope this gets the point across.

希望这可以解决问题。

#2


In your given example, I don't even see why you need to go Factory.

在你给出的例子中,我甚至不知道为什么你需要去工厂。

The essence of the Factory Pattern is to "Define an interface for creating an object, but let the subclasses decide which class to instantiate. The Factory method lets a class defer instantiation to subclasses." - Wikipedia

工厂模式的本质是“定义用于创建对象的接口,但让子类决定实例化哪个类.Plant方法允许类将实例化延迟到子类。” - *

Do you have a different type of Users, or User is itself a type of something. May be you didn't elaborate the thing clearly. We usually use interface in abstract factory method pattern, where we need to deal with multiple families of related objects.

您是否拥有不同类型的用户,或者用户本身就是某种类型的用户。可能你没有清楚地阐述这件事。我们通常在抽象工厂方法模式中使用接口,我们需要处理多个相关对象族。

NOTE: Don't forget, patterns are there to help us, it doesn't mean that we must use them because they are available, whether we need them or not.

注意:不要忘记,模式可以帮助我们,这并不意味着我们必须使用它们因为它们是可用的,无论我们是否需要它们。

#3


Not everything has to have an interface; if you have a single implementation of something, and no reason to have any other I can't see why define an interface.

并非所有东西都必须有接口;如果你有一个单独的实现,没有理由有任何其他我不明白为什么定义一个接口。

#4


Two things: (1) I'd wait until I needed (or saw a looming need) an alternative implementation before I'd create the interface and (2) interfaces almost always make unit testing easier, especially with mocking, so I frequently come up with a need for an interface right away.

两件事:(1)在创建界面之前,我会等到我需要(或看到迫在眉睫的需求)替代实现,并且(2)接口几乎总是使单元测试更容易,特别是使用模拟,所以我经常来需要立即接口。

#5


Creating the factory off an interface allows it much easier to test them with mock classes, as well as making it simpler to use IoC applications, so whilst I may not necessarily need them for the application functionality, I generally build and call most classes through interfaces.

通过接口创建工厂允许使用模拟类更容易地测试它们,以及使用IoC应用程序更简单,因此虽然我可能不一定需要它们用于应用程序功能,但我通常通过接口构建和调用大多数类。

If you are not considering Unit Tests or IoC patterns (religious opinions aside), I probably wouldn't bother.

如果你不考虑单元测试或IoC模式(抛开宗教观点),我可能不会打扰。

I find the biggest pain with using them, in Visual Studio at least, is that 'Go To Definition' on a property or function jumps to the Interface definition, not the class definition.

我发现使用它们最大的痛苦,至少在Visual Studio中,是对属性或函数的“转到定义”跳转到接口定义,而不是类定义。

#1


Here's a translation of the same problem into Java.

这是将相同问题转换为Java的过程。

Original example

public interface UserFactoryIF
{
   User createNewUser();
}

Then the implementation of the Factory

然后执行工厂

public class UserFactory implements UserFactoryIF
{
     public User createNewUser()
     {
         // whatever special logic it takes to make a User
         // below is simplification
         return new User();
     }
}

I don't see any special benefit to defining an interface for the factory, since you're defining a single interface for a centralized producer. Typically I find that I need to produce many different implementations of the same kind of product, and my factory needs to consume many different kinds of parameters. That is, we might have:

我没有看到为工厂定义接口有任何特殊好处,因为您为集中式生产者定义了单个接口。通常我发现我需要生成同一类产品的许多不同实现,我的工厂需要使用许多不同类型的参数。也就是说,我们可能会:

public interface User
{
    public String getName();
    public long getId();
    public long getUUID();
    // more biz methods on the User
}

The factory would look like this:

工厂看起来像这样:

public class UserFactory {

    public static User createUserFrom(Person person) {
        // ...
        return new UserImpl( ... );
    }

    public static user createUserFrom(AmazonUser amazonUser) {
         // ... special logic for AmazonWS user
        return new UserImpl( ... );          
    }

    private static class UserImpl implements User {
       // encapsulated impl with validation semantics to
       // insure no one else in the production code can impl
       // this naively with side effects
    }
}

Hope this gets the point across.

希望这可以解决问题。

#2


In your given example, I don't even see why you need to go Factory.

在你给出的例子中,我甚至不知道为什么你需要去工厂。

The essence of the Factory Pattern is to "Define an interface for creating an object, but let the subclasses decide which class to instantiate. The Factory method lets a class defer instantiation to subclasses." - Wikipedia

工厂模式的本质是“定义用于创建对象的接口,但让子类决定实例化哪个类.Plant方法允许类将实例化延迟到子类。” - *

Do you have a different type of Users, or User is itself a type of something. May be you didn't elaborate the thing clearly. We usually use interface in abstract factory method pattern, where we need to deal with multiple families of related objects.

您是否拥有不同类型的用户,或者用户本身就是某种类型的用户。可能你没有清楚地阐述这件事。我们通常在抽象工厂方法模式中使用接口,我们需要处理多个相关对象族。

NOTE: Don't forget, patterns are there to help us, it doesn't mean that we must use them because they are available, whether we need them or not.

注意:不要忘记,模式可以帮助我们,这并不意味着我们必须使用它们因为它们是可用的,无论我们是否需要它们。

#3


Not everything has to have an interface; if you have a single implementation of something, and no reason to have any other I can't see why define an interface.

并非所有东西都必须有接口;如果你有一个单独的实现,没有理由有任何其他我不明白为什么定义一个接口。

#4


Two things: (1) I'd wait until I needed (or saw a looming need) an alternative implementation before I'd create the interface and (2) interfaces almost always make unit testing easier, especially with mocking, so I frequently come up with a need for an interface right away.

两件事:(1)在创建界面之前,我会等到我需要(或看到迫在眉睫的需求)替代实现,并且(2)接口几乎总是使单元测试更容易,特别是使用模拟,所以我经常来需要立即接口。

#5


Creating the factory off an interface allows it much easier to test them with mock classes, as well as making it simpler to use IoC applications, so whilst I may not necessarily need them for the application functionality, I generally build and call most classes through interfaces.

通过接口创建工厂允许使用模拟类更容易地测试它们,以及使用IoC应用程序更简单,因此虽然我可能不一定需要它们用于应用程序功能,但我通常通过接口构建和调用大多数类。

If you are not considering Unit Tests or IoC patterns (religious opinions aside), I probably wouldn't bother.

如果你不考虑单元测试或IoC模式(抛开宗教观点),我可能不会打扰。

I find the biggest pain with using them, in Visual Studio at least, is that 'Go To Definition' on a property or function jumps to the Interface definition, not the class definition.

我发现使用它们最大的痛苦,至少在Visual Studio中,是对属性或函数的“转到定义”跳转到接口定义,而不是类定义。