1 2 SOLID 原则 2.1 开闭原则 OCP 定义:软件实体(类、模块、函数)应当对扩展开放,对修改关闭。
简单理解:不要修改已有代码,而是增加代码来扩展行为。
2.2 单一职责原则 SRP 定义:一个类只有一个引起他变化的原因。
简单理解:一个类或者模块应该仅做一件事,并且将这件事情做好。
典型代码实例如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 public class ReportGenerator { private String data; public ReportGenerator (String data) { this .data = data; } public String generate () { return reportContent; } } public interface ReportFormatter { String format (String content) ; } public class HtmlFormatter implements ReportFormatter { @Override public String format (String content) { return formattedContent; } } public class PdfFormatter implements ReportFormatter { @Override public String format (String content) { return formattedContent; } } public interface ReportRepository { void save (String content, String filename) ; } public class FileSystemRepository implements ReportRepository { @Override public void save (String content, String filename) { } } public interface ReportSender { void send (String content, String recipient) ; } public class EmailReportSender implements ReportSender { @Override public void send (String content, String recipient) { } } public class ReportService { private ReportGenerator generator; private ReportFormatter formatter; private ReportRepository repository; private ReportSender sender; public ReportService (ReportGenerator generator, ReportFormatter formatter, ReportRepository repository, ReportSender sender) { this .generator = generator; this .formatter = formatter; this .repository = repository; this .sender = sender; } public void createAndSendReport (String filename, String recipient, String formatType) { String content = generator.generate(); String formattedContent = formatter.format(content); repository.save(formattedContent, filename); sender.send(formattedContent, recipient); } }
2.3 依赖倒置原则 DIP 定义:
高层模块不应该依赖于低层模块,两者都应该依赖于抽象。
抽象不应该依赖于细节,细节应该依赖于抽象。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 public interface DataRepository { void save (String data) ; } public class MySQLDatabase implements DataRepository { @Override public void save (String data) { System.out.println("Saving data to MySQL: " + data); } } public class PostgreSQLDatabase implements DataRepository { @Override public void save (String data) { System.out.println("Saving data to PostgreSQL: " + data); } } public class FileSystemRepository implements DataRepository { @Override public void save (String data) { System.out.println("Saving data to file: " + data); } } public class ReportService { private DataRepository repository; public ReportService (DataRepository repository) { this .repository = repository; } public void generateReport (String reportData) { repository.save(reportData); } } public class Client { public static void main (String[] args) { DataRepository mySqlRepo = new MySQLDatabase (); ReportService reportService = new ReportService (mySqlRepo); reportService.generateReport("Important Report Data" ); } }
2.4 里氏替换原则 定义:子类型必须能够替换掉他们的父类型,程序的行为仍保持正确。
通俗理解:子类不能违背父类的语义和契约。
2.5 接口隔离原则 定义:客户端不应该被迫依赖它不使用的方法。
通俗理解:一个接口只包含调用者真正需要的方法。