接口隔离原则
客户端不应该依赖它不需要的接口;一个类对另一个类的依赖应该建立在最小的接口上。
using System;
namespace LspExample
{
class Program
{
static void Main(string[] args)
{
var driver = new Driver(new LightTank());
//var driver = new Driver(new Car());
driver.Drive();
}
}
class Driver
{
private IVehicle _vehicle; //最小依赖
public Driver(IVehicle vehicle)
{
_vehicle = vehicle;
}
public void Drive()
{
_vehicle.Run();
}
}
interface IWeapon
{
void Fire();
}
interface IVehicle
{
void Run();
}
//interface ITank
//{
// void Fire();
// void Run();
//}
interface ITank : IVehicle, IWeapon
{
}
class Car : IVehicle
{
public void Run()
{
Console.WriteLine("Car is running...");
}
}
class Truck : IVehicle
{
public void Run()
{
Console.WriteLine("Truck is running...");
}
}
class LightTank : ITank
{
public void Fire()
{
Console.WriteLine("Boom!");
}
public void Run()
{
Console.WriteLine("ka ka ka...");
}
}
class MediumTank : ITank
{
public void Fire()
{
Console.WriteLine("Boom!!");
}
public void Run()
{
Console.WriteLine("ka ka ka......");
}
}
}