Interface unit tests

The nature of the interface is the contract (contract), both visible.

And summing the average number of array

Different types of arrays, different methods

using System;
using System.Collections;

namespace March9
{
    internal class Program
    {
        public static void Main(string[] args)
        {
            int[] nums1 = new int[] {1, 2, 3, 4, 5};
            ArrayList nums2 = new ArrayList {1, 2, 3, 4, 5};
            Console.WriteLine(Sum(nums1));
            Console.WriteLine(Avg(nums1));
        }

        static int Sum(int [] nums)
        {
            int sum = 0;
            foreach (var n in nums)
            {
                sum += n;
            }
            return sum;
        }

        static double Avg(int[] nums)
        {
            int sum = 0;
            double count = 0;
            foreach (var n in nums)
            {
                sum += n;
                count++;
            }
            return sum / count;
        }
        
    }
}

Use Interface

using System;
using System.Collections;

namespace March9
{
    internal class Program
    {
        public static void Main(string[] args)
        {
            int[] nums1 = new int[] {1, 2, 3, 4, 5};
            ArrayList nums2 = new ArrayList {1, 2, 3, 4, 5,6};//ArrayList,array继承自IEnumerable接口
            Console.WriteLine(Sum(nums2));
            Console.WriteLine(Avg(nums2));
        }

        static int Sum(IEnumerable nums)
        {
            int sum = 0;
            foreach (var n in nums)
            {
                sum +=(int) n;
            }
            return sum;
        }

        static double Avg(IEnumerable nums)
        {
            int sum = 0;
            double count = 0;
            foreach (var n in nums)
            {
                sum += (int) n;
                count++;
            }
            return sum / count;
        }
        
    }
}

Interface is a loosely coupled born, to facilitate the function of the alternative,

using System;
using System.Collections;

namespace March9
{
    internal class Program
    {
        public static void Main(string[] args)
        {
            var user=new User(new VIVO());//实现用户使用不同的手机打电话,发信息
            user.Userphone();
        }

    }

    class User
    {
        private IPhone _phone;
        public User(IPhone phone)//接收接口类型的变量
        {
            _phone = phone;
        }
        public void Userphone()
        {
            _phone.Dial();
            _phone.Pickup();
            _phone.Send();
            _phone.Receive();
        }
        
    }


    interface IPhone
    {
        void Dial();//拨号
        void Pickup();//接听
        void Send();//发送
        void Receive();//接收
    }

    class NokiaPhone:IPhone
    {
        public void Dial()
        {
            Console.WriteLine("Nokia is calling");
        }
        public void Pickup()
        {
            Console.WriteLine("hello,tim");
        }
        public void Send()
        {
            Console.WriteLine("Nokia is ring");
        }
        public void Receive()
        {
            Console.WriteLine("hello,maray");
        }
    }
    
    class VIVO:IPhone
    {
        public void Dial()
        {
            Console.WriteLine("VIVO is calling");
        }
        public void Pickup()
        {
            Console.WriteLine("hello");
        }
        public void Send()
        {
            Console.WriteLine("Vivo is send");
        }
        public void Receive()
        {
            Console.WriteLine("Vivo is receiving");
        }
    }
    
    
}

Language for object-oriented design built-in support: Dependency Inversion, interface isolation, opening and closing the principle ......

Dependency Inversion

Here again it is dependent, above the driver is following the car. Driver type of car there is a field, they are tightly coupled. Each individual in this case can only open the corresponding car, other cars can not open

Driver in the field is no longer the type of car and other vehicles, but a base interface, run a variety of methods to call the car, there has been Dependency inversion.

When the user and the service provider of multiple services have followed an interface, it can be more match. Through the interface as a bridge to achieve different instances of the method call.

Interface Segregation

Contract: Party (derived class) to be no more, B (Interface) to no less. If there has been no call, it shows that he is superfluous, is a fat interface.
Single Responsibility Principle, a class of only one thing.
Example: let a man open a variety of vehicles

using System;

namespace March10
{
    internal class Program
    {
        public static void Main(string[] args)
        {
            var a=new Driver(new Car());//此时car类和Truck都能开汽车,因为他继承自IVhicle接口,如果我想开坦克呢?那么就要改变Driver引用实例的变量的类型。
            a.Drive();
        }
    }


    class Driver
    {
        private IVehicle _vehicle;//此时,IVehicle类型的变量能引用继承自IVhicle接口实例的方法;ITank类型的变量能引用继承自ITank接口的实例的方法

        public Driver(IVehicle vehicle)
        {
            _vehicle = vehicle;
        }

        public void Drive()
        {
            _vehicle.Run();
        }


    }

    interface IVehicle
    { 
        void Run();
    }

    class Car:IVehicle
    {
        public void Run()
        {
            Console.WriteLine("car is runnning!");
        }
    }

    class Truck:IVehicle
    {
        public void Run()
        {
            Console.WriteLine("truck is running!");
        }
    }

    interface ITank
    {
        void Fire();
        void Run();
    }

    class LightTank:ITank
    {
        public void Fire()
        {
            Console.WriteLine("开炮");
        }

        public void Run()
        {
            Console.WriteLine("ka……ka……ka");
        }
    }
}

This method of exchange for the above exchange for a lot of trouble, the problem is the interface pass in the fat, fire () method with less, because of different functions, I just want to drive, rather than fire do other things.
The fire () method to do a single interface, and then let ITank interface inherits two base interface. The interface is multiple inheritance, and classes can not.

using System;

namespace March10
{
    internal class Program
    {
        public static void Main(string[] args)
        {
            var a=new Driver(new Car());
            a.Drive();
        }
    }


    class Driver
    {
        private IVehicle _vehicle;

        public Driver(IVehicle vehicle)
        {
            _vehicle = vehicle;
        }

        public void Drive()
        {
            _vehicle.Run();
        }


    }

    interface IVehicle
    { 
        void Run();
    }
    interface Single
    { 
        void Fire();
    }

    class Car:IVehicle
    {
        public void Run()
        {
            Console.WriteLine("car is runnning!");
        }
    }

    class Truck:IVehicle
    {
        public void Run()
        {
            Console.WriteLine("truck is running!");
        }
    }

    interface ITank:IVehicle,Single
    {
    }

    class LightTank:ITank
    {
        public void Fire()
        {
            Console.WriteLine("开炮");
        }

        public void Run()
        {
            Console.WriteLine("ka……ka……ka");
        }
    }
}

At this point, we call ITank interface, you can just focus on the run IVhicle method inherited from the inside, Driver only run method calls, the caller will not serve more than to be.
The question again, if we changed the Driver class private ITank _vehicle; variable type constructor changes, so will find, run method car and truck classes can not be called, because too much use of the interface, and should pass small interface.

Guess you like

Origin www.cnblogs.com/lpxspring/p/12448049.html