308 lines
7.5 KiB
Markdown
308 lines
7.5 KiB
Markdown
<!-- 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)
|
||
|
||
????????§Þ??
|
||
|
||

|
||
|
||
?? ?????(realize)
|
||
|
||
?????????????§Þ??
|
||
|
||

|
||
|
||
**1.2 ????????**
|
||
|
||
?? ?????(aggregation)
|
||
|
||
??????????????????????????????????????????<3F>I????????????????????¡À?? B ?? A ????
|
||
|
||

|
||
|
||
|
||
?? ?????(composition)
|
||
|
||
??????????????????????????????????<3F>I??????????????????????ÀM??????????????????????????????????????????????????????????????????
|
||
|
||

|
||
|
||
**1.3 ?????**
|
||
|
||
?? ???????(association)
|
||
|
||
???????????????§Û????????????????????????§Û??????????????????????????????????? 1 ?? 1????? 1?????????????????????????????????§µ???????????????????§µ?????§Ü??????????????????????????§µ??????????????????????????§á??????????????
|
||
|
||

|
||
|
||
?? ???????(dependency)
|
||
|
||
???????????????, ??????????????§Û??????????????????????????????????????????????????????????????????
|
||
|
||

|
||
|
||
|
||
## 2. ????
|
||
|
||
**2.1 ????**
|
||
|
||
??????????????????????????????????????????????????????????????????????????????????????????????????????????????
|
||
|
||
**2.2 ?????????**
|
||
|
||
????????????¡À???????????
|
||
|
||

|
||
|
||
????????????????????????????????????
|
||
|
||
```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 ?????????**
|
||
|
||
?? ????
|
||
|
||
????????????
|
||
|
||

|
||
|
||
????????????????????
|
||
|
||
1. ?????????????????????
|
||
|
||
2. ????????????????????????????????????????????????
|
||
|
||
?? ??????
|
||
|
||
???????????????????????????????
|
||
|
||

|
||
|
||
?? ???
|
||
|
||
?????????????????????????????
|
||
|
||
?????4???????
|
||
|
||
1\. ????????????????????
|
||
|
||

|
||
|
||
2\. ????????????????????????????????????
|
||
|
||

|
||
|
||
3\. ?????????????????????????
|
||
|
||

|
||
|
||
4\. ??????????????
|
||
|
||
?? ????
|
||
|
||
???????????????????????????????????????
|
||
|
||

|
||
|
||
|
||
# ?¦Ï?????
|
||
|
||
- 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)
|