首页 > Java > java教程 > 正文

里氏替换原则

PHPz
发布: 2024-08-25 22:30:36
原创
1018 人浏览过

Liskov Substitution Principle

对象应该可以替换为其子类型,而不影响代码的正确性

让我们通过继承来理解这一点(Is-a 关系)
例如:鸵鸟是鸟,驼背是汽车等

示例:赛车是一辆车

public class Car{
    public double getCabinWidth(){
        //return cabin width
    }
}
登录后复制
public class RacingCar extends Car{
    @Override
    public double getCabinWidth(){
        //UNIMPLEMENTED
    }
    public double getCockpitWidth(){
        //return the cockpit width of the racing car
    }
}
登录后复制

RacingCar 覆盖了汽车类的 getCabinWidth() 但保留它未实现因为赛车没有驾驶室宽度(如果你看到一级方程式赛车,它没有任何内部空间,它所拥有的只是一个驾驶员坐的驾驶舱)
因此赛车的内部空间被称为驾驶舱。
注意:赛车的一些规格可能与通用汽车不匹配

public class CarUtil{
    Car car1 = new Car();
    Car car2 = new Car();
    Car car3 = new RacingCar();

    List<Car> myCars = new ArrayList<>();
    myCars.add(car1);
    myCars.add(car2);
    myCars.add(car3);
    // this will not work in 3rd iteration, because the getCabinWidth() in RacingCar is not implemented 
    for(Car car  : myCars){
        System.out.println(car.getCabinWidth());
    }
}
登录后复制

这是一个已经公开的设计,因为 for 循环将在第三次迭代时失败。
为了解决这个问题,我们必须直击根源,即继承本身。


解决方案 1:(打破层次结构

我们必须打破继承,相反,我们将为 Car 和 RacingCar 提供一个共同的父对象

我们将创建一个非常通用的父类,名为 Vehicle

public class Vehicle{
    public double getInteriorWidth(){
        //return the interior width
    }
}
登录后复制
public class Car extends Vehicle{
    @Override
    public double getInteriorWidth(){
        return this.getCabinWidth();
    }
    public double getCabinWidth(){
        //return cabin width
    }
}
登录后复制
public class RacingCar extends Vehicle{
    @Override
    public double getInteriorWidth(){
        return this.getCockpitWidth();
    }

    public double getCockpitWidth(){
        //return the cockpit width of the racing car
    }
}
登录后复制
public class VehicleUtils{
    Vehicle vehicle1 = new Car();
    Vehicle vehicle2 = new Car();
    Vehicle vehicle2 = new RacingCar();

    List<Vehicle> vehicles = new ArrayList<>();
    vehicles.add(vehicle1);
    vehicles.add(vehicle2);
    vehicles.add(vehicle3);
    for(Vehicle vehicle : vehicles){
        System.out.println(vehicle.getInteriorWidth());
    } 
}
登录后复制

**打破层次结构:如果替换失败则打破层次结构


解决方案 2:只告诉不要问

我们再举一个亚马逊的例子
亚马逊对所有第三方产品提供 x 折扣。
并对所有自营产品提供1.5倍x优惠(Amazon Basics产品均为亚马逊自营产品)

public class Product{
    public double discount = 20;//x amount of discount on all the third-party products on Amazon
    public double getDiscount(){
        return discount;
    }
}
登录后复制
public class InHouseProduct extends Product{
    public void applyDiscount(){
        discount  = discount*1.5;// 1.5 times more discount on InHouseProducts
    }
}
登录后复制
public class PricingUtils{
    Product p1 = new Product();
    Product p2 = new Product();
    Product p2 = new InHouseProduct();
    List<Product> products = new ArrayList<>();
    products.add(p1);
    products.add(p2);
    products.add(p2);
    for(Product product : products){
        if(product instanceOf InHouseProduct){
            ((InHouseProduct)product).applyDiscount();
        }
        System.out.println(product.getDiscount());
    }
}
登录后复制

请注意,if 语句涉及更新 InHouseProduct 的折扣金额,这违反了里氏替换原则(因为我们应该能够用其子类型 InHouseProduct 替换对象 Product),但是这里在 if 语句中,我们手动更新折扣金额,这是不应该执行的。

对 InHouseProduct 类进行轻微修改即可解决此问题

public class InHouseProduct extends Product{

    @Override
    public double getDiscount(){
        applyDiscount();
        return discount;
    }
    public void applyDiscount(){
        discount  = discount*1.5;
    }
}
登录后复制

最后从 PricingUtils 类中删除 if 语句

public class PricingUtils{
    Product p1 = new Product();
    Product p2 = new Product();
    Product p2 = new InHouseProduct();
    List<Product> products = new ArrayList<>();
    products.add(p1);
    products.add(p2);
    products.add(p2);
    for(Product product : products){
        System.out.println(product.getDiscount());
    }
}
登录后复制

告诉不要问:这里我们告诉 utils 类打印所有折扣,并且 utils 类不必询问任何内容(因为它是通过 if 来询问的)之前声明)


以上是里氏替换原则的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:dev.to
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!