This is the 15th day of my participation in the August More Text Challenge
Background:
In the actual business development, many methods are defined in the interface because of the unreasonable design at the initial stage, and these interfaces do not need to be fully implemented in the implementation class. Such an interface definition is not scalable and will be difficult to maintain later. We will use examples to demonstrate the benefits of complying with the interface isolation principle.
Concept:
- Definition of the interface isolation principle: a client should not be forced to rely on methods for which it does not apply
- The interface isolation principle requires that a large interface be divided into smaller and more specific interfaces to ensure that clients only get the methods they need
Case study:
Requirements:
- Designing a HomePage page
- Click events to jump to EditPage page
- Function support double – click event: you can double – click exit program
- Design the EditPage page
- Functions support double – click events: you can double – click to select text
- Function Long press event: You can long press to select all texts
Violation of principle to achieve:
Define the listening interface associated with the click
interface OnClickListener {
onClick(): void;
onDoubleClick(): void;
onLongPress(): void;
}
Copy the code
HomePage:
class HomePage implements OnClickListener {
onClick(): void {
console.log("Trigger click event to enter edit page");
}
onDoubleClick(): void {
console.log("Trigger a double click event to exit the page");
}
onLongPress(): void {
// No related requirements, no implementation}}Copy the code
EditPage implementation:
class EditPage implements OnClickListener {
onClick(): void {
// No related requirements, no implementation
}
onDoubleClick(): void {
console.log("Trigger double click event, select text");
}
onLongPress(): void {
console.log("Trigger long press event, select text"); }}Copy the code
Description:
As can be seen from the above example, no page has a method that is irrelevant to itself and we need to specify in the code or maintenance document which methods must be implemented and which methods are not implemented, which is neither consistent with the design pattern nor conducive to maintenance.
Achieve in accordance with principles:
Fine-grained split of the original interface:
The split interface can be implemented by freely composing pages that need to be selected
interface OnClickListener {
onClick(): void;
}
interface OnDoubleClickListener {
onDoubleClick(): void;
}
interface OnLongPressListener {
onLongPress(): void;
}
Copy the code
HomePage:
class HomePage implements OnClickListener.OnDoubleClickListener {
onClick(): void {
console.log("Trigger click event to enter edit page");
}
onDoubleClick(): void {
console.log("Trigger a double click event to exit the page"); }}Copy the code
EditPage implementation:
class EditPage implements OnDoubleClickListener.OnLongPressListener {
onDoubleClick(): void {
console.log("Trigger double click event, select text");
}
onLongPress(): void {
console.log("Trigger long press event, select text"); }}Copy the code
Description:
We can see that code that conforms to design patterns will become more flexible, as will some of the event listening interfaces in Android development. Such as operation by Sql database again, often to the operation of the database will be included, open the database, connect to the database, close the database, the database to add data, deleting data, updating data and querying data, are to the operation of the database but often these same operation will be roughly divided into two classes to design, Further refinement of the query data may also be split separately. The separation of interfaces in accordance with rational design will be very important to achieve high cohesion and low coupling code.