在进行班级设计时,您通常遵循哪些原则?
12 回答
面向对象类设计的原则(“SOLID”原则)
- SRP:单一职责原则一个类应该有一个并且只有一个改变的理由。
- OCP:开放封闭原则您应该能够扩展类行为,而无需修改它。
- LSP:Liskov 替换原则派生类必须可以替换它们的基类。
- ISP:接口隔离原则制作特定于客户端的细粒度接口。
- DIP:依赖倒置原则 依赖于抽象,而不是具体。
资料来源:http ://butunclebob.com/ArticleS.UncleBob.PrinciplesOfOod
视频(鲍勃叔叔):Robert C. Martin 的清洁编码(鲍勃叔叔)
不要忘记得墨忒耳法则。
SOLID原则。
或者至少我尽量不远离他们。
The most fundamental design pattern should be KISS (keep it simple stupid) Which means that sometimes not using classes for some elements at all it the right solution.
That and CRC(Class, Responsibility, Collaborators) cards (write the card down in your header files, not on actual cards that way they because easy to understand documentation too)
如上所述,一些基本的面向对象设计原则是 OCP、LSP、DIP 和 ISP。
Robert C. Martin (of Object Mentor) 对这些内容进行了出色的概述:OOD 原则和模式
“资源获取即初始化”范式很方便,尤其是在用 C++ 编写和处理操作系统资源(文件句柄、端口等)时。
这种方法的一个关键好处是对象一旦创建,就是“完整的”——不需要两阶段初始化,也不可能部分初始化对象。
松耦合,高内聚。
组合优于继承。
领域驱动设计通常是一个很好的原则。
基本上我摆脱了对接口的编程。我尝试将通过案例更改的内容封装起来,以避免代码重复并将代码隔离为可管理的(对于我的大脑而言)块。稍后,如果需要,我可以很容易地重构代码。
SOLID 原则和 Liskov 模式,以及单一责任模式。
A thing which I would like to add to all this is layering, Define layers in your application, the overall responsibility of a layer, they way two layers will interact. Only classes which have the same responsibility as that of the layer should be allowed in that layer. Doing this resolves a lot of chaos, ensures exceptions are handled appropriately, and it makes sure that new developers know where to place their code.
另一种设计方法是将您的类设计为可配置的,创建一种可以将配置插入到您的类中的机制,而不是覆盖子类中的方法,确定哪些更改,查看是否可以配置并确保此功能是从配置派生
我通常会尝试将类融入其中一种 oo设计模式。