OOP 有什么规则吗?
最近听说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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这些面向对象原则直接来自Head First 设计模式:
These OO principles are straight from Head First Design Patterns:
这些是概念,而不是规则。 实际上没有规则,只是需要做出的决定,有些设计比其他设计更好,有些比其他设计好得多 :-)
不过有很多指导方针 :-) 有些是特定于语言的(C++ 充满了它们)其他则是特定于 OO 的。 太多了,无法列出 :-)
在我的脑海中,重要的是:
充足的扩展和添加:-)
编辑:我应该添加,你列出的规则并不是 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:
Plenty to expand upon and add :-)
EDIT: I should add, the rules which you listed are not unique to OO
根据务实程序员的说法 - 规则是:
http://media.pragprog.com/articles/may_04_oo1.pdf
According to the Pragmatic Programmers - the rules are:
http://media.pragprog.com/articles/may_04_oo1.pdf
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...
不确定任何规则。 所有这些提到的东西对我来说更像是面向对象的范例。 我们遵循的建议很少,例如,
Not sure about any rules. All these mentioned things are more like OO paradigms to me. There are few advices we follow like,
似乎您正在寻找的是面向对象设计的原则。
总结自敏捷软件开发原则、模式和实践。 这些原则是数十年软件工程经验来之不易的产物。 它们不是单一思想的产物,而是代表了大量软件开发人员和研究人员的集成和著作。 尽管它们在这里作为面向对象设计的原则呈现,但它们实际上是长期存在的软件工程原则的特例。
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.