面向对象编程 - 最佳实践? [英] Object Oriented Programming - Best Practice?

查看:58
本文介绍了面向对象编程 - 最佳实践?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您会选择以下哪项?以及基于面向对象编程哪一种是最佳实践?

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);
}

推荐答案

我会说选项 B.这样你就可以分离关注点:你有一个 Note 可以在任何地方重复使用(不一定在网络应用程序上),并且您有一个只关心服务器通信的管理器类.

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.

您也可以考虑为多个服务器实现逻辑.例如,您可能希望与 JSON 或 XML 等数据格式进行通信.您可以实现一个接口(例如,接口 INoteManager),然后为我提到的每种数据类型(例如,NoteManagerXmlNoteManagerJson).

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 and NoteManagerJson).

这个问题的要点是关注点的分离.希望我有所帮助!:)

The main point on this question is sepration of concerns. Hope I've helped! :)

这篇关于面向对象编程 - 最佳实践?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆