首页 > 其他分享 >SOLID原则

SOLID原则

时间:2024-12-23 14:59:10浏览次数:3  
标签:code 原则 SOLID should method Principle Vehicle class

This Principle is the foundation for Design patterns in Software System. For acheiving flexibility and making code modular it is highly recommended that we abide by these principles.

Each letter in the word has different meaning to it which is as follows:

S → Single Responsibility Principle

Classes and methods in the class should have single responsibility to it. So as to avoid complexity of putting logic inside single method and single class. Designated responsibility for each class and methods makes the code readable, code maintenance is easy, provides fliexibility and modularity.

Most Widely Used Example → Spring Framework has divided the business logic of MVC, Service and Database related functions across various layers like Controller, Service, and Repository layer and making use of appropriate annotations.

Note → We should avoid using utility classes common in a package as developers tend to dump all the helping methods in these utility classes, instead we should have some private helper methods in the same class.

Example Use Case :

Calculator Application using Single Responsibility Principle

O → Open Closed Principle

“Software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification.”

This can be achieved using Inheritance or creating Interfaces
which adds layer of abstraction, ensure loose coupling among the implementing classes, also if some code is common across these classes, then we make use of composition or inheritance.

Example Use Case :

Payment Application having different payment types like Credit Card, Paypal, Debit Card Payments which can be implemented using one common interface and in future if new Payment type is introduced that can be directly extended using new class.

Payment Intreface having execute method implemented by Credit Crad, Debit Card and any new payment type to be introduced in future.

Credit Card Payment Type Implementation

Payment Application creating payment objects on runtime

In this case if we have to add new payment type like Pyapal in future we need to create new class of Paypal and do not need to change any other class and create the object of Paypal in main class and use the method.

L → Liskov Substitution Principle

Objects of superclass should be replaceable with objects of subclass without affecting correctness of program.

Example Use Case :

Consider vehicle abstract class with method rent and Car,
Truck extending this Vehicle class and providing the implementation, respectively.​ Another class Electric Vehicle extends Vehicle and has rent method implementation which depends on the result of its own method isCharged().​ So, it means it is violating the Liskov substitution principle because if Vehicle instance is Electric Scooter, then he will not be simply able to rent the scooter instead he needs to first charge also.

Liskov Substitution Principle Violation

Main Vehicle Class Extended by Car, Truck and Electric Scooter

Electric Scooter Extending Vehicle but violates LSP

We cannot replace Electric Scooter directly in the places where Vehicle instance is used in our main application hence it is violating Liskov Substitution Principle.

LSP Implementation :

Chargeable Interface with method charge

In addition to Vehicle interface Electric Scooter is implementing both Vehicle and Chargeable Interface.

Rental Service class implementation

We can directly pass object of Vehicle whether it is electic scooter which has both chargeable and vehcile implementations or simply a car class which only has vehcile interface implementation now the code will not break and this clearly follows Liskov Substitution principle.

Note : remaining principles covered in part 2 https://saumyaawasthi187.medium.com/s-o-l-i-d-principle-part-2-17ef9e2adb87

标签:code,原则,SOLID,should,method,Principle,Vehicle,class
From: https://www.cnblogs.com/lori/p/18624033

相关文章

  • 【从零开始入门unity游戏开发之——C#篇26】C#面向对象动态多态——接口(Interface)、接
    文章目录一、接口(`Interface`)1、接口的定义接口的基本语法:2、接口的实现实现接口:3、接口的特性4、接口的优势5、小结二、接口也遵循里氏替换原则三、密封方法(`sealed`)1、**密封方法的定义**2、**如何使用密封方法**示例代码:3、**解释**4、**为什么要使用密封方法?**5......
  • 渗透之SolidState靶机
    目录1、信息收集1.1namp扫描1.扫描端口2.端口详细信息3.脚本扫描1.2nikto1.3gobuster2、信息整合2.1、端口服务2.2、页面3、渗透3.1获取shell1.JAMES漏洞2.尝试默认密码3.修改邮箱用户密码4.查看用户邮件5.进入ssh6.逃逸rbash获取到正常......
  • [机器人机构学]课设三:SOLIDWORKS模型转URDF
    提示:使用软件SOLIDWORKS2022插件sw2urdf需要安装:VMwareWorkstationProROS操作系统不想自己安装ROS的看这里系列文章目录[机器人机构学]课设一:通过杆件四大参数确定机械臂模型[机器人机构学]课设二:三维建模及爆炸图动画制作[机器人机构学]课设三:SOLIDWORKS模......
  • 软件设计和开发原则
    软件开发中有一些公认的设计和开发原则,有助于提高代码的质量、可维护性和扩展性。以下是一些核心的开发原则:1.SOLID原则SOLID是五个面向对象设计原则的缩写:单一职责原则(SRP):每个类或模块只应有一个原因导致其变化,即每个类只负责一个职责。开放封闭原则(OCP):软件实体(类、模块......
  • WPF DashStyles,Solid,Dash,Dot,DashDot,DashDotDot
    <Windowx:Class="WpfApp73.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.......
  • SOLIDWORKS快捷键命令大全
    SOLIDWORKS是一款广泛应用于工程设计、产品开发及机械制造领域的三维计算机辅助设计(CAD)软件。它以其直观的用户界面、强大的设计功能,成为了深受专业设计师喜爱的设计工具。想要在SOLIDWORKS中以更高的效率绘图,掌握一些快捷键,会让你的设计工作更加得心应手!我们一起来看下SOLIDWORK......
  • 数据库设计的四大原则:优化性能、保证一致性与高效处理
    目录一.数据冗余最小化二.数据一致性三.事务处理四.查询性能优化数据库设计不仅是关于创建表和字段的简单任务,更是构建一个高效运行、易于维护且能够确保数据一致性的系统的核心。一个好的数据库设计不仅能提升应用程序的性能,还能为未来的扩展和维护奠定坚实基础。......
  • 国内外免费 solidworks零件库 3d零件库 traceparts零件库下载网站推荐
    SolidWorks零件库‌是包含标准件和常用件模型的资源库,旨在帮助用户更高效地进行设计和建模工作。它包含了丰富的零件资源,涵盖了多种常用的机械零件和标准件,能够极大地提升设计效率和准确性‌。今天小编给大家推荐几家国外和国内比较优秀的零部件模型免费下载的平台。一、CAD......
  • 靶场奇妙记之SolidState
    公众号:泷羽Sec-Ceo声明!        学习视频来自B站up主**泷羽sec**有兴趣的师傅可以关注一下,如涉及侵权马上删除文章,笔记只是方便各位师傅的学习和探讨,文章所提到的网站以及内容,只做学习交流,其他均与本人以及泷羽sec团队无关,切勿触碰法律底线,否则后果自负!!!!有兴趣的......
  • 设计原则看这一篇就够了——单一职责原则
    1.原则介绍-what单一职责在不同书籍中的理解:一个模块应该有且仅有一个变化的原因。——《敏捷软件开发:原则、实践与模式》一个模块应该对一类且仅对一类行为者(actor)负责。——《架构整洁之道》单一职责原则的英文是SingleResponsibilityPrinciple,缩写为SRP。这个原......