gpt4 book ai didi

java - 具有 hibernate 参数的类,有什么清理建议吗?

转载 作者:行者123 更新时间:2023-11-30 04:10:40 24 4
gpt4 key购买 nike

很多时候构造函数的一些参数是 optional当对象创建时。它们稍后使用 setter 进行设置(例如:在延迟初始化期间)。

我的问题是,在某些情况下,参数根本无法通过构造函数或 setter 设置。为了这个问题,我将这样的实例变量称为 - dormant 。假设一个标准的披萨示例,其中 Pizza(cheese, chicken, toppings). 。顾客可能不想要浇头并且可能会留下它dormant.

这被认为是不好的做法吗?如果是,那么是否有任何已知的模式可以使用dormant来清理此类类。实例变量?

最佳答案

我认为Decorator Pattern是您所需要的:

The decorator pattern can be used to extend (decorate) the functionality of a certain object statically, or in some cases at run-time, independently of other instances of the same class.

在此处查看 .NET 示例:

Please help me understand the "Decorator Pattern" with a real world example.

在这里您可以找到一个 Java 示例:

Gang of Four – Decorate with Decorator Design Pattern

关于java - 具有 hibernate 参数的类,有什么清理建议吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19672466/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com