设计模式-遵守原则(java)

设计模式是什么?

设计模式是对软件设计中普遍存在(反复出现)的各种问题,所提出的解决方案。

设计模式七大原则

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)); 
    }
}

/**
 * A类
 */
class A {

    /**
     * @return 返回两个数的差
     */
    public int func1(int num1 , int num2 ){
        return num1 - num2;
    }
}

/**
 * B类继承A
 * 增加了一个新功能,完成两个数相加,然后和9求和
 */
class B extends A{

    /**
     * 无意 重写了父类方法
     * @return
     */
    @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 {


}


/**
 * A类
 */
class A extends Base{

    /**
     * @return 返回两个数的差
     */
    public int func1(int num1 , int num2 ){
        return num1 - num2;
    }
}

/**
 * B类继承A
 * 增加了一个新功能,完成两个数相加,然后和9求和
 */
class B extends Base{

    /**
     *如果B需要使用A类方法,使用组合关系
     */
    private A a = new A();

    /**
     * 重写了父类方法,可能是无意的
     * @return
     */
    public int func1(int a, int b){
        return a + b;
    }

    public int func2(int a, int b){
        return func1(a,b) + 9 ;
    }

    /**
     * 我们仍然想使用A的方法
     */
    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 {

    /**
     * 接收Shape对象,然后根据type,来绘制不同的图形
     * @param s
     */
    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 {

    /**
     * 接收Shape对象,然后根据type,来绘制不同的图形
     * @param s
     */
    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<CollegeEmployee> getAllEmployee() {
        List<CollegeEmployee> list = new ArrayList<CollegeEmployee>();
        //增加了10个员工到list
        for (int i = 0; i < 10; i++) {
            CollegeEmployee emp = new CollegeEmployee();
            emp.setId("学院员工id= " + i);
            list.add(emp);
        }
        return list;
    }
}

/**
 * 学校管理类
 * 分析 SchoolManager 类的直接朋友类有哪些  Employee、  CollegeManager
 * CollegeEmployee不是直接朋友(局部变量),这样违反了迪米特法则
 *
 */
class SchoolManager {
    /**
     * @return 返回学校总部的员工
     */
    public List<Employee> getAllEmployee() {
        List<Employee> list = new ArrayList<Employee>();
        //增加了5个员工到list
        for (int i = 0; i < 5; i++) {
            Employee emp = new Employee();
            emp.setId("学校总部员工id= " + i);
            list.add(emp);
        }
        return list;
    }

    /**
     * 该方法完成输出学校总部和学院员工信息的方法(id)
     * @param sub
     */
    void printAllEmployee(CollegeManager sub) {


        //分析问题
        //1、这里的CollegeEmployee 不是SchoolManager的直接朋友
        //2、CollegeEmployee以局部变量出现在SchoolManager
        //3、违反了迪米特法则
        //获取到学院员工
        List<CollegeEmployee> list1 = sub.getAllEmployee();
        System.out.println("------------学院员工------------");
        for (CollegeEmployee e : list1) {
            System.out.println(e.getId());
        }
        //获取到学校总部员工
        List<Employee> 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<CollegeEmployee> getAllEmployee() {
        List<CollegeEmployee> list = new ArrayList<CollegeEmployee>();
        //增加了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<CollegeEmployee> list1 = getAllEmployee();
        System.out.println("------------学院员工------------");
        for (CollegeEmployee e : list1) {
            System.out.println(e.getId());
        }
    }
}

/**
 * 学校管理类
 * 分析 SchoolManager 类的直接朋友类有哪些  Employee、  CollegeManager
 * CollegeEmployee不是直接朋友(局部变量),这样违反了迪米特法则
 *
 */
class SchoolManager {
    /**
     * @return 返回学校总部的员工
     */
    public List<Employee> getAllEmployee() {
        List<Employee> list = new ArrayList<Employee>();
        //增加了5个员工到list
        for (int i = 0; i < 5; i++) {
            Employee emp = new Employee();
            emp.setId("学校总部员工id= " + i);
            list.add(emp);
        }
        return list;
    }

    /**
     * 该方法完成输出学校总部和学院员工信息的方法(id)
     * @param sub
     */
    void printAllEmployee(CollegeManager sub) {


        //分析问题
        //1、将输出学院的员工方法,封装到CollegeManager
        sub.printEmployee();
        //获取到学校总部员工
        List<Employee> list2 = this.getAllEmployee();
        System.out.println("------------—学校总部员工------------");
        for (Employee e : list2) {
            System.out.println(e.getId());
        }
    }
}

在这里插入图片描述

合成复用原则

原则是尽量使用合成/聚合的方式,而不是使用继承
合成/聚合:
声明变量传递,对需要的方法进行调用,而不是使用继承。
如: B类中用到A类方法,不继承A,而是传入一个A的对象实例。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值