- 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.
- 大話PLC(輕松動漫版)
- Monkey Game Development:Beginner's Guide
- AngularJS Web Application Development Blueprints
- 樂學Web編程:網站制作不神秘
- Mastering Python Scripting for System Administrators
- Android Native Development Kit Cookbook
- Gradle for Android
- Mastering Apache Maven 3
- SQL Server從入門到精通(第3版)
- Angular開發入門與實戰
- 移動互聯網軟件開發實驗指導
- Fast Data Processing with Spark(Second Edition)
- Extending Unity with Editor Scripting
- Mockito Essentials
- Software Development on the SAP HANA Platform