在工厂 class 中实例化 classes 的工厂模式
Factory Pattern to instantiate classes in a Factory class
我有一个工厂 class,我在其中根据字符串匹配实例化 classes。
示例代码段:
class Factory
{
Dictionary test = new Dictionary(string, ICreate);
public FactoryMethod()
{
test.Add("classA",new a());
test.Add("classB",new b());
test.Add("classC",new c());
}
public ICreate Create(string toMatch)
{
return test[toMatch];
}
}
但现在我想匹配少量模式(文件路径),然后根据匹配的模式实例化 classes。
所以我的问题是我可以在哪里存储这些我想要匹配的模式?
它们应该在我的数据库中的 "table" 中还是我可以在这里有一个哈希表(当任何一个模式匹配时,它映射到 class 以实例化)?
首先,您的 Create
方法实际上并没有创建任何东西。它 return 是一个已经创建的实例。每次调用 Create
都会 return 相同的实例。
如果那是你想要的,那很好,但如果你每次都想要一个新实例,我可以提个建议吗?不是存储对象,而是存储 return 一个新对象的函数:
class Factory
{
Dictionary<string,Func<ICreate>> test = new Dictionary<string,Func<ICreate>>();
public Factory()
{
test.Add( "classA", () => new a() );
test.Add( "classB", () => new b() );
test.Add( "classC", () => new c() );
}
public ICreate Create(string toMatch)
{
var creationFunction = test[toMatch];
return creationFunction();
}
}
看看效果如何? lambda 表达式 () => new a()
可以像函数一样调用,每次调用都会实例化一个新的 a
。
至于模式匹配,你可以做类似的事情。我们为值添加了一个函数;现在让我们放一个作为钥匙。我们将存储一个接受字符串和 returns 布尔值的函数,而不是存储字符串。然后我们所要做的就是搜索第一个执行键 returns true.
的字典条目
class Factory
{
Dictionary<Func<string,bool>,Func<ICreate>> _map = new Dictionary<Func<string,bool>, Func<ICreate>>();
public Factory()
{
_map.Add
(
a => a.Contains("classA"),
() => new a()
);
_map.Add
(
a => a.Contains("classB"),
() => new b()
);
_map.Add
(
a => a.Contains("classC"),
() => new c()
);
}
public ICreate Create(string toMatch)
{
var func = _map.Where( e => e.Key(toMatch) ).First().Value;
return func();
}
}
public class Program
{
static public void Main()
{
var f = new Factory();
var o = f.Create("c:\folder\subfolder\classA");
Console.WriteLine("You just created an instance of '{0}'.", o.GetType().Name);
}
}
输出:
You just created an instance of 'a'.
在这个例子中,我们使用 Contains()
进行了模式匹配,但由于它是 Func,因此您可以使用任何字符串函数、正则表达式来编写您想要的任何表达式。您也可以混合搭配;例如,您可以使用正则表达式来识别需要 ClassA 的模式,但您可以使用常规 ==
比较来识别需要 ClassB 的模式。此外,如果您想确保提交的任何字符串都匹配一个且仅匹配一个模式,我们可以将 LINQ 函数从 First
更改为 Single
。
我有一个工厂 class,我在其中根据字符串匹配实例化 classes。
示例代码段:
class Factory
{
Dictionary test = new Dictionary(string, ICreate);
public FactoryMethod()
{
test.Add("classA",new a());
test.Add("classB",new b());
test.Add("classC",new c());
}
public ICreate Create(string toMatch)
{
return test[toMatch];
}
}
但现在我想匹配少量模式(文件路径),然后根据匹配的模式实例化 classes。
所以我的问题是我可以在哪里存储这些我想要匹配的模式? 它们应该在我的数据库中的 "table" 中还是我可以在这里有一个哈希表(当任何一个模式匹配时,它映射到 class 以实例化)?
首先,您的 Create
方法实际上并没有创建任何东西。它 return 是一个已经创建的实例。每次调用 Create
都会 return 相同的实例。
如果那是你想要的,那很好,但如果你每次都想要一个新实例,我可以提个建议吗?不是存储对象,而是存储 return 一个新对象的函数:
class Factory
{
Dictionary<string,Func<ICreate>> test = new Dictionary<string,Func<ICreate>>();
public Factory()
{
test.Add( "classA", () => new a() );
test.Add( "classB", () => new b() );
test.Add( "classC", () => new c() );
}
public ICreate Create(string toMatch)
{
var creationFunction = test[toMatch];
return creationFunction();
}
}
看看效果如何? lambda 表达式 () => new a()
可以像函数一样调用,每次调用都会实例化一个新的 a
。
至于模式匹配,你可以做类似的事情。我们为值添加了一个函数;现在让我们放一个作为钥匙。我们将存储一个接受字符串和 returns 布尔值的函数,而不是存储字符串。然后我们所要做的就是搜索第一个执行键 returns true.
的字典条目class Factory
{
Dictionary<Func<string,bool>,Func<ICreate>> _map = new Dictionary<Func<string,bool>, Func<ICreate>>();
public Factory()
{
_map.Add
(
a => a.Contains("classA"),
() => new a()
);
_map.Add
(
a => a.Contains("classB"),
() => new b()
);
_map.Add
(
a => a.Contains("classC"),
() => new c()
);
}
public ICreate Create(string toMatch)
{
var func = _map.Where( e => e.Key(toMatch) ).First().Value;
return func();
}
}
public class Program
{
static public void Main()
{
var f = new Factory();
var o = f.Create("c:\folder\subfolder\classA");
Console.WriteLine("You just created an instance of '{0}'.", o.GetType().Name);
}
}
输出:
You just created an instance of 'a'.
在这个例子中,我们使用 Contains()
进行了模式匹配,但由于它是 Func,因此您可以使用任何字符串函数、正则表达式来编写您想要的任何表达式。您也可以混合搭配;例如,您可以使用正则表达式来识别需要 ClassA 的模式,但您可以使用常规 ==
比较来识别需要 ClassB 的模式。此外,如果您想确保提交的任何字符串都匹配一个且仅匹配一个模式,我们可以将 LINQ 函数从 First
更改为 Single
。