- 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.
- 極簡算法史:從數學到機器的故事
- Instant Testing with CasperJS
- Android項目開發入門教程
- 數據結構簡明教程(第2版)微課版
- Rust Cookbook
- 小程序開發原理與實戰
- 大數據分析與應用實戰:統計機器學習之數據導向編程
- Unity 2D Game Development Cookbook
- 蘋果的產品設計之道:創建優秀產品、服務和用戶體驗的七個原則
- Java程序設計案例教程
- TMS320LF240x芯片原理、設計及應用
- Apache Camel Developer's Cookbook
- R語言:邁向大數據之路(加強版)
- Kotlin進階實戰
- 例說FPGA:可直接用于工程項目的第一手經驗