Mkdir700's Note

Mkdir700's Note

架构设计

SOLID 原则详解

SOLID principles are essential in object-oriented design to make software more flexible, maintainable, and scalable. The Single Responsibility Principle (S) emphasizes that a class should have only one reason to change, illustrated by separating responsibilities in code. The Open/Closed Principle (O) advocates for extending rather than modifying existing code, demonstrated through polymorphism in shape calculations. The Liskov Substitution Principle (L) highlights the importance of substitutability in inheritance, showcased by maintaining consistency in shape areas. The Interface Segregation Principle (I) stresses the importance of specialized interfaces, preventing unnecessary method implementations. Lastly, the Dependency Inversion Principle (D) emphasizes decoupling high-level and low-level modules through abstractions, promoting code reusability and flexibility.
115
0
0
2025-03-14

常见设计模式 Python 实现

该文章介绍了三种常见的设计模式在Python中的实现,分别是单例模式(Singleton Pattern)、工厂模式(Factory Pattern)、观察者模式(Observer Pattern)。在单例模式中,通过实现一个`DatabaseConnectionPool`类来管理数据库连接池实例,确保全局唯一连接池,并支持获取和释放连接的功能。工厂模式则是设计一个`FileParser`抽象基类和具体解析器类,以及一个`ParserFactory`工厂类,根据文件扩展名创建对应的解析器。最后,在观察者模式中,通过实现`Subject`接口、`StockMarket`类管理股票价格并通知观察者,包括`EmailNotifier`、`SMSNotifier`和`AppNotifier`三种不同类型的观察者。这些设计模式为软件架构提供了灵活、可维护和可扩展的设计解决方案。
101
0
0
2025-03-14