当前位置:网站首页>程序设计原则
程序设计原则
2022-06-13 09:38:00 【吴迪98】
设计原则是提高代码可维护性,帮助建立灵活、稳定的系统的基石。主要包括通用设计原则和S.O.L.I.D原则
通用设计原则是要求我们在任何开发中都要坚持和遵守的基础原则。
S.O.L.I.D原则更适用于支持面向对象的高级语言,有些原则可能不适用于目前的前端开发,但是了解它的思想对我们也是有益的。
通用设计原则 (Common Design Principles)
Keep It Simple and Stupid (KISS)
● 代码要易读易维护。
● 不要过度设计一个方案,满足需求的最简洁的方案即是最好的方案。
Don’t Repeat Yourself (DRY)
不要重复,重复的逻辑应该抽离以便复用,重复的操作应该工具化工程化。
Tell, Don’t Ask
也叫“好莱坞原则”,这个原则是关于封装和职责分配的。当跟别的模块交互时,应该是直接告诉那个模块你想要的操作而不是通过判断那个模块的状态,再调用操作。
You Ain’t Gonna Need It (YAGNI)
只包含应用需要的功能,不要加一些自以为需要的功能。
TDD是一种遵守YAGNI的开发模式。
Separation of Concerns (SoC)
将功能分离成独立的功能模块,这些模块有自己独立的行为和数据,以增进代码重用性、可维护性和可测试性。
S.O.L.I.D设计原则
单一职责原则 (Single Responsibility Principle - SRP)
这条跟SoC相近,它规定每个对象应该只关注一个功能点、只有一个理由改变,避免瑞士军刀式的对象。
开放封闭原则 (Open-Closed Principle - OCP)
对扩展开放,对修改封闭。
对扩展开放,意味着有新的需求或者变化时,可以对现有的代码进行扩展,以适应新的情况。对修改封闭,意味着一旦设计完成,就可以独立完成其工作,添加需求也不用修改其内部实现。
里氏替换原则 (Liskov Substitution Principle - LSP)
任何子类都可以不需要任何修改替代父类来使用。
接口分离原则 (Interface Segregation Principle - ISP)
将相似的接口方法分组抽离形成新的接口,避免去实现一个巨大的接口。
依赖倒置原则 (Dependecy Inversion Principle - DIP)
程序要依赖于抽象接口,不要依赖于具体实现,简单讲就是面向接口、面向抽象编程。
边栏推荐
- LeetCode 202. Happy number
- [pytorch environment installation]
- LeetCode 2016. Maximum difference between incremental elements
- Mobile web effects
- (dp+ memory) acwing 901 skiing
- WIN7无法被远程桌面问题
- ThingsBoard教程(二十):使用规则链过滤遥测数据
- [51nod p3058] Xiao ming'ai set [set]
- Collection of articles on virtualization and cloud computing
- 拜登:希望尽快签署两党枪支安全改革法案
猜你喜欢

Standard template library (STL)

一篇文章读懂:Spark运行模式

【20220526】UE5.0.2 release d11782b
![[51nod 3062] n queen problem V2 [bit operation DFS]](/img/ca/4e3b01c371557d8214da6a97eba3ac.jpg)
[51nod 3062] n queen problem V2 [bit operation DFS]

【pytorch环境安装搭建】

Consolas-with-Yahei

Exercise 7-7 string replacement (15 points)

简述请求过程

(topological sorting +bfs) acwing 848 Topological sequence of digraph

Trees and binary trees: traversal of binary trees
随机推荐
matlab轮毂电机分析模糊pid控制垂向振动分析
Execution order of subclass and parent constructor
GCC compilation process
[ssl1271] sort I [heap]
Interrupt handling mechanism
Exploitation of competitive loopholes in attacking and defending world PWN play conditions
[51nod p3216] Award [bit operation]
Tree and binary tree: basic operation and implementation of binary tree
删除软链接
SpEL表达式 简单使用
【 ssl2 ⁶⁶⁶⁶⁶⁶⁶⁶⁶⁶⁶⁶⁶⁶⁶⁶⁶⁶⁶83
Protocol UART of schematic diagram
Memory management -- Viewing memory space from the perspective of executing programs and processes
[51nod 3062] n queen problem V2 [bit operation DFS]
Summary of random number learning
Musk's "meta universe" dream
A hot MySQL training topic, making you a master of SQL
苹果放大招!这件事干的太漂亮了……
Tree and binary tree: application of binary tree traversal
Exercise 7-10 finding specified characters (15 points)