构造函数的设计模式
我遇到了一个设计问题的挑战,我将在下面尝试描述该问题。
假设一个类(称为 A)有一个带有一堆参数的构造函数。由于在每个实例化中都写所有这些参数又累又脏,所以我编写了另一个类,称之为StyleSheetA,它封装了所有这些参数,并且是A的构造函数的唯一参数。这样,我可以准备一些默认值StyleSheet是后面要用到的模板,如果需要的话我可以修改它们。
此时,我需要扩展 A。假设 B 扩展 A。B 将有自己的样式表,即 StyleSheetB。我认为StyleSheetB扩展StyleSheetA是合适的,这样通过一个样式表参数,B的构造函数也可以构造其超类A。但我担心这种设计可能存在缺陷。例如,如果我决定为样式表添加 getter/setter 该怎么办?有没有一种新颖的方法来处理所有这些情况?我是不是走错路了?对于那些感到困惑的人,我在这里附上一些代码:
class A
{
StyleSheetA ss;
A(StyleSheetA ss)
{
this.ss = ss;
// Do some stuff with ingredients of styleSheet
}
}
class StyleSheetA
{
int n1;
int n2;
// :
// :
int n100;
}
class B extends A
{
B(StyleSheetB ss)
{
super(ss);
// Do some stuff with ingredients of styleSheet
}
}
class StyleSheetB extends StyleSheetA
{
int n101;
int n102;
// :
// :
int n200;
}
感谢您的任何帮助或建议,也将不胜感激您的批评者。
编辑:我正在使用 java me 进行开发,因此没有泛型支持。
I have been challenged by a design issue which I will try to describe below.
Suppose that a class, call it A, has a constructor with a bunch of parameters. Since it is tiring and dirty to write all those parameters in each instantiation, I have written another class, call it StyleSheetA, which encapsulates all those parameters and is the only parameter to the constructor of A. In this way, I can prepare some default StyleSheetA templates to be used later, and if it is needed, I can modify them.
And at this point, I need to extend A. Suppose B extends A. B will have its own stylesheet, namely StyleSheetB. I think it will be appropriate that StyleSheetB extends StyleSheetA, so with one stylesheet parameter, constructor of B can also construct its super class A. But I am afraid of the possibility that this design may have flaws. For example what if I decide to add a getter/setter for the stylesheet? Is there a novel way to handle all these situations? Am I in the wrong way? For those who are confused, I attach some code here:
class A
{
StyleSheetA ss;
A(StyleSheetA ss)
{
this.ss = ss;
// Do some stuff with ingredients of styleSheet
}
}
class StyleSheetA
{
int n1;
int n2;
// :
// :
int n100;
}
class B extends A
{
B(StyleSheetB ss)
{
super(ss);
// Do some stuff with ingredients of styleSheet
}
}
class StyleSheetB extends StyleSheetA
{
int n101;
int n102;
// :
// :
int n200;
}
Thank you for any help or suggestions, also any of your critics will be appreciated.
Edit: I am developing in java me so there is no generics support.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在我看来,您只是将参数太多的问题从类
A
转移到类StyleSheetA
。为了说明我的观点,请考虑这个问题:您将如何实例化
StyleSheetA
?无论如何,可能使用接受所有这些参数的构造函数。此设计可能给您带来的唯一好处是,如果您有一组由StyleSheetA
对象封装的相同参数值,您将在A
的多个实例中重用该对象。如果是这样,请记住,尽管您有不同的A
实例,但它们会共享相同的参数,因此这不是一个好的选择。我可以建议您尝试重构您的类
A
本身。尝试将其分成更小的班级。如果需要,请尝试创建子类以避免条件分支等。现在,我不知道您的类
A
是什么样子,但也许如果您这样做,您将拥有多个类,每个类都有它自己的一组参数。如果任何参数是鉴别器(意味着它确定类“类型”),您将能够摆脱它,只需使用子类,并依靠内置类型系统来完成它。It seems to me that you are only moving the problem of having too many parameters from class
A
to classStyleSheetA
.To illustrate my point, think of this question: How would you instantiate
StyleSheetA
? Probably using a constructor that accepts all these parameters, anyway. The only benefit this design may give you is if you have a same set of parameter values encapsulated by an object ofStyleSheetA
which you will reuse among multiple instances ofA
. If so, bear in mind that although you'd have different instances ofA
they would share the same parameters, so it isn't a good choice.What I could recommend you is to try to refactor your class
A
itself. Try to break it up into smaller classes. If nesseccary, try to create subclasses to avoid conditional branches, etc.Now, I don't know how your class
A
looks like, but maybe if you do so you'll have several classes, each with its own set of parameters. And if any of the parameters is a discriminator (meaning that it determines the class "type") you will be able to get rid of it, just by using subclasses, and relying on built in type system to do it instead.您是否考虑过使用 IoC 容器(例如 StructureMap)来管理构造函数依赖项?这可能会让很多事情变得更容易。
Have you considered using an IoC container, like StructureMap, to manage your constructor dependencies? That might make a lot of this stuff easier.
关于 getter 和 setter 问题的思考:
“B”中的构造函数意味着附加参数 (n101+) 对于类的操作是必需的。如果您只是使用完整参数列表扩展该类,则
B
中的 n101...n200 和A
中的 n1...n100 会有 getter 和 setter。这表明也许不要让StylesheetB
扩展StylesheetA
,而是让 B 类的构造函数为B(StyleSheetA,StyleSheetB)
,这样您就可以在类A
中为其参数设置一个设置器,继承该设置器,并在B
中为StylesheetB
放置一个设置器。A thoughts on the getter and setter issue:
The constructor in 'B' implies that the additional parameters (n101+) are necessary for the operation of the class. If you were just extending the class with a full parameter list, you would have getters and setters for n101...n200 in
B
and n1...n100 inA
. This suggests perhaps not havingStylesheetB
extendStylesheetA
, but rather have the constructor to class B beB(StyleSheetA,StyleSheetB)
, this way you can have a setter in classA
for it's parameters, have that inherited and also put one inB
forStylesheetB
.