- 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.
- Spring Boot 2實戰之旅
- C# 7 and .NET Core Cookbook
- Facebook Application Development with Graph API Cookbook
- LabVIEW2018中文版 虛擬儀器程序設計自學手冊
- PostgreSQL for Data Architects
- BeagleBone Media Center
- Hadoop+Spark大數據分析實戰
- Linux環境編程:從應用到內核
- EPLAN實戰設計
- Scientific Computing with Scala
- Getting Started with Laravel 4
- 區塊鏈底層設計Java實戰
- 編程與類型系統
- C專家編程
- Django 3.0入門與實踐