- Expert Android Programming
- Prajyot Mainkar
- 215字
- 2021-07-08 10:29:14
Interface Segregation Principle
The Interface Segregation Principle states that:
No client should be forced to depend on methods it does not use.
According to this principle, if an interface has too many methods, then we need to divide the interface into smaller interfaces with fewer methods. A simple example of this principle is shown next.
Let us assume we are using a custom interface to detect various states of a view:
public interface ClickListener { public void onItemClickListener(View v, int pos); public void onItemLongClickListener(View v, int pos); public void onItemPressListener(View v, int pos); public void onSelectedListener(View v, int pos); }
Now, while implementing this listener, we only want the onItemClickListener or the onItemLongClickListener; the others are not required but we still have to use them in the code. This violates the Interface Segregation Principle.
Now we can easily resolve this by splitting the interface into smaller interfaces, like this:
public interface ClickListener { public void onItemClickListener(View v, int pos); public void onItemLongClickListener(View v, int pos); } public interface HoldListener { public void onItemPressListener(View v, int pos); public void onSelectedListener(View v, int pos); }
Now we will only initialize the ClickListener and use its methods instead of the old interface where we had to utilize four methods. Here we have segregated them into two different interfaces.
- C++程序設計(第3版)
- Git Version Control Cookbook
- Getting Started with React
- Flutter開發實戰詳解
- Learn to Create WordPress Themes by Building 5 Projects
- Network Automation Cookbook
- Web Application Development with MEAN
- Instant RubyMotion App Development
- Oracle Exadata專家手冊
- D3.js 4.x Data Visualization(Third Edition)
- SQL Server 2016數據庫應用與開發
- Learning Continuous Integration with TeamCity
- Unity 3D腳本編程:使用C#語言開發跨平臺游戲
- 產品架構評估原理與方法
- Building Apple Watch Projects