Design Patterns six principles (four) - Interface Segregation Principle

Interface Segregation Principle


Interface for Java developers who are not familiar, it exists in almost every Java program, abstract synonymous. Speaking before the Interface Segregation Principle, first talk about the interface, the interface is divided into the following two:

实例接口(Object Interface): 在Java中声明一个类,然后用new关键字产生一个实例,是对一个类型的事物的描述,这就是一种接口。或许我们乍一看会有点懵,怎么和我们原来学习的接口不一样呢,其实我们这样想,我们都知道,在Java中有一个Class类,表示正在运行的类和接口,换句话说每一个正在运行时的类或接口都是Class类的对象,这是一种向上的抽象。接口是一种更为抽象的定义,类是一类相同事物的描述集合,那为什么不可以抽象为一个接口呢?

类接口(Class Interface): 这就是我们经常使用的用interface定义的接口

Parenthetically, the interface isolated interface in Java interface defined by the principles stated in the interface is not the narrow sense, but a broader concept, can be an interface, an abstract entity class or classes.


definition:

The client should not rely on it does not interface; a class dependent on another class should be based on the smallest interface.

The origin of the problem:

Class A class I dependent via the interface B, C, D via the interface class I dependent, if the interface I for classes A and B interfaces is not the smallest, the Class B and D must be a method to realize they do not need.

solution:

The bloated interface I into separate several interfaces, classes A and C, respectively, to establish an interface dependencies they need. That is, using the interface segregation principle.

For example:

A class I dependent interfaces Method 1, Method 2, Method 3, class A and B is implemented by a class dependent. C Class I-dependent interfaces Method 1, Method 4, Method 5, D-C is implemented by a class dependent. For class B and class D, although they are less than the method exists, but implements the interface I, so it must be to achieve less than these methods. FIG unfamiliar class may be understood with reference to the program code, as follows:

interface I {
    public void method1();
    public void method2();
    public void method3();
    public void method4();
    public void method5();
}

class A{
    public void depend1(I i){
        i.method1();
    }
    public void depend2(I i){
        i.method2();
    }
    public void depend3(I i){
        i.method3();
    }
}

class B implements I{
    public void method1() {
        System.out.println("类B实现接口I的方法1");
    }
    public void method2() {
        System.out.println("类B实现接口I的方法2");
    }
    public void method3() {
        System.out.println("类B实现接口I的方法3");
    }
    //对于类B来说,method4和method5不是必需的,但是由于接口A中有这两个方法,
    //所以在实现过程中即使这两个方法的方法体为空,也要将这两个没有作用的方法进行实现。
    public void method4() {}
    public void method5() {}
}

class C{
    public void depend1(I i){
        i.method1();
    }
    public void depend2(I i){
        i.method4();
    }
    public void depend3(I i){
        i.method5();
    }
}

class D implements I{
    public void method1() {
        System.out.println("类D实现接口I的方法1");
    }
    //对于类D来说,method2和method3不是必需的,但是由于接口A中有这两个方法,
    //所以在实现过程中即使这两个方法的方法体为空,也要将这两个没有作用的方法进行实现。
    public void method2() {}
    public void method3() {}

    public void method4() {
        System.out.println("类D实现接口I的方法4");
    }
    public void method5() {
        System.out.println("类D实现接口I的方法5");
    }
}

public class Client{
    public static void main(String[] args){
        A a = new A();
        a.depend1(new B());
        a.depend2(new B());
        a.depend3(new B());
        
        C c = new C();
        c.depend1(new D());
        c.depend2(new D());
        c.depend3(new D());
    }
} 

We can see, if the interface is too bloated, as long as the method appears interface, regardless of its dependent class has no use, implementation class must to implement these methods, which is obviously not a good design. If this design modified to comply with the principles of the interface isolation, it must be split interfaces I. Here we have the original interface I split into three interfaces:

interface I1 {
    public void method1();
}

interface I2 {
    public void method2();
    public void method3();
}

interface I3 {
    public void method4();
    public void method5();
}

class A{
    public void depend1(I1 i){
        i.method1();
    }
    public void depend2(I2 i){
        i.method2();
    }
    public void depend3(I2 i){
        i.method3();
    }
}

class B implements I1, I2{
    public void method1() {
        System.out.println("类B实现接口I1的方法1");
    }
    public void method2() {
        System.out.println("类B实现接口I2的方法2");
    }
    public void method3() {
        System.out.println("类B实现接口I2的方法3");
    }
}

class C{
    public void depend1(I1 i){
        i.method1();
    }
    public void depend2(I3 i){
        i.method4();
    }
    public void depend3(I3 i){
        i.method5();
    }
}

class D implements I1, I3{
    public void method1() {
        System.out.println("类D实现接口I1的方法1");
    }
    public void method4() {
        System.out.println("类D实现接口I3的方法4");
    }
    public void method5() {
        System.out.println("类D实现接口I3的方法5");
    }
} 

Meaning the interface segregation principle is: the establishment of a single interface, do not create bloated interface, try to refine the interfaces, interface methods as little as possible. In other words, we want to establish a dedicated interface for each class, rather than trying to build a very large interface for all classes that depend on it to call. Examples Herein, the interface is changed to a large three-specific interface is the interface used by the principle of isolation. In programming, dependent on a few specific interface than rely on a comprehensive interface more flexible. Interface is designed for external set of "contract", by defining multiple interfaces dispersed, can prevent the spread of exotic change, improve flexibility, and maintainability.

Here, many people will feel isolated interface very similar to the principle of single responsibility principle before, it is not.

First, the Single Responsibility Principle is the primary focus of duty; and the interface segregation principle focus isolation interfaces depend.

Second, the Single Responsibility Principle is mainly constrained class, followed by the interfaces and methods, it is aimed at program implementation and details; and the interface Interface Interface Segregation Principle The main constraint, mainly for abstraction, for the program to build the overall framework.

When using the interface segregation principle of interface constraints, to note the following:

接口尽量小,但是要有限度。对接口进行细化可以提高程序设计灵活性是不挣的事实,但是如果过小,则会造成接口数量过多,使设计复杂化。所以一定要适度。

为依赖接口的类定制服务,只暴露给调用的类它需要的方法,它不需要的方法则隐藏起来。只有专注地为一个模块提供定制服务,才能建立最小的依赖关系。

提高内聚,减少对外交互。使接口用最少的方法去完成最多的事情。

Use the interface segregation principle, must be appropriate, interface design is too large or too small is not good. Interface design time, only more time to think and plan in order to practice this principle accurately.

Guess you like

Origin www.cnblogs.com/jlutiger/p/11280646.html