SOLID rule in JAVA design.
Classes are the building blocks of your java application. If these blocks are not strong, your building (i.e. application) is going to face the tough time in future. This essentially means that not so well-written can lead to very difficult situations when the application scope goes up or application faces certain design issues either in production or maintenance.
On the other hand, set of well designed and written classes can speed up the coding process by leaps and bounds, while reducing the number of bugs in comparison.
In this post, I will list down 5 most recommended design principles, you should keep in mind, while writing your classes. These design principles are called SOLID, in short. They also form the best practices to be followed for designing your application classes.
- Single Responsibility Principle
- Open Closed Principle
- Liskov’s Substitution Principle
- Interface Segregation Principle
- Dependency Inversion Principle

5 java class design principles
Lets drill down all of them one by one.
Single Responsibility Principle
The name of the principle says it all:
"One class should have one and only one responsibility"
In other words, you should write, change and maintain a class for only one purpose. If it is model class then it should strictly represent only one actor/ entity. This will give you the flexibility to make changes in future without worrying the impacts of changes for another entity.
Similarly, If you are writing service/manager class then it should contain only that part of method calls and nothing else. Not even utility global functions related to module. Better separate them in another globally accessible class file. This will help in maintaining the class for that particular purpose, and you can decide the visibility of class to specific module only.
Open Closed Principle
This is second important rule which you should keep in mind while designing your application. It says:
"Software components should be open for extension, but closed for modification"
What does it mean?? It means that your classes should be designed such a way that whenever fellow developers wants to change the flow of control in specific conditions in application, all they need to extend your class and override some functions and that’s it.
If other developers are not able to design desired behavior due to constraints put by your class, then you should reconsider changing your class. I do not mean here that anybody can change the whole logic of your class, but he/she should be able to override the options provided by software in unharmful way permitted by software.
For example, if you take a look into any good framework like struts or spring, you will see that you can change their core logic and request processing, BUT you modify the desired application flow just by extending some classes and plugin them in configuration files.
Liskov’s Substitution Principle
This principle is a variation of previously discussed open closed principle. It says:
"Derived types must be completely substitutable for their base types"
It means that the classes fellow developer created by extending your class should be able to fit in application without failure. I.e. if a fellow developer poorly extended some
part of your class and injected into framework/ application then it should not break the application or should not throw fatal exceptions.
This can be insured by using strictly following first rule. If your base class is doing one thing strictly, the fellow developer will override only one feature incorrectly in worst case. This can cause some errors in one area, but whole application will not do down.
Interface Segregation Principle
This principle is my favorite one. It is applicable to interfaces as single responsibility principle holds to classes. It says:
"Clients should not be forced to implement unnecessary methods which they will not use"
Take an example. Developer Alex created an interface “Reportable” and added two methods generateExcel() and generatedPdf(). Now client ‘A’ wants to use this interface but he intend to use reports only in PDF format and not in excel. Will he achieve the functionality easily.
NO. He will have to implement two methods, out of which one is extra burden put on him by designer of software. Either he will implement another method or leave it blank. So are not desired cases, right??
So what is the solution? Solution is to create two interfaces by breaking the existing one. They should be like PdfReportable and ExcelReportable. This will give the flexibility to user to use only required functionality only.
Dependency Inversion Principle
Most of us are already familiar with the words used in principle’s name. It says:
"Depend on abstractions, not on concretions"
In other words. you should design your software in such a way that various modules can be separated from each other using an abstract layer to bind them together. The classical use of this principle of BeanFactory in spring framework. In spring framework, all modules are provided as separate components which can work together by simply injected dependencies in other module. They are so well closed in their boundaries that you can use them in other software modules apart from spring with same ease.
This has been achieved by dependency inversion and open closed principles. All modules expose only abstraction which is useful in extending the functionality or plugin in another module.
These were five class design principle which makes the best practices to be followed to design your application classes. Let me know of your thoughts.
Happy Learning !!
Share this:
SOLID rule in JAVA design.的更多相关文章
- [Java] Design Pattern:Code Shape - manage your code shape
[Java] Design Pattern:Code Shape - manage your code shape Code Shape Design Pattern Here I will intr ...
- java design pattern - adapter pattern
场景 适配器模式 总结 参考资料 场景 在编程中我们经常会遇到驴头不对马嘴的情况,比如以前是继承A的接口的对象,现在外部调用的时候需要该对象已B接口的形式来调用 ,这个时候我们可以让对象同时集成A和B ...
- java Design Patterns
设计模式(Design Patterns) ——可复用面向对象软件的基础 设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结.使用设计模式是为了 ...
- Java Design Pattern(Factory,Singleton,Prototype,Proxy)
一.Factory 设计模式: the most common pattern,create a new object ,eg. A a=new A();工厂模式的好处:工厂模式可以做到把创建对象单独 ...
- Java Design Patterns(2)
1.Factory Design pattern 工厂设计模式的优点 (1)工厂设计模式提供了接口而不是实现的代码方法. (2)工厂模式从客户端代码中删除实际实现类的实例化.工厂模式使我们的代码更健壮 ...
- Java Design Demo -简单的队列-异步多任务队列(java android)
简单的单线程队列 -- 工作的时候遇到劣质打印机.给打印机发消息,打印机就会打印,如果在打印机还在打印的时候,就 再发消息打印,就会出现消息丢失.所以需要给上一个任务一些处理的间隔时间. 单线程的消息 ...
- JAVA DESIGN PATTERN
工厂模式(factory) 简单工厂模式的概念 就是建立一个工厂类,对实现了同一接口的一些类进行实例的创建.简单工厂模式的实质是由一个工厂类根据传入的参数,动态决定应该创建哪一个产品类(这些产品类继承 ...
- analysed of J-SON/XML processing model Extend to java design model (J-SON/XML处理模型分析 扩展到Java设计模型 )
一.JSON和XML 1.JSON JSON(JavaScript Object Notation)一种轻量级的数据交换格式,具有良好的可读和便于快速编写的特性.可在不同平台之间进行数据交换.JSON ...
- Java design patterna
Java中的设计模式 设计模式是解决特定问题/任务的充分证明的解决方案. 现在,一个问题会在你脑海中产生什么样的具体问题?让我举个例子来解释一下. 给出的问题:假设您要创建一个只应创建单个实例(或对象 ...
随机推荐
- fine-grained
鸟类图像分类,CUB-200-2011,可以适用于图片左右的调整.
- 神奇的sort()函数
今天来谈一谈sort()函数,sort() 方法用于对数组的元素进行排序,用法为arrayObject.sort(sortby):括号中的为可选参数,准确来说应该是一个函数,这个函数用来规定排序方法, ...
- Sqlserver 函数
SQL2008 表达式:是常量.变量.列或函数等与运算符的任意组合. 1. 字符串函数 函数 名称 参数 示例 说明 ascii(字符串表达式) select ascii('abc') 返回 97 返 ...
- 前端中JSON对象和JSON字符串的相互转换
资料来源: http://www.css88.com/archives/3919
- 关于蜂窝物联技术 NBIoT 的一些观点
背景 SigFox 开始于2009,这项技术对长期作为标准移动电话交互标准的3GPP造成了有力冲击.SigFox 解决了终端设备互联场景下的多个痛点: 1 过于复杂: 2 昂贵的设备: 3 耗电不持久 ...
- 知识积累:CA详解
所有证书有多种文件编码格式,主要包括: CER编码(规范编码格式):是BER(基本编码格式)的一个变种,比BER规定得更严格DER编码(卓越编码格式):是BER(基本编码格式)的一个变种, 比BER ...
- Excel 去掉每次打开弹出自定义项安装的弹窗
弹窗: 解决方案: 一.打开“文件”——“选项”如图. 二.选择“加载项”,下面的“管理”,选择“COM加载项”,然后点击“转到”,弹出框: 三:在“可用加载项”下面你会发现有一项是“LoadTest ...
- Python Iterable Iterator Yield
可以直接作用于for循环的数据类型有以下几种: 一类是集合数据类型,如list / tuple / dict / set / str /等(对于这类iterable的对象,因为你可以按照你的意愿进行重 ...
- 多线程下的 Lambda表达式 异步 WebClient 读取程序图标,来作为托盘 图标 logo ico
//读取程序图标,来作为托盘图标this.notifyIcon.Icon = System.Drawing.Icon.ExtractAssociatedIcon(System.Windows.Form ...
- JAVA访问权限
同一个类 同一个包 不同包的子类 不同包的非子类 Private √ Default √ √ Protected √ √ √ Public √ √ √ √