CS-Notes/notes/面向对象思想.md

308 lines
7.5 KiB
Markdown
Raw Normal View History

2018-02-20 10:40:05 +08:00
<!-- GFM-TOC -->
* [S.O.L.I.D](#solid)
2018-02-22 14:47:22 +08:00
* [1. ??????????](#1-??????????)
* [2. ?????????](#2-?????????)
* [3. ?????<3F>I???](#3-?????<3F>I???)
* [4. ?????????](#4-?????????)
* [5. ???????????](#5-???????????)
* [???????<3F><>????](#???????<3F><>????)
* [1. ???](#1-???)
* [2. ???](#2-???)
* [3. ???](#3-???)
2018-02-20 10:40:05 +08:00
* [UML](#uml)
2018-02-22 14:47:22 +08:00
* [1. ???](#1-???)
* [2. ????](#2-????)
* [?<3F><>?????](#?<3F><>?????)
2018-02-20 10:40:05 +08:00
<!-- GFM-TOC -->
# S.O.L.I.D
2018-02-22 14:47:22 +08:00
S.O.L.I.D???????????????(OOD&OOP)?<3F><>?????????????(Programming Priciple)?????????<3F><>??
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
|??<3F><> |?? |???????|
2018-02-20 10:40:05 +08:00
| -- | -- | -- |
2018-02-22 14:47:22 +08:00
|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 |???????????|
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
## 1. ??????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
???????????????????????????????????<3F><>?????????????????????????<3F><>????????????<3F><>?????????????<3F><>????????????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
## 2. ?????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
?????????????????????????????????????????????????????????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
## 3. ?????<3F>I???
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
????????????????????<3F>I?<3F><>??????????????????????? is-a ?????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
## 4. ?????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
?????????????????<3F><>???????????????<3F><>??????????????????????????????<3F><>?
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
## 5. ???????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
1. ?????<3F><>?????????????<3F><>?????????????????
2. ????????????????????????????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
# ???????<3F><>????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
???????<3F><>???????????????????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
## 1. ???
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
???<3F><>??????????????????????????????????????<3F>q?????????????????<3F><>??????????????????????????????????????????????????????<3F><>????????????????????????????????????????????????????????????????????????????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
?????????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
1. ??????????????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
2. ????????????????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
3. ??????????<3F><>??????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
4. ????????????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
???? Person ???? name??gender??age ???????????????? get() ?????????? Person ????? name ????? gender ????????????? age ????????? age ???????? work() ??????<3F><>?
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
??? gender ??????? int ??????????<3F>՛<EFBFBD>????????????????????????????????????????????????????????????????????????????????<3F><>?
2018-02-20 10:40:05 +08:00
```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!");
}
}
}
```
2018-02-22 14:47:22 +08:00
## 2. ???
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
???????? **is-a** ????????? Cat ?? Animal ??????? is-a ???????????? Cat ????? Animal???????? Animal ?? private ????????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
Cat ??????? Animal ????????????????? Animal ???? Cat ?????????????????????? **???????**??
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
??????????????<3F>I??????????????????????<3F>I?<3F><>??????????????????????? is-a ?????
2018-02-20 10:40:05 +08:00
```java
Animal animal = new Cat();
```
2018-02-22 14:47:22 +08:00
## 3. ???
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
???????????????????????????????????????????????????????????????<3F><>???????????????????????????????????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
???????????????1. ??<3F><>?2. ???????????3. ????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
?????????<3F><>???????Instrument????????????Wind ?? Percussion????????????? play() ???????????? main() ???????????? Instrument ?????? Wind ?? Percussion ?????? Instrument ??????? play() ????????????????????????????? play() ???????????? Instrument ????????
2018-02-20 10:40:05 +08:00
```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
2018-02-22 14:47:22 +08:00
## 1. ???
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
**1.1 ??????**
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
????????????: ??????generalize????????realize????????? is-a ?????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
?? ???????(generalization)
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
????????<3F><>??
2018-02-20 10:40:05 +08:00
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/29badd92-109f-4f29-abb9-9857f5973928.png)
2018-02-22 14:47:22 +08:00
?? ?????(realize)
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
?????????????<3F><>??
2018-02-20 10:40:05 +08:00
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/4b16e1d3-3a60-472c-9756-2f31b1c48abe.png)
2018-02-22 14:47:22 +08:00
**1.2 ????????**
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
?? ?????(aggregation)
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
??????????????????????????????????????????<3F>I????????????????????<3F><>?? B ?? A ????
2018-02-20 10:40:05 +08:00
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/34259bb8-ca3a-4872-8771-9e946782d9c3.png)
2018-02-22 14:47:22 +08:00
?? ?????(composition)
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
??????????????????????????????????<3F>I??????????????????????<3F>M??????????????????????????????????????????????????????????????????
2018-02-20 10:40:05 +08:00
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/7dda050d-ac35-4f47-9f51-18f18ed6fa9a.png)
2018-02-22 14:47:22 +08:00
**1.3 ?????**
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
?? ???????(association)
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
???????????????<3F><>????????????????????????<3F><>??????????????????????????????????? 1 ?? 1????? 1?????????????????????????????????<3F><>???????????????????<3F><>?????<3F><>??????????????????????????<3F><>??????????????????????????<3F><>??????????????
2018-02-20 10:40:05 +08:00
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/4ccd294c-d6b2-421b-839e-d88336ff5fb7.png)
2018-02-22 14:47:22 +08:00
?? ???????(dependency)
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
???????????????, ??????????????<3F><>??????????????????????????????????????????????????????????????????
2018-02-20 10:40:05 +08:00
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/47ca2614-509f-476e-98fc-50ec9f9d43c0.png)
2018-02-22 14:47:22 +08:00
## 2. ????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
**2.1 ????**
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
??????????????????????????????????????????????????????????????????????????????????????????????????????????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
**2.2 ?????????**
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
????????????<3F><>???????????
2018-02-20 10:40:05 +08:00
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/80c5aff8-fc46-4810-aeaa-215b5c60a003.png)
2018-02-22 14:47:22 +08:00
????????????????????????????????????
2018-02-20 10:40:05 +08:00
```java
2018-02-22 14:47:22 +08:00
publc class ???? {
public void ??();
2018-02-20 10:40:05 +08:00
}
2018-02-22 14:47:22 +08:00
publc class ???? {
public void ??????();
public void ???????();
private void ?ڈ???();
2018-02-20 10:40:05 +08:00
}
2018-02-22 14:47:22 +08:00
public class ???? {
public void ?????G??();
2018-02-20 10:40:05 +08:00
}
2018-02-22 14:47:22 +08:00
public class ??? {
public void ??????????();
2018-02-20 10:40:05 +08:00
}
2018-02-22 14:47:22 +08:00
public class ??? {
public void ???????();
2018-02-20 10:40:05 +08:00
}
```
2018-02-22 14:47:22 +08:00
**2.3 ???????????????**
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
?????????????????????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
?????????????????????????????????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
**2.4 ?????????????**
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
????????????????????????????????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
**2.5 ?????????**
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
?? ????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
????????????
2018-02-20 10:40:05 +08:00
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/25b8adad-2ef6-4f30-9012-c306b4e49897.png)
2018-02-22 14:47:22 +08:00
????????????????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
1. ?????????????????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
2. ????????????????????????????????????????????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
?? ??????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
???????????????????????????????
2018-02-20 10:40:05 +08:00
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/b7b0eac6-e7ea-4fb6-8bfb-95fec6f235e2.png)
2018-02-22 14:47:22 +08:00
?? ???
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
?????????????????????????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
?????4???????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
1\. ????????????????????
2018-02-20 10:40:05 +08:00
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/a13b62da-0fa8-4224-a615-4cadacc08871.png)
2018-02-22 14:47:22 +08:00
2\. ????????????????????????????????????
2018-02-20 10:40:05 +08:00
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/33821037-dc40-4266-901c-e5b38e618426.png)
2018-02-22 14:47:22 +08:00
3\. ?????????????????????????
2018-02-20 10:40:05 +08:00
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/dec6c6cc-1b5f-44ed-b8fd-464fcf849dac.png)
2018-02-22 14:47:22 +08:00
4\. ??????????????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
?? ????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
???????????????????????????????????????
2018-02-20 10:40:05 +08:00
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/6ab5de9b-1c1e-4118-b2c3-fb6c7ed7de6f.png)
2018-02-22 14:47:22 +08:00
# ?<3F><>?????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
- Java ??????
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
- [???????????SOLID???](http://www.cnblogs.com/shanyou/archive/2009/09/21/1570716.html)
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
- [????UML?????????](http://design-patterns.readthedocs.io/zh_CN/latest/read_uml.html#generalization)
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
- [UML??<3F><>???????????????sequence diagram](http://www.cnblogs.com/wolf-sun/p/UML-Sequence-diagram.html)
2018-02-20 10:40:05 +08:00
2018-02-22 14:47:22 +08:00
- [?????????????????------???????<3F><>????](http://blog.csdn.net/jianyuerensheng/article/details/51602015)