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

wufei1232024-09-08java2

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

概念 接口

接口定义类必须实现的契约或一组方法和属性。接口用于确保类遵循某种格式,但它们不提供方法的实现,仅提供方法的签名。

每当一个类实现一个接口时,它就会签署该接口的所有契约(方法和属性)。每个属性和方法都是强制实现的。

坚硬的

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

关于lsp

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

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

因此建议派生类应该尽可能接近基类,这样派生类就可以在代码不做任何修改的情况下替换其基类。

该原则由 barbara liskov 于 1988 年基于数据抽象和类型理论提出。源自契约式设计 (dbc) 的概念,由 bertrand meyer 于 1986 年推广。

这一原则的另一个具体说明是:

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

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

实际应用

考虑一个带有 fly() 方法的 bird 基类,该方法将在两个子类中使用:sparrow 和 ostrich。

文件: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!

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 中应用“里氏替换原则”的详细内容,更多请关注知识资源分享宝库其它相关文章!

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。