构造函数的设计模式

发布于 2024-10-16 20:06:32 字数 1096 浏览 1 评论 0原文

我遇到了一个设计问题的挑战,我将在下面尝试描述该问题。

假设一个类(称为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小忆控 2024-10-23 20:06:32

在我看来,您只是将参数太多的问题从类 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 class StyleSheetA.

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 of StyleSheetA which you will reuse among multiple instances of A. If so, bear in mind that although you'd have different instances of A 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.

苏别ゝ 2024-10-23 20:06:32

您是否考虑过使用 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.

情释 2024-10-23 20:06:32

关于 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 in A. This suggests perhaps not having StylesheetB extend StylesheetA, but rather have the constructor to class B be B(StyleSheetA,StyleSheetB), this way you can have a setter in class A for it's parameters, have that inherited and also put one in B for StylesheetB.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文