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

308 lines
7.5 KiB
Markdown
Raw Blame History

<!-- GFM-TOC -->
* [S.O.L.I.D](#solid)
* [1. ??????????](#1-??????????)
* [2. ?????????](#2-?????????)
* [3. ?????<3F>I???](#3-?????<3F>I???)
* [4. ?????????](#4-?????????)
* [5. ???????????](#5-???????????)
* [???????§³????](#???????§ã????)
* [1. ???](#1-???)
* [2. ???](#2-???)
* [3. ???](#3-???)
* [UML](#uml)
* [1. ???](#1-???)
* [2. ????](#2-????)
* [?¦Ï?????](#?¦Ï?????)
<!-- GFM-TOC -->
# 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 ??????????§Õ›¥????????????????????????????????????????????????????????????????????????????????§³?
```java
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 ?????
```java
Animal animal = new Cat();
```
## 3. ???
???????????????????????????????????????????????????????????????§Ø???????????????????????????????????????
???????????????1. ??§µ?2. ???????????3. ????????
?????????§µ???????Instrument????????????Wind ?? Percussion????????????? play() ???????????? main() ???????????? Instrument ?????? Wind ?? Percussion ?????? Instrument ??????? play() ????????????????????????????? play() ???????????? Instrument ????????
```java
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)
????????§Þ??
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/29badd92-109f-4f29-abb9-9857f5973928.png)
?? ?????(realize)
?????????????§Þ??
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/4b16e1d3-3a60-472c-9756-2f31b1c48abe.png)
**1.2 ????????**
?? ?????(aggregation)
??????????????????????????????????????????<3F>I????????????????????¡À?? B ?? A ????
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/34259bb8-ca3a-4872-8771-9e946782d9c3.png)
?? ?????(composition)
??????????????????????????????????<3F>I??????????????????????ÀM??????????????????????????????????????????????????????????????????
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/7dda050d-ac35-4f47-9f51-18f18ed6fa9a.png)
**1.3 ?????**
?? ???????(association)
???????????????§Û????????????????????????§Û??????????????????????????????????? 1 ?? 1????? 1?????????????????????????????????§µ???????????????????§µ?????§Ü??????????????????????????§µ??????????????????????????§á??????????????
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/4ccd294c-d6b2-421b-839e-d88336ff5fb7.png)
?? ???????(dependency)
???????????????, ??????????????§Û??????????????????????????????????????????????????????????????????
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/47ca2614-509f-476e-98fc-50ec9f9d43c0.png)
## 2. ????
**2.1 ????**
??????????????????????????????????????????????????????????????????????????????????????????????????????????????
**2.2 ?????????**
????????????¡À???????????
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/80c5aff8-fc46-4810-aeaa-215b5c60a003.png)
????????????????????????????????????
```java
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 ?????????**
?? ????
????????????
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/25b8adad-2ef6-4f30-9012-c306b4e49897.png)
????????????????????
1. ?????????????????????
2. ????????????????????????????????????????????????
?? ??????
???????????????????????????????
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/b7b0eac6-e7ea-4fb6-8bfb-95fec6f235e2.png)
?? ???
?????????????????????????????
?????4???????
1\. ????????????????????
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/a13b62da-0fa8-4224-a615-4cadacc08871.png)
2\. ????????????????????????????????????
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/33821037-dc40-4266-901c-e5b38e618426.png)
3\. ?????????????????????????
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/dec6c6cc-1b5f-44ed-b8fd-464fcf849dac.png)
4\. ??????????????
?? ????
???????????????????????????????????????
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/6ab5de9b-1c1e-4118-b2c3-fb6c7ed7de6f.png)
# ?¦Ï?????
- Java ??????
- [???????????SOLID???](http://www.cnblogs.com/shanyou/archive/2009/09/21/1570716.html)
- [????UML?????????](http://design-patterns.readthedocs.io/zh_CN/latest/read_uml.html#generalization)
- [UML??§³???????????????sequence diagram](http://www.cnblogs.com/wolf-sun/p/UML-Sequence-diagram.html)
- [?????????????????------???????§³????](http://blog.csdn.net/jianyuerensheng/article/details/51602015)