search
HomeBackend DevelopmentPython Tutorial在面向对象编程时对于类的划分有哪些心得?

有时候一些东西可以写成一个类也可以多个类,如何兼顾各种约束,找到一个合适的平衡点?
例如Python自有的cgi.escape和HTMLParser.HTMLParser,混入帕斯卡命名法不说,将这两个功能这样封装是出于何种考虑?

回复内容:

以容易写unittest又绝对不会破坏封装为准

首先是方法学:
  • 基于用例分析(Use case Analysis)的方法
  • 基于CRC方法(Class Responsibility Collaborator

其次了解面向对象的原则
  • 抽象原则
  • 遵循高内聚,低耦合的原则
  • 遵循SOLID 原则特别是开闭原则(Open/closed principle)和单一责任原则(Single responsibility principle)
最后,持续进行重构 经验之谈。

前期的需求分析的时候,把需求、功能划分细致一点。然后,把要做的功能分几堆。某一堆功能专注做某一类事情,比如数据库读,数据库写,接口交互,数据计算等。
你看,天然的不就出来“类”这个东西了么。

设计的时候,重点注意两个方面。
第一,接口必须隔离。SOLID里提到了单一职责、接口隔离两个原则。单一职责确实很难,有时候打破它的性价比更高。但接口隔离一定要做到。接口不隔离,就不可能单一职责,就很难遵守开闭、里氏等原则。
第二,考虑开闭原则。开闭原则是一切软件设计的核心思想。但是要严格做到真的太难了。首先没人能在刚开始的时候就考虑到所有以后可能的变化;其次即使考虑到了,实现起来的成本也会居高不下。所以,在可以接受的工作量范围内,以你和业务、需求方的经验来做一些开闭的设计就行。

开发的时候,主要靠经验和技巧来做一些重构。
这里可以提一点,就是有些看起来很无聊的代码规范有时候真的很有帮助。比如一个方法不能超过30行,一个类不能超过300行这种。当行数超过规范的时候,你自然就会想重构、拆分子类。

另外 @vczh 说到了以unittest为准,这也是一个很好的思路,尤其是在tdd实践中。 如果针对实体的话,按照面向对象对类的定义,一组相同特性的对象可以归一个类就算一个类,如果多个类共用一组属性的,将共用属性组归为一个继承类,然后具体类继承为子类再实现差异。

如果能全部归一类又可以分多类,应该没处理好公共属性。 这个问题必须要分开讨论。

对于需求变化不频繁的,当然是以test是否方便为依据。

至于需求变化频繁的,相信我,只要能跑就行。

所以我从来都是尽可能不干业务逻辑层的活,我向来是努力写核心功能。举例来说,对于典型的后端mvc的项目,我一般主要写m,顶多写一点m和c之间的接口。但是c和v的主要部分我一点都不想动。这个时候,我写完我的接口就可以好好以test为依据写类了。 纯靠感觉... This is why we need design patterns. You may want to refer to GoF("Design Patterns: Elements of Reusable Object-Oriented Software").
=============================
好吧我没啥写大型项目的经验,还是看以上各位答主答的吧。 SOLID(单一功能、开闭原则、里氏替换、接口隔离以及依赖反转) 根据业务吧,
如果从面相对象的思路去思考,
无非是把概念进行封装,
概念可以分为物件,
行为和状态,
当然,
还有一些是自己认为它是一个完整的概念,
也可以划分出去,
单独描绘,
有点像画画。 类要封装变化。把容易根据需求变化的地方封装起来,这也对应设计模式的开闭原则(对派生开放,对修改封闭)
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Python vs. C  : Understanding the Key DifferencesPython vs. C : Understanding the Key DifferencesApr 21, 2025 am 12:18 AM

Python and C each have their own advantages, and the choice should be based on project requirements. 1) Python is suitable for rapid development and data processing due to its concise syntax and dynamic typing. 2)C is suitable for high performance and system programming due to its static typing and manual memory management.

Python vs. C  : Which Language to Choose for Your Project?Python vs. C : Which Language to Choose for Your Project?Apr 21, 2025 am 12:17 AM

Choosing Python or C depends on project requirements: 1) If you need rapid development, data processing and prototype design, choose Python; 2) If you need high performance, low latency and close hardware control, choose C.

Reaching Your Python Goals: The Power of 2 Hours DailyReaching Your Python Goals: The Power of 2 Hours DailyApr 20, 2025 am 12:21 AM

By investing 2 hours of Python learning every day, you can effectively improve your programming skills. 1. Learn new knowledge: read documents or watch tutorials. 2. Practice: Write code and complete exercises. 3. Review: Consolidate the content you have learned. 4. Project practice: Apply what you have learned in actual projects. Such a structured learning plan can help you systematically master Python and achieve career goals.

Maximizing 2 Hours: Effective Python Learning StrategiesMaximizing 2 Hours: Effective Python Learning StrategiesApr 20, 2025 am 12:20 AM

Methods to learn Python efficiently within two hours include: 1. Review the basic knowledge and ensure that you are familiar with Python installation and basic syntax; 2. Understand the core concepts of Python, such as variables, lists, functions, etc.; 3. Master basic and advanced usage by using examples; 4. Learn common errors and debugging techniques; 5. Apply performance optimization and best practices, such as using list comprehensions and following the PEP8 style guide.

Choosing Between Python and C  : The Right Language for YouChoosing Between Python and C : The Right Language for YouApr 20, 2025 am 12:20 AM

Python is suitable for beginners and data science, and C is suitable for system programming and game development. 1. Python is simple and easy to use, suitable for data science and web development. 2.C provides high performance and control, suitable for game development and system programming. The choice should be based on project needs and personal interests.

Python vs. C  : A Comparative Analysis of Programming LanguagesPython vs. C : A Comparative Analysis of Programming LanguagesApr 20, 2025 am 12:14 AM

Python is more suitable for data science and rapid development, while C is more suitable for high performance and system programming. 1. Python syntax is concise and easy to learn, suitable for data processing and scientific computing. 2.C has complex syntax but excellent performance and is often used in game development and system programming.

2 Hours a Day: The Potential of Python Learning2 Hours a Day: The Potential of Python LearningApr 20, 2025 am 12:14 AM

It is feasible to invest two hours a day to learn Python. 1. Learn new knowledge: Learn new concepts in one hour, such as lists and dictionaries. 2. Practice and exercises: Use one hour to perform programming exercises, such as writing small programs. Through reasonable planning and perseverance, you can master the core concepts of Python in a short time.

Python vs. C  : Learning Curves and Ease of UsePython vs. C : Learning Curves and Ease of UseApr 19, 2025 am 12:20 AM

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),