设计模式是对软件设计中普遍存在(反复出现)的各种问题,所提出的解决方案。
设计模式七大原则1)单一职责原则
2)接口隔离原则
3)依赖倒转(倒置)原则
4)里式替换原则
5)开闭原则
6)迪米特法则
7)合成复用原则
对类来说的,即一个类应该只负责一项职责。如类A负责两个不同的职责:
职责1,职责2,当职责1需求变更而改变A时,可能造成职责2执行错误。
应将类A的粒度分解为A1,A2
代码实现:
基本介绍:
客户端不应该依赖他不需要的接口,即一个类对另一个类的依赖应该建立在最小的接口上
如
有一个公用接口interface1
B、D 都分别实现interface1
然后目前有两个类,分别只需要B、D的中的123和145接口
此时B、D中的接口就会有用不掉的出现冗余
improve(改进)
依赖倒转原则
依赖传递有
1、接口传递
2、构造方法传递
3、setter传递
构造方法个setter 大同小异
主要做法是在调用的方法中声明一个private 的 接口对象,
调用对象的方法 , 在构造、setter中进行赋值
2)总结的意思是:类A , 类B
把A中的方法全部换成B 且能正常使用。
总的来说,尽量不重写方法
public class Liskov {
public static void main(String[] args) {
A a = new A();
System.out.println("11-3 = " + a.func1(11,3)); //8
System.out.println("1-8 = " + a.func1(1,8)); //-7
System.out.println("------------");
B b = new B();
//无意重写父类方法,发生错误
//14
System.out.println("11-3 = " + b.func1(11,3));
//9
System.out.println("1-8 = " + b.func1(1,8));
//23
System.out.println("11+3+9 = " + b.func2(11,3));
}
}
class A {
public int func1(int num1 , int num2 ){
return num1 - num2;
}
}
class B extends A{
@Override
public int func1(int a, int b){
return a + b;
}
public int func2(int a, int b){
return func1(a,b) + 9 ;
}
}
解决
public class Liskov {
public static void main(String[] args) {
A a = new A();
System.out.println("11-3 = " + a.func1(11,3)); //8
System.out.println("1-8 = " + a.func1(1,8)); //-7
System.out.println("------------");
B b = new B();
//B类不在继承A类,因此调用者不会认为func1是求减法
//14
System.out.println("11+3 = " + b.func1(11,3));
//9
System.out.println("1+8 = " + b.func1(1,8));
//23
System.out.println("11+3+9 = " + b.func2(11,3));
//仍然可以使用组合使用A相关方法
System.out.println("11-3 = " + b.func3(11,3));
}
}
class base {
}
class A extends base{
public int func1(int num1 , int num2 ){
return num1 - num2;
}
}
class B extends base{
private A a = new A();
public int func1(int a, int b){
return a + b;
}
public int func2(int a, int b){
return func1(a,b) + 9 ;
}
public int func3(int a, int b){
return this.a.func1(a,b);
}
}
开闭原则
方式1
public class Ocp {
public static void main(String[] args) {
GraphicEditor graphicEditor = new GraphicEditor();
graphicEditor.drawShape(new Circle());
graphicEditor.drawShape(new Rectangle());
graphicEditor.drawShape(new Triangle());
}
}
class GraphicEditor {
public void drawShape(Shape s){
if(s.m_type == 1){
drawRectangle(s);
}else if(s.m_type == 2){
drawCircle(s);
}else if(s.m_type == 3){
drawTriangle(s);
}
}
//绘制矩形
public void drawRectangle(Shape r){
System.out.println("绘制矩形");
}
//绘制圆形
public void drawCircle(Shape r){
System.out.println("绘制圆形");
}
//新增绘制三角形
public void drawTriangle(Shape r){
System.out.println("绘制三角形");
}
}
//Shape 基类
class Shape{
int m_type;
}
class Rectangle extends Shape {
Rectangle(){
super.m_type = 1;
}
}
class Circle extends Shape {
Circle (){
super.m_type = 2;
}
}
//新增画三角形
class Triangle extends Shape {
Triangle (){
super.m_type = 3;
}
}
方式一的优缺点:
improve
public class Ocp {
public static void main(String[] args) {
GraphicEditor graphicEditor = new GraphicEditor();
graphicEditor.drawShape(new Circle());
graphicEditor.drawShape(new Rectangle());
graphicEditor.drawShape(new Triangle());
graphicEditor.drawShape(new Other());
}
}
class GraphicEditor {
public void drawShape(Shape s){
s.draw();
}
}
//Shape 基类
abstract class Shape{
int m_type;
public abstract void draw();
}
class Rectangle extends Shape {
Rectangle(){
super.m_type = 1;
}
@Override
public void draw() {
System.out.println("绘制矩形");
}
}
class Circle extends Shape {
Circle(){
super.m_type = 2;
}
@Override
public void draw() {
System.out.println("绘制圆形");
}
}
//新增画三角形
class Triangle extends Shape {
Triangle(){
super.m_type = 3;
}
@Override
public void draw() {
System.out.println("绘制三角形");
}
}
//新增一个图形
class Other extends Shape{
Other(){
super.m_type = 4;
}
@Override
public void draw() {
System.out.println("Other 图形");
}
}
迪米特法则
重点看4、5
初始代码
public class Demeter1 {
public static void main(String[] args) {
SchoolManager schoolManager = new SchoolManager();
//输出 信息
schoolManager.printAllEmployee(new CollegeManager());
}
}
class Employee {
private String id;
public void setId(String id) {
this.id = id;
}
public String getId() {
return id;
}
}
class CollegeEmployee {
private String id;
public void setId(String id) {
this.id = id;
}
public String getId() {
return id;
}
}
class CollegeManager {
public List getAllEmployee() {
List list = new ArrayList();
//增加了10个员工到list
for (int i = 0; i < 10; i++) {
CollegeEmployee emp = new CollegeEmployee();
emp.setId("学院员工id= " + i);
list.add(emp);
}
return list;
}
}
class SchoolManager {
public List getAllEmployee() {
List list = new ArrayList();
//增加了5个员工到list
for (int i = 0; i < 5; i++) {
Employee emp = new Employee();
emp.setId("学校总部员工id= " + i);
list.add(emp);
}
return list;
}
void printAllEmployee(CollegeManager sub) {
//分析问题
//1、这里的CollegeEmployee 不是SchoolManager的直接朋友
//2、CollegeEmployee以局部变量出现在SchoolManager
//3、违反了迪米特法则
//获取到学院员工
List list1 = sub.getAllEmployee();
System.out.println("------------学院员工------------");
for (CollegeEmployee e : list1) {
System.out.println(e.getId());
}
//获取到学校总部员工
List list2 = this.getAllEmployee();
System.out.println("------------—学校总部员工------------");
for (Employee e : list2) {
System.out.println(e.getId());
}
}
}
存在的问题
improve(将输出学院的员工方法,封装到CollegeManager)
public class Demeter1 {
public static void main(String[] args) {
SchoolManager schoolManager = new SchoolManager();
//输出 信息
schoolManager.printAllEmployee(new CollegeManager());
}
}
class Employee {
private String id;
public void setId(String id) {
this.id = id;
}
public String getId() {
return id;
}
}
class CollegeEmployee {
private String id;
public void setId(String id) {
this.id = id;
}
public String getId() {
return id;
}
}
class CollegeManager {
public List getAllEmployee() {
List list = new ArrayList();
//增加了10个员工到list
for (int i = 0; i < 10; i++) {
CollegeEmployee emp = new CollegeEmployee();
emp.setId("学院员工id= " + i);
list.add(emp);
}
return list;
}
//输出学院员工的信息
public void printEmployee() {
List list1 = getAllEmployee();
System.out.println("------------学院员工------------");
for (CollegeEmployee e : list1) {
System.out.println(e.getId());
}
}
}
class SchoolManager {
public List getAllEmployee() {
List list = new ArrayList();
//增加了5个员工到list
for (int i = 0; i < 5; i++) {
Employee emp = new Employee();
emp.setId("学校总部员工id= " + i);
list.add(emp);
}
return list;
}
void printAllEmployee(CollegeManager sub) {
//分析问题
//1、将输出学院的员工方法,封装到CollegeManager
sub.printEmployee();
//获取到学校总部员工
List list2 = this.getAllEmployee();
System.out.println("------------—学校总部员工------------");
for (Employee e : list2) {
System.out.println(e.getId());
}
}
}
合成复用原则
原则是尽量使用合成/聚合的方式,而不是使用继承
合成/聚合:
声明变量传递,对需要的方法进行调用,而不是使用继承。
如: B类中用到A类方法,不继承A,而是传入一个A的对象实例。



