设计模式02
七大设计原则
开闭原则
依赖倒置原则
单一职责原则
接口隔离原则
迪米特法则
里氏替换原则
组合复用原则
开闭原则
一个软件实体如类,模块和函数应该对扩展开放,对修改关闭
用抽象构建框架,用实现扩展细节
优点:提供软件系统的可复用性及可维护性
举例:打折活动
Icourse
1 public interface ICourse { 2 Integer getId(); 3 String getName(); 4 Double getPrice(); 5 6 }
JavaCourse
1 public class JavaCourse implements ICourse { 2 3 private Integer Id; 4 private String name; 5 private Double price; 6 7 public JavaCourse(Integer id, String name, Double price) { 8 this.Id = id; 9 this.name = name; 10 this.price = price; 11 } 12 13 @Override 14 public Integer getId() { 15 return this.Id; 16 } 17 18 @Override 19 public String getName() { 20 return this.name; 21 } 22 23 @Override 24 public Double getPrice() { 25 return this.price; 26 } 27 28 29 }
JavaDiscountCourse
public class JavaDiscountCourse extends JavaCourse { public JavaDiscountCourse(Integer id, String name, Double price) { super(id, name, price); } public Double getOriginPrice(){ return super.getPrice(); } public Double getPrice(){ return super.getPrice()*0.8; } }
Test
1 public class Test { 2 3 public static void main(String[] args) { 4 5 ICourse iCourse = new JavaDiscountCourse(1,"java",200d); 6 JavaDiscountCourse javaCourse = (JavaDiscountCourse)iCourse; 7 8 System.out.println("id:"+javaCourse.getId()+ 9 "名称:"+javaCourse.getName()+ 10 "课程原价"+javaCourse.getOriginPrice()+ 11 "价格:"+javaCourse.getPrice()); 12 13 } 14 }
DIagram 如图
依赖倒置原则
高层模块不应该依赖于低层模块,两者应该依赖其抽象
抽象不应该依赖细节,细节应该依赖抽象
针对接口编程,不要针对实现编程
可以减少类间的耦合性,提高系统稳定性,提高代码可读性和可维护性,降低修改程序所造成的风险
Buil
public class Buil { /** * 扩展性差,不应该针对实现编程 */ public void StudyJavaCourse(){ System.out.println("bill 在学习java课程"); } public void StudyVueCourse(){ System.out.println("buil 在学习vue课程"); } }
Test
public class Test { public static void main(String[] args) { Buil buil = new Buil(); buil.StudyJavaCourse(); buil.StudyVueCourse(); } }
Test的实现依赖于Buil的实现,高层西模块依赖于底层次模块
@通过接口方法的方式注入具体的实现
ICourse
1 public interface ICourse { 2 3 void StudyCourse(); 4 5 }
JavaCourse
1 public class JavaCourse implements ICourse{ 2 3 @Override 4 public void StudyCourse() { 5 System.out.println("Buil在学习java课程"); 6 } 7 8 }
VueCourse
1 public class VueCourse implements ICourse { 2 @Override 3 public void StudyCourse() { 4 System.out.println("Buil在学习vue课程"); 5 } 6 7 }
Buil
public class Buil { public void studyCourses (ICourse iCourse){ iCourse.StudyCourse(); } }
Test
1 public class Test { 2 3 4 public static void main(String[] args) { 5 Buil buil = new Buil(); 6 buil.studyCourses(new JavaCourse()); 7 buil.studyCourses(new VueCourse()); 8 9 } 10 11 }
Diagram:
@通过构造器方注入具体的实现
Buil
public class Buil { private ICourse iCourse; public Buil(ICourse iCourse){ this.iCourse = iCourse; } public void studyCourses (){ iCourse.StudyCourse(); } }
Test
1 public class Test { 2 3 4 public static void main(String[] args) { 5 Buil buil = new Buil(new JavaCourse()); 6 buil.studyCourses(); 7 } 8 9 }
setter注入
1 public class Buil { 2 3 public ICourse iCourse; 4 5 public void setiCourse(ICourse iCourse) { 6 this.iCourse = iCourse; 7 } 8 9 public void studyCourses (){ 10 iCourse.StudyCourse(); 11 } 12 13 }
Test
1 public class Test { 2 3 4 public static void main(String[] args) { 5 Buil buil = new Buil(); 6 buil.setiCourse(new JavaCourse()); 7 buil.studyCourses(); 8 9 buil.setiCourse(new VueCourse()); 10 buil.studyCourses(); 11 12 } 13 15 }
Diagram:
单一职责原则
不要存在多于一个导致类变更的原因
一个类/接口/方法只负责一项职责
优点:降低类的复杂度,提高类的可读性,提高系统的可维护性,变更时风险率降低
FlyBird
public class FlyBird { public void mainMoveMode(String birdName) { System.out.println(birdName + "用翅膀飞"); } }
Workbird
public class WalkBird { public void mainMoveMode(String birdName) { if ("鸵鸟".equals(birdName)) { System.out.println(birdName + "用脚走"); } } }
Test
public class Test { public static void main(String[] args) { FlyBird flyBird = new FlyBird(); flyBird.mainMoveMode("大雁"); WalkBird walkBird = new WalkBird(); walkBird.mainMoveMode("鸵鸟"); } }
Diagram
接口级别的单一职责
ICourseContent
1 public interface ICourseContent { 2 3 String getCourseName(); 4 byte[] getCourseVideo(); 5 6 }
ICourseManager
1 public interface ICourseManager { 2 3 void studyCourse(); 4 void refundCourse(); 5 6 7 }
CourseImpl
1 public class CourseImpl implements ICourseContent,ICourseManager { 2 3 @Override 4 public String getCourseName() { 5 return null; 6 } 7 8 @Override 9 public byte[] getCourseVideo() { 10 return new byte[0]; 11 } 12 13 @Override 14 public void studyCourse() { 15 16 } 17 18 @Override 19 public void refundCourse() { 20 21 } 22 }
Diagram
方法级别的单一职责
public class Method { private void updateUserInfo(String userName,String address){ userName = "Buil"; address = "beijing"; } private void updateUserName(String userName,String address){ userName = "Buil"; } private void updateUserAddress(String userName,String address){ address = "beijing"; } private void updateUserInfo(String userName,String... properties){ userName = "Buil"; } }
未完待续