CS-Notes/notes/面向对象思想.md
2018-02-22 14:47:22 +08:00

7.5 KiB
Raw Blame History

S.O.L.I.D

S.O.L.I.D???????????????(OOD&OOP)?§Þ?????????????(Programming Priciple)?????????§Õ??

??§Õ ?? ???????
SRP The Single Responsibility Principle ??????????
OCP The Open Closed Principle ?????????
LSP The Liskov Substitution Principle ?????<3F>I???
ISP The Interface Segregation Principle ?????????
DIP The Dependency Inversion Principle ???????????

1. ??????????

???????????????????????????????????Ñö?????????????????????????¦²????????????§Ö?????????????¦Å????????????????

2. ?????????

?????????????????????????????????????????????????????????????

3. ?????<3F>I???

????????????????????<3F>I?¦Ê??????????????????????? is-a ?????

4. ?????????

?????????????????§»???????????????Ñö??????????????????????????????¨¢?

5. ???????????

  1. ?????öö?????????????ï…?????????????????
  2. ????????????????????????????????

???????§³????

???????§³???????????????????????

1. ???

???¨®??????????????????????????????????????<3F>q?????????????????ÈÉ??????????????????????????????????????????????????????§»????????????????????????????????????????????????????????????????????????????????

?????????????

  1. ??????????????????

  2. ????????????????????

  3. ??????????§Ú??????????

  4. ????????????????

???? Person ???? name??gender??age ???????????????? get() ?????????? Person ????? name ????? gender ????????????? age ????????? age ???????? work() ??????¨¢?

??? gender ??????? int ??????????§Õ›¥????????????????????????????????????????????????????????????????????????????????§³?

public class Person {
    private String name;
    private int gender;
    private int age;

    public String getName() {
        return name;
    }

    public String getGender() {
        return gender == 0 ? "man" : "woman";
    }

    public void work() {
        if(18 <= age && age <= 50) {
            System.out.println(name + " is working very hard!");
        } else {
            System.out.println(name + " can't work!");
        }
    }
}

2. ???

???????? is-a ????????? Cat ?? Animal ??????? is-a ???????????? Cat ????? Animal???????? Animal ?? private ????????????

Cat ??????? Animal ????????????????? Animal ???? Cat ?????????????????????? ?????????

??????????????<3F>I??????????????????????<3F>I?¦Ê??????????????????????? is-a ?????

Animal animal = new Cat();

3. ???

???????????????????????????????????????????????????????????????§Ø???????????????????????????????????????

???????????????1. ??§µ?2. ???????????3. ????????

?????????§µ???????Instrument????????????Wind ?? Percussion????????????? play() ???????????? main() ???????????? Instrument ?????? Wind ?? Percussion ?????? Instrument ??????? play() ????????????????????????????? play() ???????????? Instrument ????????

public class Instrument {
    public void play() {
        System.out.println("Instument is playing...");
    }
}

public class Wind extends Instrument {
    public void play() {
        System.out.println("Wind is playing...");
    }
}

public class Percussion extends Instrument {
    public void play() {
        System.out.println("Percussion is playing...");
    }
}

public class Music {
    public static void main(String[] args){
        List<Instrument> instruments = new ArrayList<>();
        instruments.add(new Wind());
        instruments.add(new Percussion());
        for(Instrument instrument : instruments){
            instrument.play();
        }
    }
}

UML

1. ???

1.1 ??????

????????????: ??????generalize????????realize????????? is-a ?????

?? ???????(generalization)

????????§Þ??

?? ?????(realize)

?????????????§Þ??

1.2 ????????

?? ?????(aggregation)

??????????????????????????????????????????<3F>I????????????????????¡À?? B ?? A ????

?? ?????(composition)

??????????????????????????????????<3F>I??????????????????????ÀM??????????????????????????????????????????????????????????????????

1.3 ?????

?? ???????(association)

???????????????§Û????????????????????????§Û??????????????????????????????????? 1 ?? 1????? 1?????????????????????????????????§µ???????????????????§µ?????§Ü??????????????????????????§µ??????????????????????????§á??????????????

?? ???????(dependency)

???????????????, ??????????????§Û??????????????????????????????????????????????????????????????????

2. ????

2.1 ????

??????????????????????????????????????????????????????????????????????????????????????????????????????????????

2.2 ?????????

????????????¡À???????????

????????????????????????????????????

publc class ???? {
   public void ??();
}

publc class  ???? {
  public void  ??????();
  public void  ???????();
  private void ?Úˆ???();
}

public class ???? {
    public void  ?????G??();
}

public class ??? {
   public void  ??????????();
}

public class ??? {
   public void  ???????();
}

2.3 ???????????????

?????????????????????????

?????????????????????????????????????

2.4 ?????????????

????????????????????????????????????

2.5 ?????????

?? ????

????????????

????????????????????

  1. ?????????????????????

  2. ????????????????????????????????????????????????

?? ??????

???????????????????????????????

?? ???

?????????????????????????????

?????4???????

1. ????????????????????

2. ????????????????????????????????????

3. ?????????????????????????

4. ??????????????

?? ????

???????????????????????????????????????

?¦Ï?????