* Single responsibility principle
definition ： A class should be a set of highly relevant functions , Data encapsulation .
practice ： Based on the understanding of business and requirements , To divide a class , Responsibilities of a function .
give an example ： For example, to achieve a loading image function , And when loading the image to achieve the image cache . At least two classes should be required to do this . One is to load and display images of the class Imageloader. One is the cache class ImageCache. If you write two classes together , It can lead to the readability of the code , flexibility , And scalability becomes very poor . Suppose you need to modify the caching mechanism now , So we just need to modify it ImageCache Just like one place . Anything that needs to use the image cache doesn't need to be modified .
* Open close principle
definition ： For extension, it is developed , For modified closed .
practice ： When new requirements arise , as far as possible （ Pay attention to try your best ） Do not modify the original code . Instead, it extends the original code . For example, create a new implementation class .
means ： The principle of opening and closing is realized through abstraction . Abstract an abstract class or interface . High level module （ caller ） Abstract through dependency （ We will talk about the principle of dependency inversion ） use “ Dependency injection ”（ stay java In is called set method ） To follow the opening and closing principle .
give an example ： Xiaoqiang truck factory is in the early stage of construction , Only big trucks can be built . Later, with the changes of the times , Xiaoqiang car factory needs to build Ferrari . So Xiaoqiang set up a Ferrari workshop . That is, it does not affect the construction of trucks , Can build a new Ferrari . But the resources of Xiaoqiang are limited , There is no way for colleagues to maintain the two workshops , So he abstracted the method of making cars , This workshop can press tires , Spray painting completes a series of car building actions . Use big tires when building trucks （set Big tire ） Ferrari is built with small tires （set Small tire ）
Xiaoqiang will do whatever he says , It's time to upgrade the workshop through abstraction . But dreams are beautiful , The reality is cruel . This is because we ignore the principle of quasi Riemannian substitution and the principle of dependency inversion . Xiaoqiang's first transformation ended in failure .
* Ritz's substitution principle
definition ： All references to the base class must be able to use its subclasses transparently . Popular speaking , Subclasses can be used wherever the parent class can appear .
practice , Using abstraction , Extract an abstract class or interface .
give an example ： Here is an example of engine installation . The abstract workshop can install the engine , But trucks have large slow engines , And Ferrari needs advanced small engines . Because of technical reasons . Xiaoqiang has written down the function of installing engine, and can only install large engine . There's no problem building trucks . But when it comes to Ferrari . But mistakenly installed the large engine on the Ferrari . to this end , Xiaoqiang's car factory can't make Ferrari , It's a big loss .
* The principle of Dependence Inversion
definition ： Dependency abstraction , Don't rely on details .
practice ： Interface oriented programming .
give an example ： Xiaoqiang sees the crux of the problem , So the workshop was transformed , Abstract the function of installing engine , No longer write to install large engines . Instead, the details of installing the engine are left to the truck and Ferrari respectively . And the high-level module （ The caller here refers to the car maker , Because it's the workers who use the workshop to make cars ） Just call the abstract workshop , According to the demand set The car will do . such , Abstract through dependency , Cars are being produced in a steady stream .
At this time, the witty Xiaoqiang found that , Trucks don't need to be polished , Ferrari needs to be polished . But it's all in the same workshop , That led to truck building , Although the truck did not achieve the function of grinding , But I still go through the class , Lead to the waste of class resources . At this time, Xiaoqiang thought of the class interface isolation principle .
* Interface separation principle
definition ： High level module （ caller ） You should not rely on interfaces it doesn't need
practice ： It's going to be huge , Bulky interfaces split into smaller ones , More specific interface
give an example ： Through the observation of the car making process . Xiaoqiang transformed the large workshop into many small workshops , There is a workshop where engines are installed , Workshop for tire installation , Paint shop , Grinding workshop . The truck was finished after being assembled and painted . The caller no longer needs to polish the workshop . We have completed the task and saved resources . Xiaoqiang instantly felt that he was full of wit .
* Dimitar principle
Definition and Practice ： Only talk to your direct friends . Class don't visit friends who are far away from you .
give an example ： Xiaoqiang's cars are becoming more and more , In the past, users had to ask the middleman to buy a car for them after knowing the details of the car , This is extremely inconvenient . Fortunately, the middleman told the user , Just tell him the type and price of the car . He can go to Xiaoqiang to find a suitable car for users . Users can easily get the car they want . And also no longer need to go to the Xiaoqiang car factory to learn the details of the car . Reduced the travel and toil of travel . Middlemen have also defined their own positioning .