当前位置:网站首页>Opening and closing principle
Opening and closing principle
2022-06-28 14:12:00 【Hua Weiyun】
Opening and closing principle
Definition
This principle has two characteristics , One is to say ‘ It's open to expansion (Open for extension)’, The other is that ‘ Closed for changes (Closed for modification)’[ASD].
【 Problem introduction 】
We need to deal with something , There are fixed steps :a->b->c, But one day a customer has a need , An additional step should be added in certain cases , At this time, we should follow the principle of opening and closing , The original method logic should not be modified directly , Instead, write subclasses , To expand .
【 Code 】
BasicProcessing
package pers.keafmd.accumulate.designmode.openclosedprinciple;/** * Keafmd * * @ClassName: BasicProcessing * @Description: * @author: Cowherd Conan * @date: 2022-06-27 19:22 */public interface BasicProcessing { void handle();}BasicProcessingLogic
package pers.keafmd.accumulate.designmode.openclosedprinciple;/** * Keafmd * * @ClassName: BasicProcessingLogic * @Description: Basic processing logic * @author: Cowherd Conan * @date: 2022-06-27 19:19 */public class BasicProcessingLogic implements BasicProcessing{ public int num; public BasicProcessingLogic() { num = 10; } @Override public void handle(){ System.out.println(" Processing steps a..."); System.out.println(" Processing steps b..."); System.out.println(" Processing steps c..."); System.out.println(num); }}CustomerSpecialProcessingLogic
package pers.keafmd.accumulate.designmode.openclosedprinciple;/** * Keafmd * * @ClassName: CustomerSpecialProcessingLogic * @Description: Customer processing logic * @author: Cowherd Conan * @date: 2022-06-27 19:21 */public class CustomerSpecialProcessingLogic extends BasicProcessingLogic{ @Override public void handle(){ super.handle(); } public void specialHandel(){ super.handle(); System.out.println(" Processing steps d..."); }}OpenClosedPrincipleTest
package pers.keafmd.accumulate.designmode.openclosedprinciple;import java.util.Random;/** * Keafmd * * @ClassName: OpenClosedPrincipleTest * @Description: Opening and closing principle * @author: Cowherd Conan * @date: 2022-06-27 19:26 */public class OpenClosedPrincipleTest { public static void main(String[] args) { BasicProcessing basicProcessing = new BasicProcessingLogic(); boolean flag = new Random().nextBoolean();; if(flag){ basicProcessing.handle(); }else{ CustomerSpecialProcessingLogic processingLogic = new CustomerSpecialProcessingLogic(); processingLogic.specialHandel(); } }}The test results :
Normal logic
Processing steps a... Processing steps b... Processing steps c...10Process finished with exit code 0Special logic :
Processing steps a... Processing steps b... Processing steps c...10 Processing steps d...Process finished with exit code 0to open up - Closure principle is the core of object-oriented design . Following this principle can bring the huge benefits claimed by object-oriented technology , It's maintainable 、 Scalable 、 Reusable 、 Flexibility . Developers should abstract only those parts of the program that show frequent changes , However , It's also not a good idea to deliberately abstract every part of an application . It's just as important to reject the immature abstraction as the abstraction itself [ASD]. Bear in mind , Bear in mind
边栏推荐
- Deveco studio 3.0 editor configuration tips
- Tencent cloud international ECS has no network after logging in. How to troubleshoot?
- Double buffer drawing
- Based on asp Net based document retrieval system
- i++ , ++i
- 木兰开放作品许可证1.0面向社会公开征求意见
- 腾讯云国际云服务器登录之后没有网络,如何排查?
- 基于asp.net的文献检索系统
- 中国内地仅四家突围 联想智慧颐和园荣获 “2022年IDC亚太区智慧城市大奖”
- Action interprets value. The chairman of chenglian Youpin Han attended the Guangdong Yingde flood fighting donation public welfare event
猜你喜欢

Tencent cloud international ECS has no network after logging in. How to troubleshoot?

Why can't Bert completely kill the BM25??

Nature | mapping the interaction map of plant foliar flora to establish genotype phenotype relationship

Only four breakthrough Lenovo smart Summer Palace in mainland China won the "IDC Asia Pacific Smart City Award in 2022"

ArrayList源码解析

Foreign trade SEO Webmaster Tools

开源社邀您参加OpenInfra Days China 2022,议题征集进行中~

中国内地仅四家突围 联想智慧颐和园荣获 “2022年IDC亚太区智慧城市大奖”

外贸邮件推广怎么统计维度

Research and Simulation of chaotic digital image encryption technology based on MATLAB
随机推荐
Special test for cold and hot start of app
设计一个有getMin功能的栈
Design artificial intelligence products: technical possibility, user acceptability and commercial feasibility
你的代码会说话吗?(上)
2022中式烹調師(高級)試題及在線模擬考試
同花顺上开户是安全的吗
China Radio and television 5g package is coming, lower than the three major operators, but not as low as expected
Native JS implements drag and drop of page elements
荐书丨《大脑通信员》:如果爱情只是化学反应,那还能相信爱情吗?
Simple understanding of ThreadLocal
猫狗队列
你的代碼會說話嗎?(上)
3、项目的整体UI架构
2022金属非金属矿山安全检查(地下矿山)复训题库及在线模拟考试
What does VPS do? What are the famous brands? What is the difference with ECS?
等保备案主体是谁?在当地网安进行备案是吗?
If a programmer goes to prison, will he be assigned to write code?
Reverse a stack with recursive functions and stack operations only
NPOI导出Excel并下载到客户端
Build a learning environment