1.设计模式-七大设计原则

1.1 单一职责原则(Single Responsibility Principle, SRP)

定义:一个类或方法仅有一个引起变化的动因,其职责应聚焦于单一业务维度

代码示例

class TrafficTool {
    public void run(String tool) {
        System.out.println(tool + "在公路上跑");
    }
}

// 符合 SRP 的示例:拆分职责到不同类
class RoadTrafficTool {
    public void run(String tool) {
        System.out.println(tool + "在公路上跑");
    }
}

class AirTrafficTool {
    public void run(String tool) {
        System.out.println(tool + "在天空飞行");
    }
}

1.2 开闭原则(Open-Closed Principle, OCP)

定义:软件实体应对扩展开放,对修改关闭。

代码示例

// 违反 OCP 的示例:直接修改原有图形类
class GraphicEditor {
    public void drawShape(Shape s) {
        if (s.type == 1) drawRectangle();
        else if (s.type == 2) drawCircle(); // 新增图形需修改此处
    }
}

// 符合 OCP 的示例:通过抽象扩展
abstract class Shape {
    public abstract void draw();
}

class Circle extends Shape {
    @Override
    public void draw() { System.out.println("绘制圆形"); }
}

class Triangle extends Shape {
    @Override
    public void draw() { System.out.println("绘制三角形"); }
}

1.3 里氏替换原则(Liskov Substitution Principle, LSP)

定义:子类应能替换父类且不影响程序正确性。

代码示例

// 违反 LSP 的示例:正方形继承长方形导致逻辑冲突
class Rectangle {
    void setWidth(int w) { ... }
    void setHeight(int h) { ... }
}

class Square extends Rectangle {
    @Override
    void setWidth(int w) { 
        super.setWidth(w);
        super.setHeight(w); // 破坏父类行为
    }
}

// 符合 LSP 的示例:通过接口定义独立行为
interface Shape { double area(); }

class Rectangle implements Shape { /* 实现矩形面积计算 */ }

class Square implements Shape { /* 实现正方形面积计算 */ }

1.4 接口隔离原则(Interface Segregation Principle, ISP)

定义:客户端不应依赖不需要的接口。

代码示例:

// 违反 ISP 的示例:臃肿的动物接口
interface Animal {
    void eat();
    void fly();
    void swim();
}

// 符合 ISP 的示例:拆分接口
interface Eatable { void eat(); }
interface Flyable { void fly(); }
interface Swimmable { void swim(); }

class Bird implements Eatable, Flyable { ... }
class Fish implements Eatable, Swimmable { ... }

1.5 依赖倒置原则(Dependency Inversion Principle, DIP)

定义:高层模块不依赖底层模块,二者均依赖抽象。

代码示例:

// 违反 DIP 的示例:直接依赖具体数据库
class OrderService {
    private MySQLDatabase db;
    public OrderService() { this.db = new MySQLDatabase(); }
}

// 符合 DIP 的示例:依赖抽象接口
interface Database {
    void save(Order order);
}

class OrderService {
    private Database db;
    public OrderService(Database db) { this.db = db; }
}

class MySQLDatabase implements Database { ... }

1.6 迪米特法则(Law of Demeter, LoD)

定义:类应减少对其他类的依赖,仅与直接朋友交互。

代码示例

// 违反 LoD 的示例:领导类直接访问课程类
class Leader {
    public void countCourses(Employee e) {
        List<Course> courses = e.getCourses();
        System.out.println(courses.size());
    }
}

// 符合 LoD 的示例:通过中间层封装
class Employee {
    public int countCourses() { ... }
}

class Leader {
    public void countCourses(Employee e) {
        System.out.println(e.countCourses());
    }
}

1.7 合成复用原则(Composite Reuse Principle, CRP)

定义:优先使用组合/聚合,而非继承。

代码示例

// 违反 CRP 的示例:通过继承复用引擎功能
class Car extends Engine { ... }

// 符合 CRP 的示例:通过组合实现复用
class Car {
    private Engine engine;
    public Car(Engine engine) { this.engine = engine; }
    void start() { engine.start(); }
}

interface Engine { void start(); }
class ElectricEngine implements Engine { ... }
posted @ 2025-04-12 10:10  雾里看花的少年  阅读(16)  评论(0)    收藏  举报