* [S.O.L.I.D](#solid) * [1. ??????????](#1-??????????) * [2. ?????????](#2-?????????) * [3. ?????I???](#3-?????I???) * [4. ?????????](#4-?????????) * [5. ???????????](#5-???????????) * [???????????](#???????????) * [1. ???](#1-???) * [2. ???](#2-???) * [3. ???](#3-???) * [UML](#uml) * [1. ???](#1-???) * [2. ????](#2-????) * [??????](#??????) # 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 |?????I???| |ISP| The Interface Segregation Principle |?????????| |DIP| The Dependency Inversion Principle |???????????| ## 1. ?????????? ????????????????????????????????????????????????????????????????????????????????????????????????????? ## 2. ????????? ????????????????????????????????????????????????????????????? ## 3. ?????I??? ????????????????????I???????????????????????? is-a ????? ## 4. ????????? ??????????????????????????????????????????????????????????????? ## 5. ??????????? 1. ??????????????????????????????????? 2. ???????????????????????????????? # ??????????? ?????????????????????????????? ## 1. ??? ?????????????????????????????????????????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 ?????????????????????? **???????**?? ??????????????I??????????????????????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 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) ??????????????????????????????????????????I?????????????????????? B ?? A ???? ![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/34259bb8-ca3a-4872-8771-9e946782d9c3.png) ?? ?????(composition) ??????????????????????????????????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)