设计模式七大原则
设计模式目的
1、代码重用性
2、可读性
3、可读性
4、扩展性
5、可靠性
6、高内聚低耦合
七大原则
1、单一职责原则
一个类或方法中只做一件事情
2、接口隔离原则
一个类通过接口去依赖另一个类的所有实现方法(拆接口)
public interface InterfaceIsolate {
public static void main(String[] args) {
C c = new C();
c.depend1(new A());
D d = new D();
d.depend2(new B());
d.depend3(new B());
}
}
//将接口拆开
interface interface1 {
void method1();
}
interface interface2 {
void method2();
void method3();
}
interface interface3 {
void method4();
}
class A implements interface1 {
@Override
public void method1() {
System.out.println("method1");
}
}
class B implements interface2 , interface3 {
@Override
public void method2() {
System.out.println("method2");
}
@Override
public void method3() {
System.out.println("method3");
}
@Override
public void method4() {
System.out.println("method4");
}
}
//类通过接口来实现类的方法
class C {
public void depend1(interface1 i) {
i.method1();
}
}
class D {
public void depend2(interface2 i) {
i.method2();
i.method3();
}
public void depend3(interface3 i) {
i.method4();
}
}
3、依赖倒转(倒置原则)
接口和抽象类定义好规范
使用实现类来实现接口或抽象类
1、通过接口传递实现
public class DependencyConvert {
public static void main(String[] args) {
OpenTV openTV = new OpenTV();
openTV.open(new Changhong());
}
}
interface MyTV {
void open(OpsTV opsTV);//通过接口传递实现
}
interface OpsTV {
void play();
}
class OpenTV implements MyTV {
@Override
public void open(OpsTV opsTV) {
opsTV.play();
}
}
class Changhong implements OpsTV {
@Override
public void play() {
System.out.println("长虹TV开始播放");
}
}
2、通过构造方法实现
public class DependencyConvert {
public static void main(String[] args) {
OpenTV openTV = new OpenTV(new Changhong());
openTV.open();
}
}
interface MyTV {
void open();
}
interface OpsTV {
void play();
}
class OpenTV implements MyTV {
OpsTV opsTV;
OpenTV(OpsTV opsTV) { //通过构造方法实现
this.opsTV = opsTV;
}
@Override
public void open() {
opsTV.play();
}
}
class Changhong implements OpsTV {
@Override
public void play() {
System.out.println("长虹TV开始播放");
}
}
3、通过setter注入实现
public class DependencyConvert {
public static void main(String[] args) {
OpenTV openTV = new OpenTV();
openTV.setOpsTV(new Changhong());
openTV.open();
}
}
interface MyTV {
void open();
}
interface OpsTV {
void play();
}
class OpenTV implements MyTV {
OpsTV opsTV;
public void setOpsTV(OpsTV opsTV) {//通过setter注入实现
this.opsTV = opsTV;
}
@Override
public void open() {
opsTV.play();
}
}
class Changhong implements OpsTV {
@Override
public void play() {
System.out.println("长虹TV开始播放");
}
}
4、里氏替换原则
继承时,尽量不要重写父类方法
采取依赖、聚合、组合等关系代替
public class replaceLi {
public static void main(String[] args) {
int a = 8;
int b = 3;
Test2 test2 = new Test2();
System.out.println(test2.f1(a, b));
System.out.println(test2.f2(a, b));
}
}
class Test1 {
public int f1(int a,int b) {
return a-b;
}
}
class Test2 {
Test1 test1 = new Test1();
public int f1(int a, int b) {
return a + b;
}
public int f2(int a, int b) {
return test1.f1(a,b);
}
}
5、开闭原则
ocp原则:对扩展方开放,对修改方(调用方)关闭
public class OcpPrinciple {
public static void main(String[] args) {
Graphic graphic = new Graphic();
graphic.drawGraphic(new Triangle());
graphic.drawGraphic(new Rectangle());
}
}
class Graphic {
void drawGraphic(Shape shape) { //调用方无需修改
shape.draw();
}
}
abstract class Shape {
abstract void draw();
}
class Triangle extends Shape {
@Override
void draw() {
System.out.println(" 绘制三角形 ");
}
}
class Rectangle extends Shape { //扩展放直接继承Shape
@Override
void draw() {
System.out.println(" 绘制矩形 ");
}
}
6、迪米特法则
最少知道原则:一个类对自己依赖的类知道越少越好,将逻辑封装到类的内部(尽量,无法避免)
直接朋友:
1、出现在成员变量、方法参数、方法返回值的类
2、局部变量中的类都不是直接朋友!
public class Demeter {
public static void main(String[] args) {
PrintSchoolAllPeople printSchoolAllPeople = new PrintSchoolAllPeople();
printSchoolAllPeople.printPeople(new PrintSchoolStudent());
}
}
class SchoolAllPeople {
private String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@Override
public String toString() {
return "SchoolAllPeople{" +
"id='" + id + '\'' +
'}';
}
}
class SchoolStudent {
private String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@Override
public String toString() {
return "SchoolStudent{" +
"id='" + id + '\'' +
'}';
}
}
class PrintSchoolStudent {
public List<SchoolStudent> getAllSchoolStudent() {
ArrayList<SchoolStudent> list = new ArrayList<>();
for (int i = 1; i <= 4; i++) {
SchoolStudent schoolStudent = new SchoolStudent();
schoolStudent.setId("学生id:"+i);
list.add(schoolStudent);
}
return list;
}
public void printStudent() {
List<SchoolStudent> list = getAllSchoolStudent();
System.out.println("###############学生###############");
for (SchoolStudent e : list) {
System.out.println(e);
}
}
}
class PrintSchoolAllPeople {
public List<SchoolAllPeople> getAllSchoolAllPeople() {
ArrayList<SchoolAllPeople> list = new ArrayList<>();
for (int i = 1; i <= 10; i++) {
SchoolAllPeople schoolAllPeople = new SchoolAllPeople();
schoolAllPeople.setId("学校人员id:"+i);
list.add(schoolAllPeople);
}
return list;
}
void printPeople(PrintSchoolStudent printSchoolStudent) {
printSchoolStudent.printStudent();
List<SchoolAllPeople> list1 = getAllSchoolAllPeople();
System.out.println("##############所有人员##############");
for (SchoolAllPeople e : list1) {
System.out.println(e);
}
}
}
7、合成复用原则
尽量使用合成/聚合,而不是继承