It takes about 5 minutes to read this article

  • preface

  • 1. Overview of interface isolation principles

  • 1.1. Customized services

  • 1.2. Interface contamination

  • 2. Application of the interface isolation principle

  • 2.1. Embodiment in memo mode

preface

1. Overview of interface isolation principles

The interface isolation principle is the interface isolation principle. So why do you want to isolate interfaces? This is because only certain interfaces need to be exposed instead of all interfaces. Therefore, you need to isolate the interfaces and isolate the interfaces that do not need to be isolated.

1.1. Customized services

For example, a role service contains three methods, named method1, method2, and method3, which correspond to three different clients. Normally, we would integrate all three methods into one interface, and then the three clients would implement the interface with three methods. But we know that each client needs only a method, the implementation of each client is much exposed the two methods, then you can use the interface segregation principle, the three methods, their respective interface is implemented, and then by three client implementation, so as to ensure the only exposure to every client need method without much additional exposure method.

1.2. Interface contamination

The above is the simplest example of interface isolation. It can also be seen from this example that in actual development, methods may be integrated into one interface for convenience, resulting in overcrowding and pollution of the interface. In subsequent development, if service methods need to be exposed, interface isolation cannot be easily realized. Therefore, you can analyze the interfaces to be implemented before implementing them, that is, each interface only implements one role or one type of task.

For example, if we have a Person interface with eat, drink, walk, run, work, learn and other methods in the interface, then we can split the methods in the Person interface. Eat and drink can be a separate interface. Walk and run are separate interfaces, and work and Learn are separate interfaces. This ensures that each interface only does the same type of work, and that each interface represents a role and implements an object of the same interface. In this way, it is convenient to realize the external exposure service in the later period, so as to maintain the stability and practicality of the whole system.

2. Application of the interface isolation principle

2.1. Embodiment in memo mode

The memo pattern captures the state of an object and externalizes it, without breaking encapsulation, so that the object can be restored to its stored state at a later time.

/** * The primitive class that stores state to Memento * This class is the original used class, and the changed state is stored in the Memento class. */ public class Originator {/** * a state that is stored in the Memento if the state changes */ private String state; public String getState() { return state; } public void setState(String state) { this.state = state; } /** * public Memento saveStateToMemento() {return new Memento(state); } @return */ public void getStateFromMemento(Memento Memento) {state = Memento. GetState (); }} /** * The status of the Originator class is the same as that of the original class. */ public class Memento {private String state; public Memento(String state) { this.state = state; } public String getState() { return state; }} /** * Restore status class, Public class CareTaker {private List<Memento> mementoList = new ArrayList<>(); public void add(Memento memento) { mementoList.add(memento); } public Memento get(int index) { return mementoList.get(index); } } /** * */ public class MementoDemo { public static void main(String[] args) { Originator originator = new Originator(); CareTaker careTaker = new CareTaker(); Originator.setstate ("state #1"); / / Originator.setState ("state #1"); originator.setState("state #2"); careTaker.add(originator.saveStateToMemento()); originator.setState("state #3"); careTaker.add(originator.saveStateToMemento()); originator.setState("state #4"); System.out.println("current state:" +originator.getState()); / / originator to recover the rzhetsky. GetStateFromMemento (careTaker. Get (0)); System.out.println("first save state:"+originator.getState()); / / originator to recover the rzhetsky. GetStateFromMemento (careTaker. Get (1)); System.out.println("second save state:"+originator.getState()); }}Copy the code

The example above makes sure that the state of the Originator object is captured without changing the wrapper, stored in Memento, and then restored by the CareTaker.

Without breaking the understanding of encapsulation, the memo object is required to provide a dual interface to the outside world, a wide interface and a narrow interface. In the preceding example, the wide interface refers to the CareTaker and the narrow interface refers to the Originator.

A wide interface can be understood as a need to access information stored by the memo role in order to recover its state.

The narrow interface can be understood as not needing to read the information stored in the memo, but simply getting a state.

The memorandum mode implements interface isolation in this way, ensuring that external interfaces are lightweight and not bloated. Therefore, the interface isolation principle is the guiding principle and the memo mode is the practical application.

The core of dependency inversion is: Rely on the abstract, not the concrete. The abstract is the interface that we usually work with. The interface does what we want to do, and the concrete is the class that implements the interface, which does the specific process to achieve the goal.

References:

1. Java and Patterns