[英]Can I use the Factory pattern when the types I want to create each have different input parameters?
In my windows forms project I have a parent form that is creates other user controls and then places on a TabControl. 在我的Windows窗体项目中,我有一个父窗体,该窗体创建其他用户控件,然后放置在TabControl上。 My problem is that the code in the parent form is getting very large and difficult to manage. 我的问题是父表单中的代码变得非常大且难以管理。
In my parent form I have methods that look like this: 在我的父母表格中,我有一些看起来像这样的方法:
public SubControl1 CreateSubControl1(Guid ID, Info userInfo, bool populate)
public SubControl1 CreateSubControl1(Guid ID, Info userInfo, bool populate, StateType1 state)
public SubControl2 CreateSubControl2(Guid ID, Info userInfo, bool populate, String dataFile, String dataFile2, )
public SubControl2 CreateSubControl2(Guid ID, Info userInfo, bool populate, String dataFile, String dataFile2, StateType2 state)
private SubControl3 CreateSubControl3(Guid ID, Info userInfo, bool populate, String dateFile)
private SubControl3 CreateSubControl3(Guid ID, Info userInfo, bool populate, String dateFile, StateType3 state)
private SubControl4 CreateSubControl4(Guid ID, Info userInfo, bool populate, WorkingFolder wf)
private SubControl4 CreateSubControl4(Guid ID, Info userInfo, bool populate, WorkingFolder wf, StateType4 state)
At the moment depending on which SubControl (1-4) I want I create I call the corresponding CreateSubControlX
method. 目前取决于我要创建哪个SubControl(1-4),我调用相应的CreateSubControlX
方法。 This works OK for now, however I am sure there is a better way of doing this perhaps by harvesting off all the creational code to a sort of factory class. 目前,这可以正常工作,但是我敢肯定,通过将所有创建代码收集到某种工厂类中,这是一种更好的方法。
However, because each of my derived types, have slightly different input parameters I am wondering how to do this? 但是,由于每个派生类型的输入参数都略有不同,所以我想知道如何执行此操作? Should I create a 'Simple' factory that has a general Create
method that takes in all the possible types of parameters and a type (to distinguish which SubControl 1 to 4 to create). 我应该创建一个具有简单Create
方法的“简单”工厂,该方法接受所有可能的参数类型和类型(以区分要创建的SubControl 1到4)。 I could then use null for any parameters I don't want to set. 然后,我可以对任何我不想设置的参数使用null。 This seems like a bad idea to me. 对我来说,这似乎是个坏主意。
Eg ControlFactory.Create(ID, userInfo, false, null, null, null, SubControlType1)
例如ControlFactory.Create(ID, userInfo, false, null, null, null, SubControlType1)
BaseControl
is the base class for all types of controls eg SubControl1
to SubControl4
. BaseControl
是所有控件类型的基类,例如SubControl1
到SubControl4
。
Can anyone offer any help? 谁能提供任何帮助?
What ever you do, do not create one mega method with a bezillion parameters. 无论您做什么,都不要创建具有庞大参数的大型方法。 It makes the code unreadable, and it makes life difficult for the people who will have to write code to call that method (including you). 它使代码不可读,并且使那些不得不编写代码才能调用该方法的人员(包括您)生活变得困难。
I mean really. 我是说真的 Look at this method call and tell me what it does: 查看此方法调用,并告诉我它的作用:
ControlFactory.Create(ID, userInfo, false, null, null, null, SubControlType1)
You can't. 你不能 With three null args and a true/false, it's unreadable, unmaintainable code. 具有三个null参数和true / false,它是不可读,不可维护的代码。
Make multiple overloaded or similar named methods that are as specific as you can make them. 使多个重载或类似的命名方法尽可能具体。 For frequently used methods, I would consider eliminating one of the boolean args by creating two methods, one hard coded to the True case and one hard coded to the False case. 对于经常使用的方法,我会考虑通过创建两种方法来消除布尔型参数之一,一种是硬编码为True情况,另一种硬编码为False情况。 These call a common internal function that takes a boolean arg for code sharing but not expected to be used by outside consumers. 它们调用一个通用的内部函数,该函数使用布尔arg进行代码共享,但不希望外部使用者使用。 You always want the most frequently performed actions to require the shorted path to complete - in terms of code execution and in terms of keystrokes required to get there. 您始终希望执行最频繁的操作需要较短的路径才能完成-就代码执行和到达那里所需的击键而言。
The path to creating methods (APIs) that are easy to use is in reducing the number of parameters, using types that are descriptive (enum better than boolean), and method and parameter names that aid in discovery via code completion tooltips. 创建易于使用的方法(API)的途径是减少参数的数量,使用描述性的类型(枚举比布尔更好的枚举)以及有助于通过代码完成工具提示进行发现的方法和参数名称。 Every time you go against this mantra you're creating a lot of extra work and complexity for yourself and everyone else. 每次违反此口头禅时,都会为您自己和其他所有人带来很多额外的工作和复杂性。
Your parameter list is growing to a point where it might be reasonable to introduce a parameter object. 您的参数列表已经增长到可以引入参数对象的地步。 Introducing this kind of object might make the factory pattern more maintainable in the long run. 从长远来看,引入这种对象可能会使工厂模式更易于维护。
BaseClass Create( ParameterObject parameterObject )
The type you create instances of can take any kind and number of parameters as long as the factory also knows about those parameters so that it can pass them on during the construction. 您创建的实例的类型可以使用任何种类和数量的参数,只要工厂也知道这些参数,以便它可以在构造过程中传递它们。
To make this maintainable, it's often best to have 1 abstract factory for 1 type. 为了使其易于维护,通常最好为1种类型创建1个抽象工厂。 So in your case with different SubControl
types, you'd probably be better off using a separate factory for each SubControl
. 因此,在使用不同SubControl
类型的情况下,最好为每个SubControl
使用单独的工厂。
If you are using .Net 4 you can have named arguments which are very nice :) 如果您使用的是.Net 4,则可以使用命名参数,这非常好:)
public CreateSubControl(Guid ID, Info userInfo, bool populate, String dateFile = null, String dataFile2 = null, StateType1 statetype1 = null, StateType2 statetype2 = null)
{
// Do stuff with those variables here
}
And to call it (just some examples): 并称之为(仅举几个例子):
CreateSubControl(id, userinfo, populate, dateFile = "blah", statetype2 = somethinghere)
CreateSubControl(id, userinfo, populate, dateFile = "blah", dateFile2 = "blah2", statetype3 = somethingelsehere)
But I do agree that its hard to read this code or tell what you are supposed to do and why there are so many similar types. 但是我确实同意很难阅读此代码或告诉您应该做什么以及为什么会有这么多类似类型。 Maybe you need an interface? 也许您需要一个界面?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.