| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,11 @@ | |||
| 1 | + <?xml version="1.0" encoding="UTF-8"?> | ||
| 2 | + <module type="JAVA_MODULE" version="4"> | ||
| 3 | + <component name="NewModuleRootManager" inherit-compiler-output="true"> | ||
| 4 | + <exclude-output /> | ||
| 5 | + <content url="file://$MODULE_DIR$"> | ||
| 6 | + <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" /> | ||
| 7 | + </content> | ||
| 8 | + <orderEntry type="jdk" jdkName="corretto-1.8" jdkType="JavaSDK" /> | ||
| 9 | + <orderEntry type="sourceFolder" forTests="false" /> | ||
| 10 | + </component> | ||
| 11 | + </module> | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,20 @@ | |||
| 1 | + package DependencyInversion; | ||
| 2 | + | ||
| 3 | + public class DependencyInversion1 { | ||
| 4 | + public static void main(String[] args) { | ||
| 5 | + Person person = new Person(); | ||
| 6 | + person.receive(new Email()); | ||
| 7 | + } | ||
| 8 | + } | ||
| 9 | + | ||
| 10 | + class Email { | ||
| 11 | + public String getInfo() { | ||
| 12 | + return "电子邮件:Hello World!!!"; | ||
| 13 | + } | ||
| 14 | + } | ||
| 15 | + | ||
| 16 | + class Person { | ||
| 17 | + public void receive(Email e) { | ||
| 18 | + System.out.println(e.getInfo()); | ||
| 19 | + } | ||
| 20 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,39 @@ | |||
| 1 | + package DependencyInversion; | ||
| 2 | + | ||
| 3 | + /** | ||
| 4 | + * 如果我们获取的对象是微信、短信等等,则新增类,同时 Person 也要增加相应的接收方法 | ||
| 5 | + * 解决思路:引入一个抽象的接口 IReceiver,表示接收者,这样 Person 类与接口 IReceiver 发生依赖 | ||
| 6 | + * 因为 Email、WeChat 等等属于接收的范围,它们各自实现 IReceiver 接口就可以,这样我们就符合依赖倒转原则 | ||
| 7 | + */ | ||
| 8 | + public class DependencyInversion2 { | ||
| 9 | + public static void main(String[] args) { | ||
| 10 | + Person2 Person2 = new Person2(); | ||
| 11 | + Person2.receive(new Email2()); | ||
| 12 | + Person2.receive(new WeChat()); | ||
| 13 | + } | ||
| 14 | + } | ||
| 15 | + | ||
| 16 | + interface IReceiver { | ||
| 17 | + public String getInfo(); | ||
| 18 | + } | ||
| 19 | + | ||
| 20 | + class Email2 implements IReceiver { | ||
| 21 | + @Override | ||
| 22 | + public String getInfo() { | ||
| 23 | + return "电子邮件:Hello World!"; | ||
| 24 | + } | ||
| 25 | + } | ||
| 26 | + | ||
| 27 | + class WeChat implements IReceiver { | ||
| 28 | + @Override | ||
| 29 | + public String getInfo() { | ||
| 30 | + return "微信消息:Hello weixin"; | ||
| 31 | + } | ||
| 32 | + } | ||
| 33 | + | ||
| 34 | + | ||
| 35 | + class Person2 { | ||
| 36 | + public void receive(IReceiver i) { | ||
| 37 | + System.out.println(i.getInfo()); | ||
| 38 | + } | ||
| 39 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,93 @@ | |||
| 1 | + package Dimit; | ||
| 2 | + | ||
| 3 | + import java.util.ArrayList; | ||
| 4 | + import java.util.List; | ||
| 5 | + | ||
| 6 | + public class Demeter1 { | ||
| 7 | + public static void main(String[] args) { | ||
| 8 | + SchoolManager schoolManager = new SchoolManager(); | ||
| 9 | + schoolManager.printAllEmployee(new CollegeManager()); | ||
| 10 | + } | ||
| 11 | + } | ||
| 12 | + | ||
| 13 | + //学校总部员工 | ||
| 14 | + class Employee { | ||
| 15 | + private String id; | ||
| 16 | + | ||
| 17 | + public String getId() { | ||
| 18 | + return id; | ||
| 19 | + } | ||
| 20 | + | ||
| 21 | + public void setId(String id) { | ||
| 22 | + this.id = id; | ||
| 23 | + } | ||
| 24 | + } | ||
| 25 | + | ||
| 26 | + //学院员工 | ||
| 27 | + class CollegeEmployee { | ||
| 28 | + private String id; | ||
| 29 | + | ||
| 30 | + public String getId() { | ||
| 31 | + return id; | ||
| 32 | + } | ||
| 33 | + | ||
| 34 | + public void setId(String id) { | ||
| 35 | + this.id = id; | ||
| 36 | + } | ||
| 37 | + } | ||
| 38 | + | ||
| 39 | + //管理学院员工的类 | ||
| 40 | + class CollegeManager { | ||
| 41 | + //返回学院的所有员工 | ||
| 42 | + public List<CollegeEmployee> getAllEmployee() { | ||
| 43 | + List<CollegeEmployee> list = new ArrayList<CollegeEmployee>(); | ||
| 44 | + for (int i = 0; i < 10; i++) {//增加了10个员工 | ||
| 45 | + CollegeEmployee emp = new CollegeEmployee(); | ||
| 46 | + emp.setId("学院员工id=" + i); | ||
| 47 | + list.add(emp); | ||
| 48 | + } | ||
| 49 | + return list; | ||
| 50 | + } | ||
| 51 | + | ||
| 52 | + //获取学院员工 | ||
| 53 | + public void printEmployee() { | ||
| 54 | + List<CollegeEmployee> list1 = this.getAllEmployee(); | ||
| 55 | + System.out.println("------------学院员工------------"); | ||
| 56 | + for (CollegeEmployee e : list1) { | ||
| 57 | + System.out.println(e.getId()); | ||
| 58 | + } | ||
| 59 | + } | ||
| 60 | + } | ||
| 61 | + | ||
| 62 | + /** | ||
| 63 | + * 管理学校员工的类 | ||
| 64 | + * 分析:SchoolManager类的直接朋友有哪些? | ||
| 65 | + * 直接朋友有“Employee”、“CollegeManager” | ||
| 66 | + * 非直接朋友有“CollegeEmployee” | ||
| 67 | + * 这就违背了迪米特法则 | ||
| 68 | + */ | ||
| 69 | + class SchoolManager { | ||
| 70 | + //返回学校总部的员工 | ||
| 71 | + public List<Employee> getAllEmployee() { | ||
| 72 | + List<Employee> list = new ArrayList<Employee>(); | ||
| 73 | + | ||
| 74 | + for (int i = 0; i < 5; i++) { //增加了5个员工 | ||
| 75 | + Employee emp = new Employee(); | ||
| 76 | + emp.setId("学校总部员工id= " + i); | ||
| 77 | + list.add(emp); | ||
| 78 | + } | ||
| 79 | + return list; | ||
| 80 | + } | ||
| 81 | + | ||
| 82 | + //完成输出学校总部和学院员工信息的方法 | ||
| 83 | + void printAllEmployee(CollegeManager sub) { | ||
| 84 | + //获取学院员工 | ||
| 85 | + sub.printEmployee(); | ||
| 86 | + //获取学校总部员工 | ||
| 87 | + List<Employee> list2 = this.getAllEmployee(); | ||
| 88 | + System.out.println("------------学校总部员工------------"); | ||
| 89 | + for (Employee e : list2) { | ||
| 90 | + System.out.println(e.getId()); | ||
| 91 | + } | ||
| 92 | + } | ||
| 93 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,93 @@ | |||
| 1 | + package Dimit; | ||
| 2 | + | ||
| 3 | + import java.util.ArrayList; | ||
| 4 | + import java.util.List; | ||
| 5 | + | ||
| 6 | + public class Demeter2 { | ||
| 7 | + public static void main(String[] args) { | ||
| 8 | + SchoolManager2 schoolManager2 = new SchoolManager2(); | ||
| 9 | + schoolManager2.printAllEmployee2(new CollegeManager2()); | ||
| 10 | + } | ||
| 11 | + } | ||
| 12 | + | ||
| 13 | + //学校总部员工 | ||
| 14 | + class Employee2 { | ||
| 15 | + private String id; | ||
| 16 | + | ||
| 17 | + public String getId() { | ||
| 18 | + return id; | ||
| 19 | + } | ||
| 20 | + | ||
| 21 | + public void setId(String id) { | ||
| 22 | + this.id = id; | ||
| 23 | + } | ||
| 24 | + } | ||
| 25 | + | ||
| 26 | + //学院员工 | ||
| 27 | + class CollegeEmployee2 { | ||
| 28 | + private String id; | ||
| 29 | + | ||
| 30 | + public String getId() { | ||
| 31 | + return id; | ||
| 32 | + } | ||
| 33 | + | ||
| 34 | + public void setId(String id) { | ||
| 35 | + this.id = id; | ||
| 36 | + } | ||
| 37 | + } | ||
| 38 | + | ||
| 39 | + //管理学院员工的类 | ||
| 40 | + class CollegeManager2 { | ||
| 41 | + //返回学院的所有员工 | ||
| 42 | + public List<CollegeEmployee2> getAllEmployee2() { | ||
| 43 | + List<CollegeEmployee2> list = new ArrayList<CollegeEmployee2>(); | ||
| 44 | + for (int i = 0; i < 10; i++) {//增加了10个员工 | ||
| 45 | + CollegeEmployee2 emp = new CollegeEmployee2(); | ||
| 46 | + emp.setId("学院员工id=" + i); | ||
| 47 | + list.add(emp); | ||
| 48 | + } | ||
| 49 | + return list; | ||
| 50 | + } | ||
| 51 | + | ||
| 52 | + //获取学院员工 | ||
| 53 | + public void printEmployee2() { | ||
| 54 | + List<CollegeEmployee2> list1 = this.getAllEmployee2(); | ||
| 55 | + System.out.println("------------学院员工------------"); | ||
| 56 | + for (CollegeEmployee2 e : list1) { | ||
| 57 | + System.out.println(e.getId()); | ||
| 58 | + } | ||
| 59 | + } | ||
| 60 | + } | ||
| 61 | + | ||
| 62 | + /** | ||
| 63 | + * 管理学校员工的类 | ||
| 64 | + * 分析:SchoolManager2类的直接朋友有哪些? | ||
| 65 | + * 直接朋友有“Employee2”、“CollegeManager2” | ||
| 66 | + * 非直接朋友有“CollegeEmployee2” | ||
| 67 | + * 这就违背了迪米特法则 | ||
| 68 | + */ | ||
| 69 | + class SchoolManager2 { | ||
| 70 | + //返回学校总部的员工 | ||
| 71 | + public List<Employee2> getAllEmployee2() { | ||
| 72 | + List<Employee2> list = new ArrayList<Employee2>(); | ||
| 73 | + | ||
| 74 | + for (int i = 0; i < 5; i++) { //增加了5个员工 | ||
| 75 | + Employee2 emp = new Employee2(); | ||
| 76 | + emp.setId("学校总部员工id= " + i); | ||
| 77 | + list.add(emp); | ||
| 78 | + } | ||
| 79 | + return list; | ||
| 80 | + } | ||
| 81 | + | ||
| 82 | + //完成输出学校总部和学院员工信息的方法 | ||
| 83 | + void printAllEmployee2(CollegeManager2 sub) { | ||
| 84 | + //获取学院员工 | ||
| 85 | + sub.printEmployee2(); | ||
| 86 | + //获取学校总部员工 | ||
| 87 | + List<Employee2> list2 = this.getAllEmployee2(); | ||
| 88 | + System.out.println("------------学校总部员工------------"); | ||
| 89 | + for (Employee2 e : list2) { | ||
| 90 | + System.out.println(e.getId()); | ||
| 91 | + } | ||
| 92 | + } | ||
| 93 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,102 @@ | |||
| 1 | + package InterfaceSegregation; | ||
| 2 | + | ||
| 3 | + // 不遵循接口隔离原则,会导致每个类都要实现接口中的所有方法 | ||
| 4 | + public class Segregation1 { | ||
| 5 | + } | ||
| 6 | + | ||
| 7 | + //接口 | ||
| 8 | + interface Interface1 { | ||
| 9 | + void operation1(); | ||
| 10 | + | ||
| 11 | + void operation2(); | ||
| 12 | + | ||
| 13 | + void operation3(); | ||
| 14 | + | ||
| 15 | + void operation4(); | ||
| 16 | + | ||
| 17 | + void operation5(); | ||
| 18 | + } | ||
| 19 | + | ||
| 20 | + class B implements Interface1 { | ||
| 21 | + | ||
| 22 | + @Override | ||
| 23 | + public void operation1() { | ||
| 24 | + System.out.println("B实现了operation1"); | ||
| 25 | + } | ||
| 26 | + | ||
| 27 | + @Override | ||
| 28 | + public void operation2() { | ||
| 29 | + System.out.println("B实现了operation2"); | ||
| 30 | + } | ||
| 31 | + | ||
| 32 | + @Override | ||
| 33 | + public void operation3() { | ||
| 34 | + System.out.println("B实现了operation3"); | ||
| 35 | + } | ||
| 36 | + | ||
| 37 | + @Override | ||
| 38 | + public void operation4() { | ||
| 39 | + System.out.println("B实现了operation4"); | ||
| 40 | + } | ||
| 41 | + | ||
| 42 | + @Override | ||
| 43 | + public void operation5() { | ||
| 44 | + System.out.println("B实现了operation5"); | ||
| 45 | + } | ||
| 46 | + } | ||
| 47 | + | ||
| 48 | + class D implements Interface1 { | ||
| 49 | + @Override | ||
| 50 | + public void operation1() { | ||
| 51 | + System.out.println("D实现了operation1"); | ||
| 52 | + } | ||
| 53 | + | ||
| 54 | + @Override | ||
| 55 | + public void operation2() { | ||
| 56 | + System.out.println("D实现了operation2"); | ||
| 57 | + } | ||
| 58 | + | ||
| 59 | + @Override | ||
| 60 | + public void operation3() { | ||
| 61 | + System.out.println("D实现了operation3"); | ||
| 62 | + } | ||
| 63 | + | ||
| 64 | + @Override | ||
| 65 | + public void operation4() { | ||
| 66 | + System.out.println("D实现了operation4"); | ||
| 67 | + } | ||
| 68 | + | ||
| 69 | + @Override | ||
| 70 | + public void operation5() { | ||
| 71 | + System.out.println("D实现了operation5"); | ||
| 72 | + } | ||
| 73 | + } | ||
| 74 | + | ||
| 75 | + class A { | ||
| 76 | + public void depend1(Interface1 i) { | ||
| 77 | + i.operation1(); | ||
| 78 | + } | ||
| 79 | + | ||
| 80 | + public void depend2(Interface1 i) { | ||
| 81 | + i.operation2(); | ||
| 82 | + } | ||
| 83 | + | ||
| 84 | + public void depend3(Interface1 i) { | ||
| 85 | + i.operation3(); | ||
| 86 | + } | ||
| 87 | + | ||
| 88 | + } | ||
| 89 | + | ||
| 90 | + class C { | ||
| 91 | + public void depend1(Interface1 i) { | ||
| 92 | + i.operation1(); | ||
| 93 | + } | ||
| 94 | + | ||
| 95 | + public void depend4(Interface1 i) { | ||
| 96 | + i.operation4(); | ||
| 97 | + } | ||
| 98 | + | ||
| 99 | + public void depend5(Interface1 i) { | ||
| 100 | + i.operation5(); | ||
| 101 | + } | ||
| 102 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments