Interface isolation principle -- give an example to illustrate the interface isolation principle in Java design pattern

Posted by areid on Sat, 12 Feb 2022 12:50:56 +0100

  • Before introducing the principle of interface isolation, let's take a look at the first example in the following example - counterexample

1, Examples

1. Counterexample

(1) Class diagram description

  • Because the class diagram is relatively clear, let's look at the class diagram first

  • As you can see, DogPlays Java and CatPlays Java implements the interface PetInterface respectively. DogPlaysUse and CatPlaysUse rely on DogPlays and CatPlays respectively through this interface, but they haven't used up all the methods in the implementation class, resulting in a waste of resources

(2) Code description

  • If the class diagram is not very clear, look directly at the code and feel it!
  • Direct code:

  • If you still don't understand the code, look down at the test and feel the calls between them

(3) Testing

  • Come on, let's take a look at the test and feel it

  • Of course, this test is a little responsible. It mainly allows you to experience the scenarios that may be applied to this situation in our development. Then we can make the test a little simpler. Next, let's look at the simpler ones

    It's much easier!
  • OK, you should understand this case thoroughly here!

(4) Analyze shortcomings (summary)

  • It can be seen that there are two implementations of PetInterface, namely dogplays Java and catplays java
  • The DogPlaysUse class relies on the DogPlays class through the interface PetInterface, but only the playBall and liketogouut methods are used.
    The DogPlays class implements all the methods of the PetInterface interface interface, so it causes waste
  • Similarly, the CatPlaysUse class relies on the CatPlays class through the interface PetInterface, but only the playBall, climbTree and catchMouse methods are used, so it also causes waste

2. Positive example

  • So how to optimize the above case?
  • Since an interface is wasteful, we will disassemble it. This is actually our interface isolation principle. Please continue

(1) Class diagram description

  • As shown in the figure, for the above cases, if the interface isolation principle is used for optimization, it is most reasonable to split into three interfaces in our design

(2) Code description


  • Compared with the counter example, it looks much cleaner and more comfortable

(3) Testing

  • There's nothing to say about the test. See for yourself

(4) Programme evaluation

  • Compared with the first writing method, the writing method of using interface isolation obviously makes the code clear to others. After the interface is disassembled, each line has its responsibilities, and the code is not bloated. It is easy to maintain if subsequent function changes or additions are made

3, Summary

  • The client should not rely on interfaces that it does not need
  • The dependence of one class on another should be based on the smallest interface,
  • In fact, don't put other methods outside a function module in an interface. It's a little like the principle of single responsibility. Let's experience it for ourselves

4, Attached code

1. Counterexample code

package com.liu.susu.principle.segregation.example2;

import org.springframework.stereotype.Service;

/**
 * @Description Interface isolation principle - positive example
 * @Author susu
 * @date 2022-02-12
 **/
public interface PetInterface {
    void playBall();//Play ball
}
interface DogInterface {
    void likeToGoOut();//walk a dog
}
interface CatInterface {
    void climbTree();//climb up a tree
    void catchMouse();//Catch a mouse
}

class DogPlays implements PetInterface,DogInterface{
    public void playBall() {
        System.out.println("Dogs like playing with balls");
    }
    public void likeToGoOut() {
        System.out.println("Dogs like to be walked every day");
    }
}

class CatPlays implements PetInterface,CatInterface{
    public void playBall() {
        System.out.println("Kittens like to play ball");
    }
    public void climbTree() {
        System.out.println("Kittens like climbing trees");
    }
    public void catchMouse() {
        System.out.println("Kittens like to catch mice");
    }
}

class DogPlaysUse {
    public void playBall(PetInterface petInterface){
        petInterface.playBall();
    }
    public void likeToGoOut(DogInterface dogInterface) {
        dogInterface.likeToGoOut();
    }
}

class CatPlaysUse {
    public void playBall(PetInterface petInterface){
        petInterface.playBall();
    }
    public void climbTree(CatInterface catInterface) {
        catInterface.climbTree();
    }
    public void catchMouse(CatInterface catInterface) {
        catInterface.catchMouse();
    }
}

/**
 * Test class
 */
class Test{
    public static void main(String[] args) {
        DogPlaysUse dogPlaysUse = new DogPlaysUse();
        dogPlaysUse.playBall(new DogPlays());
        dogPlaysUse.likeToGoOut(new DogPlays());

        System.out.println("\n======The kitten began to perform=====\n" );

        CatPlaysUse catPlaysUse = new CatPlaysUse();
        catPlaysUse.playBall(new DogPlays());
        catPlaysUse.climbTree(new CatPlays());
        catPlaysUse.catchMouse(new CatPlays());

    }
}
  • The following is the test of Controller
package com.liu.susu.principle.segregation.example1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @FileName PetController
 * @Description
 * @Author susu
 * @date 2022-02-12
 **/
@Controller
@RequestMapping("/dog")
public class PetController {

    @Autowired
    @Qualifier("dog")
    private PetInterface petInterface;

    @Autowired
    @Qualifier("catPlays")
    private PetInterface petInterface2;

    @Autowired
    private DogPlaysUse dogPlaysUse;
    @Autowired
    private CatPlaysUse catPlaysUse;

    @RequestMapping("/hello")
    @ResponseBody
    public String hello(){
        dogPlay();
        catPlay();
        return "hello world!";
    }
    public void dogPlay(){
        dogPlaysUse.playBall(petInterface);
        System.out.println("dogPlaysUse.playBall(petInterface)--->ok");
    }
    public void catPlay(){
        catPlaysUse.playBall(petInterface2);
        System.out.println("catPlaysUse.playBall(petInterface2)--->ok");
    }

}

2. Positive example code

package com.liu.susu.principle.segregation.example2;

/**
 * @Description Interface isolation principle - positive example
 * @Author susu
 * @date 2022-02-12
 **/
public interface PetInterface {
    void playBall();//Play ball
}
interface DogInterface {
    void likeToGoOut();//walk a dog
}
interface CatInterface {
    void climbTree();//climb up a tree
    void catchMouse();//Catch a mouse
}

class DogPlays implements PetInterface,DogInterface{
    public void playBall() {
        System.out.println("Dogs like playing with balls");
    }
    public void likeToGoOut() {
        System.out.println("Dogs like to be walked every day");
    }
}

class CatPlays implements PetInterface,CatInterface{
    public void playBall() {
        System.out.println("Kittens like to play ball");
    }
    public void climbTree() {
        System.out.println("Kittens like climbing trees");
    }
    public void catchMouse() {
        System.out.println("Kittens like to catch mice");
    }
}

class DogPlaysUse {
    public void playBall(PetInterface petInterface){
        petInterface.playBall();
    }
    public void likeToGoOut(DogInterface dogInterface) {
        dogInterface.likeToGoOut();
    }
}

class CatPlaysUse {
    public void playBall(PetInterface petInterface){
        petInterface.playBall();
    }
    public void climbTree(CatInterface catInterface) {
        catInterface.climbTree();
    }
    public void catchMouse(CatInterface catInterface) {
        catInterface.catchMouse();
    }
}

/**
 * Test class
 */
class Test{
    public static void main(String[] args) {
        DogPlaysUse dogPlaysUse = new DogPlaysUse();
        dogPlaysUse.playBall(new DogPlays());
        dogPlaysUse.likeToGoOut(new DogPlays());

        System.out.println("\n======The kitten began to perform=====\n" );

        CatPlaysUse catPlaysUse = new CatPlaysUse();
        catPlaysUse.playBall(new DogPlays());
        catPlaysUse.climbTree(new CatPlays());
        catPlaysUse.catchMouse(new CatPlays());

    }
}

Topics: Java Design Pattern Algorithm