Six principles of software design patterns Four Interface Segregation Principle (PHP language)

Reprinted, the original link https://www.cnblogs.com/zhanghengscnc/p/8299459.html

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: A Class B Class dependent via the interface I, via an interface class I C D-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: bloated interface I split into several independent interfaces, classes A and C were established dependency with an interface they need. That is, using the interface segregation principle.

To illustrate the principle of isolation interfaces:

(A non-conforming interfaces spacer design principles)

This figure means that: the method of interface I dependent class A 1, Tier 2, Tier 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 there with (that is, the method shown in red font tag) the method could be, 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 function method1();
    public function method2();
    public function method3();
    public function method4();
    public function method5();
}

class A
{
    public function depend1(I $i)
    {
        $i->method1();
    }

    public function depend2(I $i)
    {
        $i->method2();
    }

    public function depend3 (I $ I ) 
    { 
        $ I -> the method3 (); 
    } 
} 

class B the implements I 
{ 
    public  function the method1 () 
    { 
        Print ( "Class B-implemented method of interface I. 1" ); 
    } 

    public  function method2 () 
    { 
        Print ( "method B implements an interface class I, 2" ); 
    } 

    public  function the method3 () 
    { 
        Print ( "method B implements an interface class I, 3" ); 
    } 

    // class B is, method4 not required and method5 , but there are two methods A due to the interface, 
    // implementation process so even though the two methods the method body is empty, but also the role of these two methods do not achieve. 
    public  function method4 () {}
     public  function method5 () {} 
} 

class C 
{ 
    public  function depend1 (the I $ I ) 
    { 
        $ I -> the method1 (); 
    } 

    public  function depend2 (the I $ I ) 
    { 
        $ I -> method4 (); 
    } 

    public  function depend3 (the I $ I ) 
    { 
        $ I -> method5 (); 
    } 
} 

classD the implements I 
{ 
    public  function the method1 () 
    { 
        Print ( "D-implemented method of interface I 1" ); 
    } 
    // for class D is, and method2 method3 not necessary, but because the interface has two methods A , 
    // so in the implementation process be achieved even if the method body is empty of these two methods, the two methods have no effect. 
    public  function method2 () {}
     public  function the method3 () {} 

    public  function method4 () 
    { 
        Print ( "Method D-implemented interface I of. 4" ); 
    } 

    public  function method5 () 
    { 
        Print ( "D-implemented interface I of method. 5 " ); 
    } 
} 

$ A = new new A ();
 $ A -> depend1 (new B());
$a->depend2(new B());
$a->depend3(new B());

$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 split the original interface I three interfaces, the design shown in Figure 2 after split:

(FIG. 2 following the design principles of the interface isolation)

As usual, posted code of the program, are not familiar with the class diagram for the reference to a friend:

interface I1
{
    public function method1();
}

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

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

class A
{
    public function depend1(I1 $i)
    {
        $i->method1();
    }

    public function depend2(I2 $i) 
    { 
        $ I -> method2 (); 
    } 
    public  function depend3 (I2 $ I ) 
    { 
        $ I -> the method3 (); 
    } 
} 

class B the implements I1, I2 
{ 
    public  function the method1 () 
    { 
        Print ( "Class B implements the interface the method of I1. 1 " ); 
    } 

    public  function method2 () 
    { 
        Print (" method B class implement the interface I2 2 " ); 
    } 

    public  function the method3 () 
    { 
        Print("类B实现接口I2的方法3");
    }
}

class C
{
    public function depend1(I1 $i)
    {
        $i->method1();
    }

    public function depend2(I3 $i)
    {
        $i->method4();
    }

    public function depend3(I3 $i)
    {
        $i->method5();
    }
}

class D implements I1, I3
{
    public functionthe method1 () 
    { 
        Print ( "Method D-implement interface I1 is. 1" ); 
    } 

    public  function method4 () 
    { 
        Print ( "Method D-implement interface I3,. 4" ); 
    } 

    public  function method5 () 
    { 
        Print ( "Class method D implement interface I3,. 5 " ); 
    } 
} 

$ a = new new a ();
 $ a -> depend1 ( new new B ());
 $ a -> depend2 ( new new B ());
 $ a -> depend3 ( new new B ()); 

$ C = new new C ();
 $ C -> depend1 (new D());
$c->depend2(new D());
$c->depend3(new D());

 

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:

  • Interface as small as possible, but there's a limit. The interfaces can be refined to improve the flexibility of the fact that programming is not earned, but if it is too small, it will cause excessive number of interfaces, complicating the design. So be sure to moderate.
  • Customized services to dependent interface, only expose the class to call the method requires it, and it does not require the method is hidden. Only focus on providing customized services for a module in order to establish the minimum dependencies.
  • Improve cohesion and reduce the external interaction. The interfaces with the least method to accomplish most things.

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/shamohai/p/11096406.html