单一职责原则(SRP)

就一个类而言,应该仅有一个引起它变化的原因,一个类应该只做一类事

开闭原则(OCP)

软件实体(类、模块、函数等)应该是可以扩展的,但是不可修改

违反OCP

  1. /**
  2. * 违反开闭原则设计
  3. */
  4. public class UnOcp {
  5. @Test
  6. public void testUnOcp() {
  7. Circle circle = new Circle();
  8. Square square = new Square();
  9. List list = new ArrayList();
  10. list.add(circle);
  11. list.add(square);
  12. for (Object o : list) {
  13. //如果多加一种三角形就需要多加一个else if 进行判断
  14. if (o instanceof Circle) {
  15. ((Circle) o).drawCircle();
  16. } else if (o instanceof Square) {
  17. ((Square) o).drawSquare();
  18. }
  19. }
  20. }
  21. class Circle {
  22. ShapeType itsType = ShapeType.circle;
  23. void drawCircle() {
  24. System.out.println("draw Circle");
  25. }
  26. }
  27. class Square {
  28. ShapeType itsType = ShapeType.square;
  29. void drawSquare() {
  30. System.out.println("draw Square");
  31. }
  32. }
  33. enum ShapeType{
  34. circle, square;
  35. }
  36. }

符合OCP

  1. /**
  2. * 满足开闭原则
  3. * 如果新增一种形状就新建一个类继承Shape,
  4. * 无需改动testOcp这个方法
  5. */
  6. public class Ocp {
  7. @Test
  8. public void testOcp() {
  9. Circle circle = new Circle();
  10. Square square = new Square();
  11. List<Shape> shapes = new ArrayList<>();
  12. shapes.add(circle);
  13. shapes.add(square);
  14. for (Shape shape : shapes) {
  15. shape.draw();
  16. }
  17. }
  18. abstract class Shape {
  19. public abstract void draw();
  20. }
  21. class Circle extends Shape{
  22. @Override
  23. public void draw() {
  24. System.out.println("draw Circle");
  25. }
  26. }
  27. class Square extends Shape {
  28. @Override
  29. public void draw() {
  30. System.out.println("draw Square");
  31. }
  32. }
  33. }

里式替换原则(LSP)

子类型必须能够替换掉他们的基本类型

  1. public class UnLsp {
  2. @Test
  3. public void testUnLsp() {
  4. testArea(new Rectangle());
  5. testArea(new Square());
  6. }
  7. private void testArea(Rectangle rectangle) {
  8. rectangle.setHeight(5);
  9. rectangle.setWidth(4);
  10. Assert.assertEquals(20, rectangle.getArea());
  11. }
  12. class Rectangle {
  13. private int width;
  14. private int height;
  15. public void setWidth(int width) {
  16. this.width = width;
  17. }
  18. public void setHeight(int height) {
  19. this.height = height;
  20. }
  21. public int getArea() {
  22. return width * height;
  23. }
  24. }
  25. class Square extends Rectangle{
  26. public void setWidth(int width) {
  27. super.setWidth(width);
  28. super.setHeight(width);
  29. }
  30. public void setHeight(int height) {
  31. super.setWidth(height);
  32. super.setHeight(height);
  33. }
  34. }
  35. }

依赖倒置原则(DIP)

高层模块不应该直接底层模块,二者都应该依赖抽象
抽象不应该依赖于细节,细节应该依赖于抽象

违反DIP

public class UnDip {
    private static final int THERMONTER = 0x86;
    private static final int HEATER = 0x87;
    private static final int ENGAGE = 1;
    private static final int DISENGATE = 0;

    public void regulate(double minTemp, double maxTemp) throws Exception {
        while(true) {
            while(in(THERMONTER) > minTemp) {
                wait(1);
            }
            out(HEATER, ENGAGE);

            while(in(THERMONTER) < maxTemp) {
                wait(1);
            }
            out(HEATER, DISENGATE);
        }
    }

    private int in(int thermonter) {
        return 0;
    }

    private void out(int heater, int engage) {
    }
}

符合DIP

/**
 * 依赖倒置原则
 * 高层模块不应该依赖于实现,
 * 应该依赖于抽象
 */
public class Dip {

    public void regulate(Thermometer  thermometer, Heater h, double minTemp, double maxTemp) throws Exception {
        while(true) {
            while(thermometer.read() > minTemp) {
                wait(1);
            }
            h.engage();

            while(thermometer.read() < maxTemp) {
                wait(1);
            }
            h.disEngage();
        }
    }
}

class Heater {
    public void engage() {

    }
    public void disEngage() {

    }
}

class Thermometer {
    public int read() {
        return 0;
    }
}