工厂如何知道要创建的对象类型? [英] How does a factory know which type of object to create?

查看:123
本文介绍了工厂如何知道要创建的对象类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我相信工厂方法设计模式适合我正在努力做的事情,但我不确定给予它多少责任(子类的知识)。在维基百科上使用工厂方法模式的示例描述了我几乎完全是这样的情况: / p>

I believe the factory method design pattern is appropriate for what I'm trying to do, but I'm not sure how much responsibility (knowledge of subclasses it creates) to give it. The example of using the factory method pattern at Wikipedia describes the situation I'm in almost exactly:

public class ImageReaderFactory 
{
    public static ImageReader getImageReader( InputStream is ) 
    {
        int imageType = figureOutImageType( is );

        switch( imageType ) 
        {
            case ImageReaderFactory.GIF:
                return new GifReader( is );
            case ImageReaderFactory.JPEG:
                return new JpegReader( is );
            // etc.
        }
    }
}

我的问题是, figureOutImageType 的功能是什么样的?在这个具体的例子中,我假设它检查 InputStream 中的文件头,以确定数据所在的图像格式。我想知道 ImageReaderFactory 本身知道如何解析文件头并确定文件类型是GIF,JPEG等,还是调用每个 Reader内的函数类,让它知道它是什么类型的图像。可能是这样的:

My question is, what does the figureOutImageType function look like? In this specific example, I would assume that it checks a file header in the InputStream to determine which image format the data is in. I would like to know if the ImageReaderFactory itself knows how to parse file headers and determine if the file type is GIF, JPEG, etc, or if it calls a function inside each Reader class that lets it know what type of image it is. Something like this, maybe:

int figureOutImageType(InputStream is)
{
    if(GifReader.isGIF(is))
        return ImageReaderFactory.GIF;
    else if(JpegReader.isJPEG(is))
        return ImageReaderFactory.JPEG;
    // etc.
}

似乎让工厂知道如何解析图像破坏封装,并让子类决定应该创建哪一个是工厂方法设计模式的一部分。然而,似乎 figureOutImageType 函数只是添加了一些冗余代码,因为为什么不让每个子类在 InputStream getImageReader 函数中跳过开关情况?

It seems like having the factory know how to parse images breaks encapsulation, and letting the subclasses decide which one should be created is part of the factory method design pattern. Yet, it also seems like the figureOutImageType function is just adding some redundant code, because why not just have each subclass perform its check on the InputStream in the getImageReader function and skip the switch case?

我以前没有任何使用工厂的经验,而且我希望能够从过去使用过他们的一些人获得一些洞察力,以最好的方式处理这个问题。让工厂了解其子类的内部工作是否可以了解,还是应该让工厂知道要创建哪一个工作,以及如何组织这些工作?

I haven't had any experience using factories before, and I was hoping to get some insight from some people who have used them in the past on the best way to handle this problem. Is it okay to have the factory know about the inner workings of its subclasses, or should they be responsible for letting the factory know which to create, and how do you organize it all?

谢谢!

推荐答案

工厂应该有一些想法来选择要创建的实际对象。例如,.NET中的 WebRequest.Create 方法应该能够通过检查 Uri 。它不需要解析整个事情。只需要区分哪个类将负责的部分(在您的示例中,它可能只是文件头)。

Factory should have some idea about choosing the actual object to create. For example, WebRequest.Create method in .NET, should be able to choose between the different protocol clients by checking the protocol part of the Uri. It doesn't need to parse the whole thing. Just the part required to distinguish which class is going to be responsible for it (in your example, it'll probably be just the file header).

关于您的问题打破封装,不是真的...大多数时候,工厂是硬编码的,已经知道不同类型的类及其功能。它已经取决于一组已知的类提供的功能,所以你不会增加很多。您还可以将工厂的检测部分封装在另一个可由工厂和子类使用的辅助类中(以DRY原则为准)。

Regarding your question about breaking encapsulation, not really... Most of the time, the factory is hardcoded and already knows about different types of classes and their features. It already depends on the functionality offered by a known set of classes, so you are not adding much to it. You can also encapsulate the detection part of the factory in another helper class that can be used both by the factory and the subclasses (in the sprit of DRY principle).

这篇关于工厂如何知道要创建的对象类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆