Interface segregation principle of object-oriented principle of six --ISP

ISP = Interface Segregation Principle
 
ISP is defined as follows:
1, the client should not rely on his unwanted interfaces
2, a class to another class in dependence should be based on the smallest interface
3, different interface should not be combined together to form a bloated large interface, which is contamination of the interface
4, the use of multiple specialized interface than using a single interface to better overall
 
 
Several principles for the use of ISP
1, according to the principle of splitting the interface when the interface isolation, must first meet the single responsibility principle: no perfect design takes into account all design principles, it is possible to conflicts between some design principles, just as the single responsibility principle and the principle of isolating interfaces , a consideration is the responsibility of the unity of the interface, a design consideration is the method of professional (as little as possible), it will certainly be conflict. In case of conflict, as far as possible in a single main responsibility, of course, we have to consider the specific situation.
2, to improve the high polymer: improved interfaces, classes, the processing capability of the module, to reduce external interaction. For example, you submit an order to the killer, asked him to kill a person within a week, a week after the killer to complete the task, to complete the task without conditions this performance is the high cohesion. Specifically, that is: as little as possible in the interface requires public announcement method, the interface is external commitments, the less the more beneficial to the development of the system, the smaller the risk of change commitments, but also help reduce costs.
3, customized services: providing excellent service separately (Visitors need only approach) as an individual.
4, interface design there's a limit: judgment based on experience
 
 
Interface isolation single responsibility principle and the principle that both sides of a coin, they say is one thing.
Only the interface segregation principle is standing service the caller perspective problem, a single principle duty is to stand in the perspective of the service provider's point of view.
 
 
===========================================================================================
Compact is how it happened?
Contract that is to say two things, I would not want to say that Party in the contract, the party will say that I will not give less in the Deed.
Party B is not less to be relatively easy to do, because when a class implements an interface, he must be present in the interface all methods to achieve. If you do not want to realize all the ways that you left abstract method, then you this class is an abstract class can not be instantiated, that you are not a full service provider. So the party will not give less, it is mandatory.
If the party no more flexible rules, he is a thing of the design, we need to use some design principles to discipline and control everyone to write code. Because the compiler is capable of detecting to B is not less, not checked out to the party is not much.
 
So how do I know that the Party had no more to do? Very simple, just look passed to the caller interface type, there has not been no calls to the function member, if there is, it shows the type of interface passed in too much (too fat), in other words is this fat Interface interface is a merger of two or more different nature of smaller interfaces up, passed in the big port, only part of the interface to be called, on another part of the excess out.
Depending on the cause of interfaces to produce fat, the bad consequences of violation of the principle of the interface isolation may bring There are basically two:
1, the first case, the design of a problem, the interface contains too many features to come, and that part of the function of which there must never have to use, the interface is naturally a violation of the principle of isolation.
  We look at an example:
   Scene description: a pair of young lovers day girls to boys called and told him the car was rear-ended, cry Ewha rain. Small boys high emotional intelligence, to coax the little girl said it does not matter, tomorrow you buy a tank, not afraid of a rear-end collision. (Provided that little girls can not be fired, only to open ~ ~ ~ ~)
 
 
   The first edition of realization = "little girl will only drive a car, do not open other
  
#region 车辆接口和实现

    interface IVehicle
    {
        void Run();
    }

    class Car : IVehicle
    {
        public void Run()
        {
            Console.WriteLine("Car is Running");
        }
    }
    class Truck : IVehicle
    {
        public void Run()
        {
            Console.WriteLine("Truck is Running");
        }
    }

    #endregion

  Driver categories:

class Driver
    {
        private IVehicle _vehicle;
        public Driver(IVehicle vehicle)
        {
            _vehicle = vehicle;
        }
        public void Drive()
        {
            _vehicle.Run();
        }
    }

Caller to the service:

   var Driver = new new Driver ( new new Car ()); // open car 
            Driver = new new Driver ( new new Truck ()); // open truck 
            driver.Drive (); 

            // this time you will find that if you want a little girl open tanks, it is now can not meet
             // because Driver configuration parameter passing is IVehicle the interface, the interface is not ITank
             // If you want to meet girls desire to open the tanks to the streets, we must transform Driver, passing ITank interfaces, see the next example of 

            the Console.ReadKey ();

  The second edition of the realization = "little girl can open tanks, but not able to open the car

      

 class Driver
    {
        private ITank _tank;
        public Driver(ITank tank)
        {
            _tank = tank;
        }
        public void Drive()
        {
            _tank.Run();
        }
    }
   var Driver = new new Driver ( new new HeavyTank ()); // open tanks 
            driver.Drive (); 

            // this time you will find that little girls can develop tanks to the streets, but you will find that little girl now only open tanks, and not drive the
             // problem where is it?
            // we put a fat Interface (ITank) is passed in, this interface has a fat we never have to use the function, is fire.
            // So now this design is a violation of the principle of isolation interface
             // specific transformation look at the next example 

            Console.ReadKey ();

 

   The realization of the third edition = "line with the principles of the interface isolation, little girls able to open the tank, can carve a car.
#region 车辆接口和实现

    interface IVehicle
    {
        void Run();
    }

    class Car : IVehicle
    {
        public void Run()
        {
            Console.WriteLine("Car is Running");
        }
    }
    class Truck : IVehicle
    {
        public void Run()
        {
            Console.WriteLine("Truck is Running");
        }
    }

    #endregion
interface IWeapon
    {
        void Fire();
    }
  interface ITank:IVehicle,IWeapon
    {
    }
    class LightTank : ITank
    {
        public void Fire()
        {
            Console.WriteLine("Boom!");
        }

        public void Run()
        {
            Console.WriteLine("Ka Ka Ka!");
        }
    }

    class HeavyTank : ITank
    {
        public void Fire()
        {
            Console.WriteLine("Boom!!!!!!!!");
        }

        public void Run()
        {
            Console.WriteLine("Ka!!! Ka!!!! Ka!!!!!!");
        }
    }

Driver categories:

  class Driver
    {
        private IVehicle _vehicle;
        public Driver(IVehicle vehicle)
        {
            _vehicle = vehicle;
        }
        public void Drive()
        {
            _vehicle.Run();
        }
    }

Caller to the service:

      // principles of interface isolation is a service of the caller does not have to
             // this example, the caller's demand service is very simple, which is a requirement would run, does not require fire
             // therefore the original ITank own interfaces contained fire and run on the rules in line with fat interface, he provided extra interfaces to the caller
             // So the ITank the interface is isolated from the 
            var Driver = new new Driver ( new new HeavyTank ()); // open tanks 
            driver.Drive () ; 
            Driver = new new Driver ( new new car ()); // drive a car 
            driver.Drive (); 


            Console.ReadKey ();

 

 The second case continues tomorrow, wow ha ha ~~~~~~~~~~
 

 

Guess you like

Origin www.cnblogs.com/schangxiang/p/11223694.html