需要类建模方面的帮助
我一直在研究一些面向对象的概念,比如设计模式、干净的代码和其他一些东西,但我仍然对如何继续有些疑问。例如,让我们看一下我的例子。
我有一个 Person 类,它是一个模型。我想为一个人添加一些验证,例如检查年龄是否与出生日期相对应以及检查名称是否包含有效字符。
我有两种方法,但我不知道应该使用哪一种。
方法一: 我创建一个名为:的新类
class ValidatePerson {}
,该类具有方法:“validateAge()”和“validateName()”,并且我需要的每个验证都必须实现一个新方法。
方法二: 我创建一个名为的抽象类: ValidatePerson {} 将为所有验证提供一些通用方法,并且我将拥有:
class ValidatePersonAge extends ValidatePerson { validate();}
class ValidatePersonName extends ValidatePerson {validate();}
我想选择方法二,因为我必须添加的每个新验证规则都将是一个新类,并且不会影响其他类。就像,如果我需要更改新规则所需的通信方法中的某些内容,我可以覆盖它。在第一种方法中,我必须添加另一个方法,然后创建另一个方法或更改已经为其他方法工作的方法,这可能会导致崩溃。
问题是我对这一切感到有点困惑,因为我是编程新手,我希望看到一些关于它的帮助和解释。我还读到,类应该关闭以进行更改,但应开放以进行扩展(或类似的内容)。
I've being studying some OO concepts, like design patterns, clean code and some other stuff and i still have some doubts about how to proceed. For instance, lets take a look at my example.
I have a Person class that is a model. I want to add some validations to a person, like check if the age corresponds with the birth date and check if the name contains valid characters.
I have two approachs, but i dont know wich one i should use.
Approach one:
I create a new class called:
class ValidatePerson {}
and the the class have the methods: "validateAge()" and "validateName()" and every vallidation that i need i will have to implement a new method.
Approach two:
I create a abstract class called:
ValidatePerson {} that will have some commum methods to all validation and the i would have:
class ValidatePersonAge extends ValidatePerson { validate();}
class ValidatePersonName extends ValidatePerson {validate();}
I want to choose for approach two, cause every new validation rule that i will have to add will be a new class and wont affect the others. Like, if i need to alter something in the commum method that the new rule needs, i could just overwrite it. On the first approach, i would have to add another method and then create another method or alter the one thats already working for the others, that could make then crash.
The thing is that im kind of confused to all this, since im new to programming and i would like to see some help and explanation about it. I've also read that classes should be closed for changing but open for expanding (or something like this).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没有具体的正确答案。设计应该始终处于您的问题领域和业务环境的背景下。所以这里有多种选择
选项1
Person 类有一个 vailidate() 方法,您可以调用该方法来对其当前状态执行所有验证。
优点
缺点
选项2
每个属性在 Person 类中都有自己的 validateXXX() 方法。每个 setXXX() 方法都会调用相应的 validateXXX() 方法。
优点
缺点
选项 3
您可以拥有一个包含这些验证检查的 PersonBuilder。构建器将在构建 Person 对象之前执行这些验证。这样,一旦构建了 Person 对象,它就满足所有验证和不变量。
优点
缺点
您的选项 2 不正确因为 ValidatePersonAge 与 ValidatePerson 不同。你并没有完全验证这个人,而只是验证他的年龄。所以它们在语义上是不同的。
There is no specific correct answer. The design should always be in context of your problem domain and business context. So here are various options
Option 1
Person class has a vailidate() method that you can call to perform all he validations on its current state.
Pros
Cons
Option 2
Each property has its own validateXXX() method in the Person class. Each setXXX() method is going to call corresponding validateXXX() method.
Pros
Cons
Option 3
You could have a PersonBuilder that contains these validation checks. The builder will perfrom these validations before it builds the Person object. This way once the Person object is built, it satisfies all the validations and invariants.
Pros
Cons
Your option 2 is not correct because ValidatePersonAge IS NOT same as ValidatePerson. You are not validating the person entirely but only validating his age. So they are semantically different.