首页 > 图灵资讯 > 技术篇>正文

在 Typescript 和 Java 中应用“里氏替换原则”

2024-09-04 20:19:39

在 typescript 和 java 中应用“里氏替换原则”

概念 接口

界面定义类必须实现的合同或一组方法和属性。界面用于确保类遵循某种格式,但它们不提供方法的实现,只提供方法的签名。 当一个类别实现一个接口时,它将签署该接口的所有合同(方法和属性)。强制实现每个属性和方法。

坚硬的

solid 它是一个代表面向对象编程的五个基本原则的缩写词 robert c. martin(鲍勃叔叔)提出。在这里,您可以阅读更多关于他文章的信息。 这些原则旨在改进代码的结构和维护,使其更加灵活、可扩展和易于理解。这些原则可以帮助程序员创建更有组织的代码,划分职责,减少依赖,简化重构过程,促进代码重用。

关于lsp

缩写中的“l代表“里氏替换原理”。 bob叔叔用来定义这一原则的一句话是:

“派生类必须能够完全替换基类”

因此,建议派生类尽可能接近基类,这样派生类就可以在代码没有任何修改的情况下替换基类。 该原则由 barbara liskov 于 1988 提出了基于数据抽象和类型理论的年份。来自合同设计 (dbc) 的概念,由 bertrand meyer 于 1986 年推广。 这一原则的另一个具体说明是:

子类型应该用作你的基本类型,没有任何意外。

在编程中,变化和事故可能会导致问题。如果需要更换系统功能,新功能必须提供相同类型的信息,否则系统可能会出现故障。确保类别 s 具有与基类 t 同样的行为必须使用合同(界面或抽象类)来定义实现新功能的强制性方法,以确保类别 s 完整性和相似性之间的相似性 t 类。

实际应用

考虑一个带 fly() 方法的 bird 该方法将用于两个子类:sparrow 和 ostrich。

立即学习“Java免费学习笔记(深入);

文件:bird.java

class bird {
    void fly() {
        system.out.println("i can fly!");
    }
}

class sparrow extends bird {
    // herda o comportamento de 'fly' da classe 'bird'
}

class ostrich extends bird {
    @override
    void fly() {
        throw new unsupportedoperationexception("i cannot fly");
    }
}

文件:bird.ts

class bird {
  fly() {
    console.log("i can fly!");
  }
}

class sparrow extends bird {}

class ostrich extends bird {
  fly() {
    throw new error("i cannot fly");
  }
}

遇到的问题

在这里,sparrow 类遵循 lsp,因为麻雀真的能飞。然而,ostrich 类违反了 lsp,因为它以从根本上改变其行为的方式重写 fly() 打破了方法 bird 设置类别的期望。

如何修复?

我们需要通过将军 sparrow 和 ostrich 每个类别的特殊性分为合同(接口或抽象类,我将在这里使用接口) lsp,他们必须签署这些合同来调整每个类别的行为:

文件:bird.java

interface bird {
    string getname();
    void makesound();
}

interface flyingbird extends bird {
    void fly();
}

class sparrow implements flyingbird {
    private string name;

    public sparrow(string name) {
        this.name = name;
    }

    @override
    public string getname() {
        return this.name;
    }

    @override
    public void makesound() {
        system.out.println("chirp chirp!");
    }

    @override
    public void fly() {
        system.out.println(this.name + " is flying!");
    }
}

class ostrich implements bird {
    private string name;

    public ostrich(string name) {
        this.name = name;
    }

    @override
    public string getname() {
        return this.name;
    }

    @override
    public void makesound() {
        system.out.println("boom boom!");
    }
}

public class main {
    public static void main(string[] args) {
        sparrow sparrow = new sparrow("little sparrow");
        sparrow.makesound(); // chirp chirp!
        sparrow.fly(); // little sparrow is flying!

        ostrich ostrich = new ostrich("ostrich");
        ostrich.makesound(); // boom boom!
        ostrich.fly(); // error: method 'fly' does not exist on 'ostrich'
    }
}

文件:bird.ts

interface Bird {
  name: string;
  makeSound(): void;
}

interface FlyingBird extends Bird {
  fly(): void;
}

class Sparrow implements FlyingBird {
  name: string;

  constructor(name: string) {
    this.name = name;
  }

  makeSound() {
    console.log("Chirp chirp!");
  }

  fly() {
    console.log(`${this.name} is flying!`);
  }
}

class Ostrich implements Bird {
  name: string;

  constructor(name: string) {
    this.name = name;
  }

  makeSound() {
    console.log("Boom boom!");
  }
}

const sparrow = new Sparrow("Little Sparrow");
sparrow.makeSound(); // Chirp chirp!
sparrow.fly(); // Little Sparrow is flying!
sparrow.fly(); // Little Sparrow is flying!

const ostrich = new Ostrich("Ostrich");
ostrich.makeSound(); // Boom boom!
ostrich.fly(); // Error: Method 'fly' does not exist on 'Ostrich'

分析

更正说明 bird interface:定义所有鸟类的共同行为,如makesound()。所有鸟类都必须实现这个界面。

flyingbird 接口:继承自 bird 并添加 fly() 这种行为是针对会飞的鸟类的。

sparrow 类:实现 flyingbird 接口,因为麻雀可以飞。这种行为被定义为发出声音和飞行。

鸵鸟:只实现 bird 因为鸵鸟不会飞,所以没有这样的界面。 fly() 因此,方法不违反 lsp。

结论

lsp 对于确保代码模块化、可重用、易于维护至关重要。 lsp 在引入新子类或修改现有子类时,违规行为可能会导致脆弱的代码中断,因为这可能会导致一些依赖超级代码的意外行为。 对于开闭原则,子类型替换允许模块在不修改的情况下扩展 (ocp) 提供的灵活性非常重要,而里氏替换原则使之成为可能。合同(通过接口或抽象实现)对安全设计至关重要,但程序员必须充分理解它们,以帮助避免遗留软件中的常见错误。他们还提供了相关信息如何实施和使用代码的宝贵指导,只需遵守相关合同即可。

实际意义
  1. 在设计子类时,确保它们可以在任何地方使用,而不会引入错误或需要特殊处理。
  2. 因为这可能会导致维护问题和意外错误,所以避免创造违反超类预期的子类。

理解和应用里氏替换原则可以帮助开发人员创建更可预测和稳定的面向对象系统。

以上就是在 Typescript 和 Java 请关注图灵教育的其他相关文章,以应用“里氏替换原则”的详细内容!

上一篇 项目 检查参数有效性
下一篇 返回列表

文章素材均来源于网络,如有侵权,请联系管理员删除。