Interface isolation principle based on design pattern Principle and Design Pattern Principle

Source: Internet
Author: User

Interface isolation principle based on design pattern Principle and Design Pattern Principle

Before talking about the interface isolation principle, we should first clarify our main character: What is an interface? There are two types of interfaces:

One is an instance Interface, which declares a class in Java and then generates an instance using the new keyword. It is a description of a type of things, this is an interface. For example, if you define a Person class and use Person zhangSan = new Person () to generate an instance, the instance follows the Person class standard, the Person class is the interface of zhangSan. Why not read it? It doesn't matter. It takes too long for the Java language to be immersed. The main character has already appeared, so let's look at its principles. There are two definitions:

Definition 1: Clients shocould not be forced to depend upon interfaces that they don't use. The client should not rely on interfaces that are not needed.

The second definition: the dependency of one class to another one shoshould depend on The smallest possible interface. The dependency between classes should be built on the smallest interface.

The definition of a new thing is generally hard to understand, and it is normal to be obscure. Otherwise, it will make people feel that you are at a low level, which is also the basis for some international manufacturers to cheat in China, how can you worship me without a full set of terms? Let's take a look at this definition. First, let's talk about the first definition that the client should not depend on it and does not need interfaces. What does that depend on? Depending on the interface required by the client, the client can provide any excuse to remove unnecessary interfaces, so you need to refine the interface to ensure its purity. Then, let's look at the second definition, the dependency between classes should be built on the smallest interface, which requires the smallest interface and refined interfaces, which is exactly the same as the first definition, it is just two different descriptions of a thing.

We can generalize these two definitions into one sentence: to create a single interface, do not create a bloated interface. In other words, the interface should be refined as much as possible, and the methods in the interface should be as few as possible. You may be confused here. Isn't this the same as the single responsibility principle? Error: The interface isolation principle is different from the rule defined by a single responsibility. A single responsibility requires a single responsibility for the class and interface, and a heavy duty is the responsibility, methods that do not require interfaces are reduced. For example, a responsibility may contain 10 methods. All 10 methods are put in one interface and provided to multiple modules for access, each module is accessed according to the specified permissions. It does not need to be accessed by means of document constraints outside the system. It is permitted according to the single responsibility principle and is not allowed according to the interface isolation principle, because it requires "try to use multiple special interfaces", what does a special interface mean? It refers to the interfaces provided to multiple modules, and several interfaces should be provided to several modules, instead of creating a large and bloated interface, which can be accessed by all modules.

Design that does not follow the interface isolation principle:

The design follows the interface isolation principle:

 

When splitting an interface based on the Interface isolation principle, you must first meet the single responsibility principle.

 

Let's take an example to illustrate what requirements the interface isolation principle puts forward to us. Now, the most frequently used method for boys to call girls is "beauty". Let's define what beauty is today: first, we need to look good, and second, we need to be pretty, then you must have a temperament. Of course, the order of the three is different. To become a beauty, you must have the appearance, body, and temperament, let's use the class graph class to reflect the process of looking for beautiful women (of course, you can think of yourself as a star). Let's look at the class graph:

 

Defines an IPettyGirl interface, declares that all beautiful women should have goodLooking, niceFigure and greatTemperament, and then defines an abstract class AbstractSearcher. Its role is to search for beautiful women and then display information, only Beauty is defined according to this specification, and Searcher is much easier. Let's first look at the definition of beauty:

Public interface IPettyGirl {// you must have a good public void goodLooking (); // you must have a good public void niceFigure (); // you must have a public void greatTemperament ();}

Beauty is such a definition. You don't need to stream your mouth onto the keyboard. Then we can see the beauty implementation class:

Public class PettyGirl implements IPettyGirl {private String name; // All beautiful women have names public PettyGirl (String _ name) {this. name = _ name;} // pretty face public void goodLooking () {System. out. println (this. name + "--- pretty face! ");} // Public void greatTemperament () {System. out. println (this. name +" --- very good temperament! ");} // The body must be public void niceFigure () {System. out. println (this. name +" --- very good! ");}}

Then let's look at the AbstractSearcher class, which generally refers to the star detection industry. The source code is as follows:

Public abstract class AbstractSearcher {protected IPettyGirl pettyGirl; public AbstractSearcher (IPettyGirl _ pettyGirl) {this. pettyGirl = _ pettyGirl;} // search for beautiful women, list beautiful women information public abstract void show ();}

The two roles in the scenario, beauty and star scouts, have been completed. Let's write a scenario class to demonstrate our process:

Public class Client {// search for and display the beauty information public static void main (String [] args) {// define a beauty IPettyGirl yanYan = new PettyGirl ("Beautiful Girl "); abstractSearcher searcher = new Searcher (yanYan); searcher. show ();}}
Find the beauty and print the beauty information. The source code is as follows:
Public class Searcher extends actsearcher {public Searcher (IPettyGirl _ pettyGirl) {super (_ pettyGirl);} // displays information about beauty. public void show () {System. out. println ("-------- beauty information: -----------------"); // shows the face of super. pettyGirl. goodLooking (); // shows the super shape. pettyGirl. niceFigure (); // displays super temperament. pettyGirl. greatTemperament ();}}

 

The running result is as follows:

-------- The beauty information is as follows: --------------- pretty girl --- pretty face! Awesome --- very good figure! Awesome --- excellent temperament!

Use the interface isolation principle: Modify the class diagram again:

 

Split the original IPettyGirl interface into two interfaces. One is the beautiful girl IGoodBodyGirl, which features a superb face and body, but has no aesthetic quality, such as spitting, the exported products are KAO and CAO, with a low level of education. The other is IGreatTemperamentGirl, a beautiful girl with a high level of conversation and cultivation. We split a bloated interface into two specialized interfaces, improving flexibility and maintainability, whether in the future, you can easily use PettyGirl to define a beautiful beauty or a beautiful temperament. Let's first look at two types of beauty interfaces:

Public interface IGoodBodyGirl {// public void goodLooking (); // public void niceFigure ();}
Public interface IGreatTemperamentGirl {// public void greatTemperament ();}

The implementation class has not changed, but only implements two interfaces of the class. The source code is as follows:

Public class PettyGirl implements IGoodBodyGirl, IGreatTemperamentGirl {private String name; // All beautiful women have names public PettyGirl (String _ name) {this. name = _ name;} // pretty face public void goodLooking () {System. out. println (this. name + "--- pretty face! ");} // Public void greatTemperament () {System. out. println (this. name +" --- very good temperament! ");} // The body must be public void niceFigure () {System. out. println (this. name +" --- very good! ");}}

 

After such a transformation, whether it is temperament or shape beauty, you can maintain the stability of the interface. Of course, you may want to talk about it. In the future, the aesthetic view may change. Only beautiful faces are beautiful women. The IGoodBody interface still needs to be modified. Indeed, it is, but there are limits in design, you cannot consider future changes without limit. Otherwise, you will be stuck in the design quarary and cannot extricate yourself. The above principle of changing a bloated interface to two independent interface dependencies is the interface isolation principle, so that AbstractSearcher can depend on two dedicated interfaces more flexibly than relying on a comprehensive interface. An interface is a contract provided during design. Multiple Interfaces can be defined separately to prevent future changes and improve system flexibility and maintainability.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.