面向对象编程 - 最佳实践?
您会选择以下哪一项? 基于面向对象的编程哪一种是最佳实践?
A
Class Note
{
//Some properties, etc
public static Note getNoteFromServer();
public void UpdateNoteOnServer();
}
B
Class Note
{
//Some properties, etc
}
Class NoteManager
{
public static Note getNoteFromServer();
public static UpdateNoteOnServer(Note);
}
Which of the following would you go with?
And based on object oriented programming which one is the best practice?
A
Class Note
{
//Some properties, etc
public static Note getNoteFromServer();
public void UpdateNoteOnServer();
}
B
Class Note
{
//Some properties, etc
}
Class NoteManager
{
public static Note getNoteFromServer();
public static UpdateNoteOnServer(Note);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
我会说选项 B。通过这种方式,您可以分离关注点:您有一个可以在任何地方重用(不一定在网络应用程序上)的
Note
,并且您有一个只关心服务器的管理器类沟通。您还可以考虑多个服务器的实现逻辑。例如,您可能想要与 JSON 或 XML 等数据格式进行通信。您可以实现一个接口(例如,接口
INoteManager
),然后为我提到的每种数据类型实现两个带有服务器的类(例如,NoteManagerXml
和NoteManagerJson< /代码>)。
这个问题的要点是关注点分离。希望我有所帮助! :)
I would say option B. In that way you separate concerns: you have a
Note
that can be reused anywhere (and not necessarily on a networked application), and you have a manager class that only cares with server communication.You may also think on implement logic for multiple servers. For example, you may want to comunicate with data formats like JSON or XML. You may implement an interface (example, interface
INoteManager
) and then implement two classes with servers for each of the data types I mentioned (example,NoteManagerXml
andNoteManagerJson
).The main point on this question is sepration of concerns. Hope I've helped! :)
为了与我的其他答案采取不同的观点,我建议您将
Note
/NoteManager
划分为错误的 - 不是因为Note
它有什么问题,但是因为术语Manager有点代码味道,因为它非常通用,导致将类用作一般的垃圾场。如果该类负责笔记持久化,请将其命名为
NoteRepository
。如果它负责验证单个注释的内容,请将逻辑移至
注释
。如果它负责创建笔记,并提供许多方便的方法来轻松创建笔记,请将其称为
NoteFactory
。如果它负责上述所有事情,请将其分成单独的部分,因为它做得太多了。
To take a different viewpoint from my other answer, I'd suggest that your division into
Note
/NoteManager
is the wrong one - not becauseNote
has anything wrong with it, but because the term Manager is a bit of a code smell because it's very generic, inviting the use of the class as a general dumping ground.If the class is responsible for note persistence, call it
NoteRepository
.If it's responsible for validating the content of a single note, move the logic onto the
Note
.If it's responsible for creating notes, providing a number of convenience methods for easily creating notes, call it
NoteFactory
.And if it's responsible for all of the above, split it into separate pieces because it's doing too much.
你在那里问的问题是一个基于意见的问题。
您本质上是在问(如果我理解正确的话)是否最好使用一个仅包含属性的类和另一个类来管理该对象(示例 B),或者使用一个执行所有操作的类(示例 A)。
这确实取决于。如果我们计划使用 MVC 类型的框架,示例 B 会更适合,其中 Note 是模型,NoteManager 是控制器。
就我个人而言,我会选择 A 和 B 的混合,其中 NoteManager 处理控制器操作,但模型仍然有自己的方法来执行诸如管理单例实例之类的操作。那么也许是这样的?
That's a pretty opinion based question you're asking there.
You're essentially asking (if I understand correctly) whether it is better to have a Class which contains only properties and another class to manage that object (Example B) or to have a class which does everything (Example A).
It really depends. If we're planning on using a MVC kind of framework, Example B would fit better, with Note being your Model, and NoteManager being the controller.
Personally, I would go with a hybrid of A and B, where NoteManager is handling controller actions, but the Model still has methods of its own to do things like managing a singleton instance. So maybe something like this?
我认为 A 更好,原因如下:
我在 B 中看到的问题是,接收同一个类的实例的静态方法对我来说听起来多余,因为为什么要使用静态方法将行为应用于同一个类的实例?类和实例背后的整个想法是,类是框架和实例cookie,如果您需要不同的cookie,请修改您的框架并获取新的。
I think A is better, for 1 reason:
The problem i see with B is that a static method that receives an instance of the same class sounds redundant to me because, why would you use a static method to apply behaviour to an instance of the same class? The whole idea behind classes and instances is that Classes are the frame and instances cookies, if you need different cookies, modify your frame and get new ones.
这似乎取决于它在您的程序中的使用方式。如果 Note 是唯一的类或者是派生类的父类,那么拥有“管理者”、保持简单愚蠢(KISS)就没有意义。但是,如果管理器必须通过接口处理其他类,那么我可以看到有一个单独的类。
It seems to depend on how its going to be used in your program. If Note is the only class or is the parent class for derived classes then there is no point and having a "Manager", Keep It Simple Stupid (KISS). However if the Manager has to deal with other classes via Interfaces then I can see having a seperate class.
根据我的经验,最佳实践是,只要将事物分开 DRY 就是最佳实践。你可以将注释扩展到notemanager
As per my experience best practice is , as long as things are separated DRY is best practice. you can extends note to notemanager
我会选择 B,除非你想像可怜的 PHP 那样结束:
但说真的,目前做 A 似乎很直观,但你会最终将 A 分开,因为这些服务器操作将需要越来越多的相关函数,直到您拥有一个庞大的
Note
类,其中包含程序中的每个函数......I'd choose B, unless you want to end up like poor ol' PHP:
But seriously, it may seem intuitive to do A at the moment, but you'll eventually split A up, as those server operations will require more and more related functions, until you have a mammoth
Note
class which contains every function in your program...“这取决于”
它取决于的事情之一是实现的语言。
如果您使用 C# 或 Java,那么您可能希望使用
Note
/NoteManager
方法,因为这为您提供了最大的实现灵活性 - 因为static 成员是二等公民。
为了说明这一点,在 Delphi 的原始 Object Pascal 语言中,无需实例即可访问的方法和属性被称为
class
成员,而不是static
成员,并且它们可以是虚拟
,因此在后代类中被覆盖。如果您使用的语言提供“虚拟类(静态)成员”等功能以及其他一些功能,那么您可能需要将
Note/NoteManager
合并在一起。"It Depends"
One of the things it depends upon is the language of implementation.
If you are working in C# or Java, then you'll likely want to go with the
Note
/NoteManager
approach as this gives you the most flexiblity of implementation - becausestatic
members in those languages a kind of second class citizens.To illustrate, in Delphi's original Object Pascal lanaguage, methods and properties that could be accessed without an instance were known as
class
members, notstatic
members, and they could bevirtual
, and therefore overridden in descendent classes.If you're working with a language that provides features like "virtual class (static) members" and a few others, then you might want to merge
Note/NoteManager
together.我会选择“B”
原因是您可能需要将“Note”与另一种类型的控制器类一起使用,就像您对 NoteManager 所做的那样。
还使您能够将数据对象、DTO 或模型与实际控制器类分离。
I would go with "B"
Reason why is that you may require "Note" to be used with another type of Controller class, like what you have done for NoteManager.
Also gives you the ability to dissociate your Data Objects or DTO's or Model away from your actual controller classes.
C
C