OOP 有什么规则吗?

发布于 2024-07-11 12:57:03 字数 61 浏览 9 评论 0原文

最近听说OOP(Java)有9条规则。 我只知道四种:抽象、多态、继承和封装。 OOP 还有更多规则吗?

Recently I heard that there are 9 rules for OOP(Java). I know only four as Abstraction, Polymorphism, Inheritance and Encapsulation. Are there any more rules for OOP?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

流心雨 2024-07-18 12:57:09

这些面向对象原则直接来自Head First 设计模式

  • 将不同的
  • 程序封装为接口,而不是实现
  • 偏向于继承的组合
  • 一个类应该只有一个改变的理由(单一职责原则
  • 子类型必须可以替换它们的基础(里氏替换原则) strong>)
  • 类应该对扩展开放,但对修改封闭(开闭原则

These OO principles are straight from Head First Design Patterns:

  • Encapsulate what Varies
  • Program to an Interface, rather than an Implementation
  • Favour Composition over Inheritance
  • A Class should have only one reason to Change (Single Responsibility Principle)
  • Sub-Types must be substitutable for their Base (Liskov Substitition Principle)
  • Classes shoule be Open for extension, but Closed for Modification (Open-Closed Principle)
時窥 2024-07-18 12:57:09

这些是概念,而不是规则。 实际上没有规则,只是需要做出的决定,有些设计比其他设计更好,有些比其他设计好得多 :-)

不过有很多指导方针 :-) 有些是特定于语言的(C++ 充满了它们)其他则是特定于 OO 的。 太多了,无法列出 :-)

在我的脑海中,重要的是:

  • 松散耦合,高内聚
  • 编写可测试的类,您可以测试它们
  • 谨慎使用继承,并且仅在有意义的地方使用继承(更喜欢组合)
  • 尝试坚持开放/封闭原则。
  • (最重要的)KISS

充足的扩展和添加:-)

编辑:我应该添加,你列出的规则并不是 OO 所独有的

These are concepts, not rules. There are no rules really, just decisions to make, some designs are better than others, some much better than others :-)

There are plenty of guidelines though :-) Some are language specific (C++ is riddled with them) others are OO specific. Too many to list though :-)

Off the top of my head, important ones are:

  • Loose coupling, high cohesion
  • Write testable classes, which you test
  • Use inheritence sparingly and only where it makes sense (prefer composition)
  • Try stick to the open/close principle.
  • (most important) KISS

Plenty to expand upon and add :-)

EDIT: I should add, the rules which you listed are not unique to OO

习ぎ惯性依靠 2024-07-18 12:57:09

根据务实程序员的说法 - 规则是:

  • 保持干燥(不要重复自己)
  • 保持害羞(确保你的类具有高内聚性和低耦合性)
  • 并告诉其他人(关注点分离)

http://media.pragprog.com/articles/may_04_oo1.pdf

According to the Pragmatic Programmers - the rules are:

  • Keep it DRY (Don't Repeat Yourself)
  • Keep it SHY (Ensure that your classes have high cohesion and low coupling)
  • and tell the other GUY (Separation of concerns)

http://media.pragprog.com/articles/may_04_oo1.pdf

女皇必胜 2024-07-18 12:57:09

OOP 没有“规则”。

有 4 个语言属性决定语言是否面向对象(这些是您在问题中列出的内容)。

其余材料都有指导方针。 我读过的最好/最有帮助的指南是 GRASP

许多建议外行人(非 CS 专业)不容易理解。 我认为 GRASP 务实且平易近人。

我认为 GRASP 很好,因为它的名字暗示了 OO 最关键的部分 - 责任分配(分配给对象而不是程序员)。

其他所有内容都源自的两个最关键的 GRASP 概念是耦合和内聚。 这两个概念/原则驱动所有其他模式和方法。

顺便说一句 - 我刚刚采访过你吗? 您错误地抄写了问题...

There are no "Rules" to OOP.

There are 4 language properties that make a language object-oriented or not (these are the things you listed in your question).

The rest of the material out there are guidelines. The best/most helpful guidelines I've read are GRASP

Many of the suggestions are not readily understandable by laymen (non-CS majors). I thought GRASP was pragmatic and approachable.

I think GRASP is nice because it suggests the most critical part of OO in its name - Assignment of Responsibility (to objects not programmers).

The two most critical GRASP concepts from which everything else derives are coupling and cohesion. These two concepts/principals drive all other patterns and approaches.

BTW - did I just interview you? You transcribed the question incorrectly...

青萝楚歌 2024-07-18 12:57:08

不确定任何规则。 所有这些提到的东西对我来说更像是面向对象的范例。 我们遵循的建议很少,例如,

  • 关注点分离、
  • 每个类单一职责、
  • 优先选择组合而不是继承
  • 编程、接口
  • 加上所有这些都由 Billybob 提到过。

Not sure about any rules. All these mentioned things are more like OO paradigms to me. There are few advices we follow like,

  • Separation of Concern
  • Single Responsibility per Class
  • Prefer Composition over Inheritance
  • Programming to Interface
  • Plus all mentioned by Billybob, already
浅听莫相离 2024-07-18 12:57:06

似乎您正在寻找的是面向对象设计的原则

总结自敏捷软件开发原则、模式和实践。 这些原则是数十年软件工程经验来之不易的产物。 它们不是单一思想的产物,而是代表了大量软件开发人员和研究人员的集成和著作。 尽管它们在这里作为面向对象设计的原则呈现,但它们实际上是长期存在的软件工程原则的特例。

SRP 单一职责原则 一个类应该只有一个改变的理由。

OCP 开放-封闭原则 软件实体(类、包、方法等)应该对扩展开放,但对修改封闭。

LSP 里氏替换原则 子类型必须可以替换其基本类型。

DIP 依赖​​倒置原则 抽象不应该依赖于细节。 细节应该取决于抽象。

ISP接口隔离原则
客户不应被迫依赖他们不使用的方法。 接口属于客户端,而不属于层次结构。

REP 发布-重用等效原则
再利用的颗粒就是释放的颗粒。

CCP共同封闭原则
包中的类应该针对相同类型的更改关闭在一起。 影响已关闭包的更改会影响该包中的所有类,但不会影响其他包。

CRP通用重用原则
包中的类可以一起重用。 如果重用包中的其中一个类,则可以重用所有类。

ADP 无环依赖性原则
依赖图中不允许有循环。

SDP 稳定依赖原则
取决于稳定的方向。

SAP 稳定抽象原则
包应该既抽象又稳定。

Seems like what you're looking for are the Principles of Object-Oriented Design.

Summarized from Agile Software Development Principles, Patterns, and Practices. These principles are the hard-won product of decades of experience in software engineering. They are not the product of a single mind, but they represent the integration and writings of a large number of software developers and researchers. Although they are presented here as principles of object-oriented design, they are really special cases of long-standing principles of software engineering.

SRP The Single Responsibility Principle A class should have only one reason to change.

OCP The Open-Closed Principle Software entities (classes, packages, methods, etc.) should be open for extension, but closed for modification.

LSP The Liskov Substition Principle Subtypes must be substitutable for their base types.

DIP The Dependency Inversion Principle Abstractions should not depend upon details. Details should depend upons abstractions.

ISP The Interface Segregation Principle
Clients shold not be forced to depend upon methods that they do not use. Interfaces belong to clients, not to hierarchies.

REP The Release-Reuse Equivalency Principle
The granule of reuse is the granule of release.

CCP The Common Closure Principle
The classes in a package should be closed together against the same kinds of changes. A change that affects a closed package affects all the classes in that package and no other packages.

CRP The Common Reuse Principle
The classes in a package are reused together. If you reuse one of the classes in a package, you reuse them all.

ADP The Acylcic Dependencies Principle
Allow no cycles in the dependency graph.

SDP The Stable Dependencies Principle
Depend in the direction of stability.

SAP The Stable Abstractions Principle
A package should be as abstract as it is stable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文