Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Design patterns 这是否保证(或符合)工厂模式?_Design Patterns_Factory Pattern - Fatal编程技术网

Design patterns 这是否保证(或符合)工厂模式?

Design patterns 这是否保证(或符合)工厂模式?,design-patterns,factory-pattern,Design Patterns,Factory Pattern,我有一组字符串需要解析(使用正则表达式模式),以期找到某些类型的信息。信息的“类型”可以是电子邮件地址、ip地址或FQDN或其他。源字符串可以有单个值(一个电子邮件地址)、相同类型的多个值(例如,两个ip地址)、混合值(一个电子邮件地址和一个ip地址),也可以没有 为了表示找到的模式,我有一个类,它具有类型(email、ip等)及其值的属性。解析所执行的任何方法都应该返回所述类的列表,其中计数可以是零、一或更多 我的问题是,这种类型的场景对工厂模式有意义吗?我不能使用字符串作为参数传入的构造函数

我有一组字符串需要解析(使用正则表达式模式),以期找到某些类型的信息。信息的“类型”可以是电子邮件地址、ip地址或FQDN或其他。源字符串可以有单个值(一个电子邮件地址)、相同类型的多个值(例如,两个ip地址)、混合值(一个电子邮件地址和一个ip地址),也可以没有

为了表示找到的模式,我有一个类,它具有类型(email、ip等)及其值的属性。解析所执行的任何方法都应该返回所述类的列表,其中计数可以是零、一或更多

我的问题是,这种类型的场景对工厂模式有意义吗?我不能使用字符串作为参数传入的构造函数,因为构造函数返回单个类实例

然后我考虑了抽象工厂的方法,但是我的阅读工厂是为了返回不同的类而设计的

然后我阅读了另一个StackOverflow问题,其中有人指出WebRequest类的静态Create()方法是工厂模式。我的想法是,我可以通过传入源字符串来实现吗

更新:基于此响应(http://stackoverflow.com/a/4828511/240372)当您有“相同接口的不同实现”时,应该使用工厂模式。所以我的要求不符合这个标准。所以…我对最好的方法有点迷茫

编辑:我认为我使用电子邮件地址和ip地址的例子可能会让人们觉得我只是在处理“地址”问题,这会让人更加困惑。事实并非如此。让我添加一些伪代码来帮助说明

Class TypeClass
   Property Name As String
   Property Pattern As String
End Class

Class FoundValue
   Property TypeName As String
   Property Value As String
End Class

Dim possibleTypes as List(Of TypeClass)
possibleTypes.Add(New TypeClass() With {.Name = "Email", .Pattern = "some_regex_pattern" }
possibleTypes.Add(New TypeClass() With {.Name = "IPAddress", .Pattern = "some_regex_pattern" }
possibleTypes.Add(New TypeClass() With {.Name = "Date", .Pattern = "some_regex_pattern" }
possibleTypes.Add(New TypeClass() With {.Name = "Integer", .Pattern = "some_regex_pattern" }

Dim sourceStrings as List(Of String)
sourceStrings.Add("hello")
sourceStrings.Add("1.2.3.4")
sourceStrings.Add("someone@somewhere.com; who@what.com")
sourceStrings.Add("C:\Windows\notepad.exe 24 who@what.com")

For Each source in sourceStrings
    For Each type in possibleTypes
       ' compare type.pattern to source and return list of list of FoundTypes 
       '
       ' for example, the last source string would return:
       '  list containing
       '     New FoundValue() With { .TypeName = "Integer", .Value = "24" }
       '     New FoundValue() With { .TypeName = "Email", .Value = "who@what.com" }
       '
       '  whereas the second source would return
       '  list containing
       '     New FoundValue() With { .TypeName = "IPAddress", .Value = "1.2.3.4" }

谢谢。

我认为这里不需要工厂模式,原因是:您没有“同一接口的不同实现”

您的解析器可能有这种风格的代码

 List<Address> result = new empty list; 

 for each found address
      Address oneAddress = new Address
      result.put(oneAddress)
然后您可以使用工厂模式来创建正确的地址对象。

我不这么认为-

在我看来,你似乎想制作一个通用的界面,用于阅读文本中不同类型的“内容”。我将为实现下面接口的每种不同事物类型创建具体的类

public interface IThingReader
{
    IEnumerable<Thing> ReadThing(string content);
}
因此,上面的示例使用工厂来确定要创建的对象类(公共接口)的不同实现。

您的问题是“如何使用我的地址实现集计算字符串?”。我认为,如果EmailAddress、IPAddress等有共同之处,可以实现IAddress接口。然后,在对字符串求值的地方,可以返回一个字符串列表。但这不是工厂模式
public interface IThingReader
{
    IEnumerable<Thing> ReadThing(string content);
}
public class CompositeReader
{
    IThingReader[] Readers;

    public CompositeReader(IThingReader[] readers)
    {
       Readers = readers;
    }

    public List<Thing> ParseText(string text)
    {
        List<Thing> allThings = new List<Thing>();

           foreach(IThingReader reader in Readers)
           {
               IEnumerable<Thing> things = reader.ReadThing(text);
               allThings.AddRange(things);
           }

        return allThings;
    }
}
public class EmailAddress : Thing
public class IpAddress : Thing
public class Number : Thing

public class ThingFactory
{
    public Thing GetThing(string text)
    {
        if (IsEmailAddress(text))
        {
            return new EmailAddress(text);
        }
        else if (IsIpAddress(text))
        {
            int[] ipAddressParts = SplitIpAddressParts(text);
            return new IpAddress(ipAddressParts);
        }
        else
        {
            throw new UnrecognisedThingException(text);
        }
    } 
}