首页 > 编程语言 >Python - SOLID Principles

Python - SOLID Principles

时间:2024-08-16 09:17:10浏览次数:14  
标签:level Python interfaces Principles should SOLID principle LSP new

• Single responsibility principle (SRP)

• Open-closed principle (OCP)

It emphasizes that software entities, such as classes and modules, should be open for extension but closed for modification. What does that mean? It means that once a software entity is defined and implemented, it should not be changed to add new functionality. Instead, the entity should be extended through inheritance or interfaces to accommodate new requirements and behaviors.

• Liskov substitution principle (LSP)

It dictates how subclasses should relate to their superclasses. According to the LSP, if a program uses objects of a superclass, then the substitution of these objects with objects of a subclass should not change the correctness and expected behavior of the program.

Following this principle is important for maintaining the robustness of a software system. It ensures that, when using inheritance, subclasses extend their parent classes without altering their external behavior. For example, if a function works correctly with an object of a superclass, it should also work correctly with objects of any subclass of this superclass.

The LSP allows developers to introduce new subclass types without the risk of breaking existing functionality. This is particularly important in large-scale systems where changes in one part can have effects on other parts of the system. By following the LSP, developers can safely modify and extend classes, knowing that their new subclasses will integrate seamlessly with the established hierarchy and functionality.

• Interface segregation principle (ISP)

The ISP advocates for designing smaller, more specific interfaces rather than broad, general-purpose ones. This principle states that a class should not be forced to implement interfaces it does not use. In the context of Python, this implies that a class shouldn’t be forced to inherit and implement methods that are irrelevant to its purpose.

• Dependency inversion principle (DIP)

The DIP advocates that high-level modules should not depend directly on low-level modules. Instead, both should depend on abstractions or interfaces. By doing so, you decouple the high-level components from the details of the low-level components.

 

For detailed examples, see https://www.cnblogs.com/zhangzhihui/articles/18014615 .

标签:level,Python,interfaces,Principles,should,SOLID,principle,LSP,new
From: https://www.cnblogs.com/zhangzhihui/p/18362259

相关文章

  • python 利用高德得到地址对应的经伟度,由于地址原因在指定时间范围内得不到经伟度而终
    importrequests,sysimportjson,math,xlrd,xlwt,time#!/usr/bin/envpython#-*-coding:utf-8-*-frommathimportsin,asin,cos,radians,fabs,sqrtimportpandasaspdfromgeopy.distanceimportgeodesicfromopenpyxlimportload_workbookimportred......
  • 计算机毕业设计必看必学!! 93494基于springboot 书店信息管理系统,原创定制程序, java
    摘 要书店信息管理系统采用B/S结构、java开发语言、以及Mysql数据库等技术。系统主要分为管理员和用户两部分,管理员管理主要功能包括:首页、轮播图、公告栏、资源管理(图书资讯、资讯分类)交流管理(留言板、留言板分类)系统用户(管理员、顾客用户)模块管理(图书信息、分类管理、购......
  • 华为OD笔试机试 - 攀登者2 (python/c++/java 2024年C卷D卷真题算法)
    华为OD机试(C卷+D卷)2024真题目录(Java&c++&python)题目描述攀登者喜欢寻找各种地图,并且尝试攀登到最高的山峰。地图表示为一维数组,数组的索引代表水平位置,数组的元素代表相对海拔高度。其中数组元素0代表地面。例如:[0,1,2,4,3,1,0,0,1,2,3,1,2,1,0],代表如下图所示的......
  • 【视频讲解】Python用LSTM长短期记忆网络GARCH对SPX指数金融时间序列波动率滚动预测
    全文链接:https://tecdat.cn/?p=37371 原文出处:拓端数据部落公众号本文融合了多种技术,其中LSTM(长短期记忆网络)和GARCH(广义自回归条件异方差)模型尤为关键。LSTM在处理时间序列数据方面独具优势,能够捕捉长期依赖关系,为金融预测提供强大支持。GARCH模型则能有效捕捉金融时间序......
  • Python 查看当前环境所有变量的大小
    https://blog.csdn.net/lly1122334/article/details/104757467defshow_memory(unit='KB',threshold=1):'''查看变量占用内存情况:paramunit:显示的单位,可为`B`,`KB`,`MB`,`GB`:paramthreshold:仅显示内存数值大于等于threshold的变量''�......
  • python-flask计算机毕业设计助学贷款信息管理系统(程序+开题+论文)
    本系统(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。系统程序文件列表开题报告内容研究背景随着高等教育的普及与深化,越来越多的学生选择通过助学贷款来完成学业,以减轻家庭经济负担。然而,传统的助学贷款管理方式往往存在信息更新滞......
  • python-flask计算机毕业设计养老服务系统的设计与实现(程序+开题+论文)
    本系统(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。系统程序文件列表开题报告内容研究背景随着全球人口老龄化的加速发展,养老服务已成为社会关注的焦点。传统家庭养老模式正面临巨大挑战,如家庭结构小型化、子女工作繁忙导致照顾能......
  • python-flask计算机毕业设计校园二手交易平台系统(程序+开题+论文)
    本系统(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。系统程序文件列表开题报告内容研究背景随着高等教育的普及和校园生活的日益丰富,学生群体对于物品交换与再利用的需求日益增长。传统的校园二手交易往往依赖于线下张贴广告、口口......
  • 编程基础题:开关灯(C语言方式代码,C++方式代码,Python3方式编写)三种语言编写代码
    1.题目描述:假设有N蓋灯(N为不大于5000的正整数),从1到N按顺序依次编号,初始时全部处于开启状态;第一个人(1号)将灯全部关闭,第二个人(2号)将编号为2的倍数的灯打开,第三个人(3号)将编号为3的倍数的灯做相反处理(即,将打开的灯关闭,将关闭的灯打开)。依照编号递增顺序,以......
  • Python编程 - 基础知识
    前言一、常量与变量二、数据类型 三、标识符与关键字四、输出五、输入六、运算符总结前言本文正式开始介绍Python相关知识,主要有常量和变量、数据类型等,接下来咱们往下看一、常量与变量常量通俗的说就是固定不变的值,而变量就是可以变化的值,是一个用于存储......