SOLID原理的详细信息。
Before I start, I would like to say thanks to Co-Author of this article Soumya Pal.
SOLID Principle
SOLID Principle是一种广泛使用的软件设计模式。 五个原则共同构成SOLID原则。 它是由Robert C. Martin于1988年推出的,距Java 1st发行版已有8年了。 以下列出了五项原则
- S-单一责任原则O-扩展可扩展,封闭可扩展L-里斯科夫替代原理I-接口隔离原理D依赖倒置原则
Single Responsibility Principle
S代表单一责任原则。 这意味着单个模块可以承担一个特定的职责,而不是承担很多职责。 我们一般都知道一个神级,这意味着,一个级知道很多事情,这种方法违反了单一责任原则。 Let's Take an example, We need a HealthInsurance software which can approve claims which have been made。 Now we check the below class
package org.example.solidprincipleclass HealthInsurance {/*** This function will verify that, the documents provide is* all good or not good.* @return Boolean*/public Boolean isValidClaims() {// Function defination goes here.return true;} /*** This function will check wheather claims are valid or not the * approved the claims.* @return Boolean*/public Boolean approve() {if(this.isValidClaims()){return true;}return false;}}
在这里,我们可以看到一个健康保险处理索赔验证以及批准索赔的类,这里单一责任原则违反,一堂课应该 不包含一个以上的责任。 现在,让我们修改代码。
package org.example.solidprincipleclass HealthInsurance {/*** This function will verify that, the documents provide is* all good or not good.* @return Boolean*/public Boolean isValidClaims() {// Function defination goes here.return true;}}class InsuranceApprovalManager {private HealthInsurance healthInsurance;InsuranceApprovalManager (HealthInsurance healthInsurance) {this.healthInsurance = healthInsurance;}/*** This function will check wheather claims are valid or not the * approved the claims.* @return Boolean*/public Boolean approve() {if(healthInsurance.isValidClaims()){System.out.println("Insurance Claims Successfully Completed.");return true;}System.out.println("Insurance Claims Failed.");return false;}
}
现在,我们将责任分为两类,一类健康保险它将验证索赔并保险审批经理将在确认索赔的好坏之后予以批准。 现在这是继单一责任原则。
Open for Extension, Closed for Modification Principle
SOLID属性的第二原理为扩展而打开,为修改而关闭。 名称本身解释了这个概念。 一个模块的设计方式是,无需修改代码即可实现不同的行为。停但是如何? 让我们以上面所见的相同示例为例。
package org.example.solidprincipleclass HealthInsurance {/*** This function will verify that, the documents provide is* all good or not good.* @return Boolean*/public Boolean isValidClaims() {// Function defination goes here.return true;}}class InsuranceApprovalManager {private HealthInsurance healthInsurance;InsuranceApprovalManager (HealthInsurance healthInsurance) {this.healthInsurance = healthInsurance;}/*** This function will check wheather claims are valid or not the * approved the claims.* @return Boolean*/public Boolean approve() {if(healthInsurance.isValidClaims()){System.out.println("Insurance Claims Successfully Completed.");return true;}System.out.println("Insurance Claims Failed.");return false;}
}
在这个例子中保险审批经理类依赖健康保险,因为它带有健康保险在构造函数中。 现在如果再有保险说人寿保险图片中,保险经理将如何批准索赔,那么我们需要修改代码。 这里我们违反了第二个原则,因为我们正在修改代码。 让我们看看如何编写代码,这样我们就不需要修改代码即可实现行为更改。
package org.example.solidprincipleinterface Insurance {public Boolean isValidClaims();
}class HealthInsurance implements Insurance{/*** This function will verify that the documents provide is* all good or not good.* @return Boolean*/@Overridepublic Boolean isValidClaims() {// Function defination goes here.return true;}}class LifeInsurance implements Insurance{/*** This function will verify that the documents provide is* all good or not good.* @return Boolean*/@Overridepublic Boolean isValidClaims() {// Function defination goes here.return true;}}class InsuranceApprovalManager {private Insurance insurance;InsuranceApprovalManager (Insurance insurance) {this.insurance = insurance;}/*** This function will check wheather claims are valid or not the * approved the claims.* @return Boolean*/public Boolean approve() {if(insurance.isValidClaims()){System.out.println("Insurance Claims Successfully Completed.");return true;}System.out.println("Insurance Claims Failed.");return false;}
}
在这里,我们定义了一个名为保险并实施Health保险, Life保险课,现在检查保险ApprovalManager建设者
InsuranceApprovalManager (Insurance insurance) {this.insurance = insurance;
}
以参考类型保险, So that, if other 保险 types added in future we can extend it and use it. No Need for modification of the current code structure.
Liskov Substitution Principle
这个原理是由Barbara Liskov在1988年提出的。它指出模块S是T的子类型,然后T的对象可以被S的对象替换,从而改变其属性。 这意味着引用类必须能够在不知道的情况下使用对象派生类。 让我们在下面举一个例子
package org.example.solidprincipleinterface Insurance {public Boolean isValidClaims();public Boolean doctorVerification();
}class HealthInsurance implements Insurance{/*** This function will verify that the documents provide is* all good or not good.* @return Boolean*/@Overridepublic Boolean isValidClaims() {// Function defination goes here.if(this.doctorVerification()) {return true;}return false;}/*** The Claims will check by the doctor whether it is approvable or not.* @return Boolean*/@Overridepublic Boolean doctorVerification() {//Function defincation goes herereturn true;}}class InsuranceApprovalManager {private Insurance insurance;InsuranceApprovalManager (Insurance insurance) {this.insurance = insurance;}/*** This function will check wheather claims are valid or not the * approved the claims.* @return Boolean*/public Boolean approve() {if(insurance.isValidClaims()){System.out.println("Insurance Claims Successfully Completed.");return true;}System.out.println("Insurance Claims Failed.");return false;}
}
在这里检查一下健康保险类isValidClaims()此功能取决于doctorVerification()。 特别是这个例子,一切都很好。 现在,让我们检查以下示例,
package org.example.solidprincipleinterface Insurance {public Boolean isValidClaims();public Boolean doctorVerification();
}class CarInsurance implements Insurance{/*** This function will verify that the documents provide is* all good or not good.* @return Boolean*/@Overridepublic Boolean isValidClaims() {// Function defination goes here.if(this.doctorVerification()) {return true;}return false;}/*** The Claims will check by the doctor whether it is approvable or not.* @return Boolean*/@Overridepublic Boolean doctorVerification() {//Function defincation goes herereturn true;}}class InsuranceApprovalManager {private Insurance insurance;InsuranceApprovalManager (Insurance insurance) {this.insurance = insurance;}/*** This function will check wheather claims are valid or not the * approved the claims.* @return Boolean*/public Boolean approve() {if(insurance.isValidClaims()){System.out.println("Insurance Claims Successfully Completed.");return true;}System.out.println("Insurance Claims Failed.");return false;}
}
在这里我们正在使用汽车保险,所以doctorVerification()完全不需要,但是我们需要实现该功能。 这种情况违反了堆栈的第三原则。 如何克服这种情况,让我们检查以下示例。
package org.example.solidprincipleinterface Insurance {public Boolean isValidClaims();
}interface DoctorVerifiableInsurance extends Insurance {public Boolean isValidClaims();public Boolean doctorVerification();} interface EngineerVerifiableInsurance extends Insurance {public Boolean isValidClaims();public Boolean engineerVerification();} class HealthInsurance implements DoctorVerifiableInsurance{/*** This function will verify that the documents provide is* all good or not good.* @return Boolean*/@Overridepublic Boolean isValidClaims() {// Function defination goes here.if(this.doctorVerification()) {return true;}return false;}/*** The Claims will check by the doctor whether it is approvable or not.* @return Boolean*/@Overridepublic Boolean doctorVerification() {//Function defincation goes herereturn true;}}class CarInsurance implements EngineerVerifiableInsurance{/*** This function will verify that the documents provide is* all good or not good.* @return Boolean*/@Overridepublic Boolean isValidClaims() {// Function defination goes here.if(this.engineerVerification()) {return true;}return false;}/*** The Claims will check by the doctor whether it is approvable or not.* @return Boolean*/@Overridepublic Boolean engineerVerification() {//Function defincation goes herereturn true;}}class InsuranceApprovalManager {private Insurance insurance;InsuranceApprovalManager (Insurance insurance) {this.insurance = insurance;}/*** This function will check wheather claims are valid or not the * approved the claims.* @return Boolean*/public Boolean approve() {if(insurance.isValidClaims()){System.out.println("Insurance Claims Successfully Completed.");return true;}System.out.println("Insurance Claims Failed.");return false;}
}
在这里,我们定义了另外两个接口(工程师可验证保险,医生可验证保险)扩展基本接口,并实现了该类健康保险,汽车保险。 现在,我们有了正确的流程,无需执行那些不属于特定保险类别的方法。
Interface Segregation Principle:
该原则是适用于接口而不是SOLID中的类的第一项原则,并且与单职责原则相似。 它指出“不要强迫任何客户端实现与其无关的接口”。 在这里,您的主要目标是避免出现胖界面,而优先选择许多小型的特定于客户端的界面。 您应该选择许多客户端接口,而不是一个常规接口,并且每个接口都应负有特定责任。
让我们举个例子:
package org.example.solidprincipleinterface Insurance {public Boolean isValidClaims();public Boolean verification();public Boolean lifeInsurance();public Boolean healthInsurance();public Boolean fireInsurance();
}class Insuranceclaim implements Insurance{@Overridepublic Boolean lifeInsurance() {if(isValidClaims()){return true;}return false;}/*** This function will verify that the documents provide is* all good or not good.* @return Boolean*/@Overridepublic Boolean isValidClaims() {if (verification()){return true;}return false;}@Overridepublic Boolean verification() {return true;}@Overridepublic Boolean healthInsurance() {return null;}@Overridepublic Boolean fireInsurance() {return null;}
}class InsuranceApprovalManager{private Insuranceclaim insurance;InsuranceApprovalManager(Insuranceclaim insurance){this.insurance = insurance;}/*** This function will check wheather claims are valid or not the * approved the claims.* @return Boolean*/public void approve(){if(insurance.lifeInsurance()){System.out.println("Insurance amount processed successfully");}else {System.out.println("Insurance amount not approved");}}
}
在上面的示例中,您可以看到“ InsuranceClaim”需要提供“ healthInsurance()”和“ fireInsurance()”方法的实现,即使不需要它们也是如此。 这违反了接口隔离原则。 这样的违反会影响代码的可读性,并使程序员感到困惑。 那么如何解决这个问题呢? 这是将接口分为多个接口的解决方案,每个接口针对特定的行为。 看下面的例子...
package org.example.solidprincipleinterface Insurance {public Boolean isValidClaims();public Boolean verification();
}interface LifeInsurace{public Boolean lifeInsurance();
}
interface HealthInsurance{public Boolean healthInsurance();
}
interface FireInsurance{public Boolean fireInsurance();
}class Insuranceclaim implements LifeInsurace,Insurance{@Overridepublic Boolean lifeInsurance() {if(isValidClaims()){return true;}return false;}/*** This function will verify that the documents provide is* all good or not good.* @return Boolean*/@Overridepublic Boolean isValidClaims() {if (verification()){//return true;}return false;}@Overridepublic Boolean verification() {return true;}}class InsuranceApprovalManager{private Insuranceclaim insurance;InsuranceApprovalManager(Insuranceclaim insurance){this.insurance = insurance;}/*** This function will check wheather claims are valid or not the * approved the claims.* @return Boolean*/public void approve(){if(insurance.lifeInsurance()){System.out.println("Insurance amount processed successfully");}else {System.out.println("Insurance amount not approved");}}
}
在上面的示例中,“保险”界面分为四个界面,即人寿保险,健康保险,火灾保险和“保险”界面本身。 每一个都负责特定的行为,并且还可以看到,当“ InsuranceClaim”实现“ LifeInsurance”和“ Insurance”时,其他两个接口就不需要在那里存在。 因此,由于接口的分离,修改后的代码更具可读性,并且不易修改。
Dependency Inversion Principle
该原理的基本思想很简单,即很重要:提供复杂逻辑的高级模块应易于重用,并且不受提供实用程序功能的低级模块的更改的影响。 为此,您需要引入一种抽象,该抽象将高级模块和低级模块彼此分离。
基于此思想,Robert C. Martin对依赖关系反转原理的定义包括两个部分:
1)高级模块不应依赖于低级模块。 两者都应依赖抽象。 2)抽象不应依赖细节。 细节应取决于抽象。
package org.example.solidprincipleclass Insurance{public Boolean isValidClaims(){return true;}/*** The Claims will check by the doctor whether it is approvable or not.* @return Boolean*/public Boolean approval(){if(DoctorvVerification()){return true;}return false;}public Boolean DoctorvVerification(){if(isValidClaims()){return true;}return false;}
}class ManageHeathInsuranceClaim {private Insurance insurance;ManageHeathInsuranceClaim(Insurance insurance){this.insurance = insurance;}/*** This function will check wheather claims are valid or not the * approved the claims.* @return Boolean*/public void approved(){if(insurance.approval()){System.out.println("Insurance Claim processed successfully");}System.out.println("Claim couldn't be processed");}
}
在上面的示例中,可以看到ManageHealthInsuranceClaim现在依赖于保险,这是一个高级模块,它取决于低级模块。 但这违反了依赖倒置原则的第一个属性。 另外,如果有人要添加其他保险,PropertyInsurance说,为此,需要修改保险类。 为了解决这个问题,我们需要实现一个接口。 看下面的例子...
package org.example.solidprincipleinterface Insurance{public Boolean isValidClaims();public Boolean approval();
}class HealthInsuranceManager implements Insurance {public Boolean isValidClaims() {return true;}public Boolean approval() {if (doctorVerification()) {return true;}return false;}/*** The Claims will check by the doctor whether it is approvable or not.* @return Boolean*/public Boolean doctorVerification() {if (isValidClaims()) {return true;}return false;}
}class PropertyInsuranceManager implements Insurance{@Overridepublic Boolean isValidClaims() {return true;}public Boolean approval(){if(proertyVerification()){return true;}return false;}/*** The Claims will check by the govt. whether it is approvable or not.* @return Boolean*/public Boolean proertyVerification(){if(isValidClaims()){return true;}return false;}
}class ManageInsuranceClaim{private Insurance insurance;ManageInsuranceClaim(Insurance insurance){this.insurance = insurance;}/*** This function will check wheather claims are valid or not the * approved the claims.* @return Boolean*/public void approved(){if(insurance.approval()){System.out.println("Insurance Claim processed successfully");}System.out.println("Claim couldn't be processed");}
}
在修改后的代码中,高级模块取决于抽象(接口保险)而不是低级模块(HealthInsurancemanager,PropertyInsuranceManager),并且低级取决于抽象,这意味着任何人都可以添加其他保险模块,而无需 修改代码。 该代码完全满足依赖倒置原则的所有属性。
希望我们可以为您提供有关SOLID Property的深入知识。
from: https://dev.to//subhendumondal/solid-principle-in-details-1mka
SOLID原理的详细信息。相关推荐
- WebSocket 通信原理和详细使用(十六)
今天我们详细分析WebSocket 通信原理和使用 一.什么是 WebSocket ? WebSocket --一种在 2011 年被互联网工程任务组( IETF )标准化的协议.WebSocket ...
- Case Study: 利用JS实现数据库网页的数据分页、数据选择、数据详细信息查看功能
一.目标 该笔记的目的是引导读者借助WampServer平台和MySQL数据库,利用HTML/CSS/JS/PHP设计一个能够进行实现数据分页显示.数据选择.数据详细信息查看功能的数据库网页.该数据库 ...
- 请求失败或服务未及时响应 有关详细信息_「干货」从零开始的微服务搭建之路...
随着公司的业务发展,有幸经历了从单体应用迁移到分布式应用,又从分布式应用开始准备搭建微服务应用,以下是公司从零开始搭建微服务的过程,记录并分享出来,希望对大家有所帮助,我们先使用Spring Clou ...
- MapReduce的工作原理,详细解释WordCount程序
本篇文章主要说两部分:简单介绍MapReduce的工作原理:详细解释WordCount程序. MapReduce的工作原理 在<Hadoop in action>一书中,对MapReduc ...
- Requests库实战(三)---爬取豆瓣电影详细信息
完整代码 爬取豆瓣电影的详细信息 地址:豆瓣电影动画 向下滑动时新增的数据也是Ajax请求,原理和上一个项目是一样的.唯一的不同是此处请求url携带了多个参数 import requests impo ...
- 网站渗透测试原理及详细过程
渗透测试实战 site:baidu.com 渗透测试思路 site:baidu.com 带你入门渗透测试的5个项目:https://www.jianshu.com/p/5b82e42ae346 渗透测 ...
- 面向对象 solid_用简单的英语解释面向对象程序设计的SOLID原理
面向对象 solid The SOLID Principles are five principles of Object-Oriented class design. They are a set ...
- Javascript版-显示相应图片的详细信息
Hi All, 分享一个通过JS来显示相应图片的详细信息. 需求:进入页面时,动态加载图片信息:当鼠标移动到某一图片上时,则显示该图片的大图片并显示相应说明信息:当鼠标移开图片时,清除新创建的元素. ...
- Media Player Classic - HC 源代码分析 7:详细信息选项卡(CPPageFileInfoDetails)
===================================================== Media Player Classic - HC 源代码分析系列文章列表: Media P ...
最新文章
- android 有效载荷大图,避OOM
- 硬件手册里经常出现的assert以及deassert
- 静态分析android代码, 循环与trycatch
- iphone如何信任软件_你知道iPhone手机如何正确卸载软件?怪不得手机内存总是不够用!...
- cocos2d-x游戏开发(八)各类构造器
- 转:IDEA 创建类注释模板和方法注释模板
- 模拟太阳系的html,纯HTML5制作的震撼太阳系网页
- Siri背后语音识别巨头Nuance的衰落
- 江西 高职 分数 计算机,快讯!江西高职(专科)批次各高校投档分数线出炉…...
- ToolStripContainer
- Sql Server快速入门
- 数据结构中数据元素与数据项的区别
- KEIL5 C51软件安装详细图文教程
- veu中点击商品详情打开新窗口
- OSChina 周三乱弹 —— 东京不热,北海道有点热
- CyberSecurity Knowledge Base笔记
- 基于html5手机移动端对话框特效
- 微信小程序自定义搜索导航栏
- 十进制整数,转换成八进制和十六进制数并输出。
- 中国计量大学计算机专业全国名次多少,中国计量大学实力水平如何,全国排名是多少?...
热门文章
- pd安装win10错误-安全启动功能发现未经授权更改固件
- 基于android的检测心率,基于Android系统的心率信息监测软件的研究与实现
- Halcon DrawRegion()后会阻塞直到右键按下,请问如何主动取消绘制区域
- SAP AW01N 资产浏览器数据查看详解
- 基于jsp+mysql+ssm妇女联合会管理系统-计算机毕业设计
- 史密斯探测证实,BioFlash可检出空气中的SARS-CoV-2变异株,包括德尔塔和德尔塔+
- python 大数据开发工程师_大数据系列之大数据开发工程师
- 个人申请微信H5支付接口(个人免签约支付平台)
- springboot实现邮箱接收验证码
- python项目--O2O优惠券线下使用情况数据分析