• 设计模式学习笔记(一)


    设计模式学习笔记(一)

    设计模式的目的

    ​ 在编写软件过程中,程序员面临着来种子耦合性、内聚性及可维护性,可扩展性,重用性、灵活性等多方面的挑战, 设计模式是为了让程序具有更好的

    1. 代码重用性(即:相同功能的代码,不用多次编写)
    2. 可读性(即:编程规范性,便于其他程序员的阅读和理解)
    3. 可扩展性(即:当需要增加新的功能时,非常方便)
    4. 可靠性 (即:当我们增加新的功能后,对原来的功能没有影响)
    5. 使程序呈现高内聚低耦合的特性

    设计模式七大原则

    ​ 设计模式原则,其实就是程序员在编程时,应当遵守的原则,也是各种设计模式的基础(设计模式为什么这样设计的依据)

    设计模式常用七大原则:

    1. 单一职责原则
    2. 接口隔离原则
    3. 依赖倒置原则
    4. 里氏替换员职责
    5. 开闭原则
    6. 迪米特法则
    7. 合成复用原则

    单一职责原则

    基本介绍

    ​ 对类来说,即一个类应该只负责一项职责。如果类A负责两个不同的职责:职责1,职责2。当职责1需求改变而改变A时,可能造成职责2的执行错误,所以需要将A的粒度分解为A1,A2。

    1. 降低类的复杂度,一个类只负责一项职责
    2. 提高类的可读性,可维护性
    3. 降低变更引起的风险
    4. 通常情况下,我们应当遵守单一职责原则,只有逻辑足够简单,菜可以在代码级违反单一职责原则;只有类中方法数量足够少,可以在方法级保持单一职责原则

    举例:

    public class SingleResponsibility1 {
    
        public static void main(String[] args) {
            Vehicle vehicle = new Vehicle();
            vehicle.run("摩托车");
            vehicle.run("汽车");
            vehicle.run("飞机");
    
        }
    }
    //交通工具类
    //方式1
    // 1.在方式1 的run 方法中违反了单一职责原则
    // 2.解决方案:根据交通工具的运行方式不同,分解成不同的类即可
    class Vehicle{
        public void run(String vehicle){
            System.out.println(vehicle + "在公路上跑。。。");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    public class SingleResponsibility2 {
        public static void main(String[] args) {
            RoadVehicle roadVehicle = new RoadVehicle();
            roadVehicle.run("汽车");
            roadVehicle.run("摩托车");
    
            AirVehicle airVehicle = new AirVehicle();
            airVehicle.run("飞机");
        }
    }
    
    //方案2的分析:
    //1.遵守了单一职责原则
    //2.但是这样做花销很大
    //3.改进:直接修改Vehicle类,改动的代码会比较少 ==> 方案3
    class RoadVehicle{
        public void run (String vehicle){
            System.out.println(vehicle + "在公路运行");
        }
    }
    
    
    class AirVehicle{
        public void run (String vehicle){
            System.out.println(vehicle + "在天空运行");
        }
    }
    
    class WaterVehicle{
        public void run (String vehicle){
            System.out.println(vehicle + "在水中运行");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    public class SingleResponsibility3 {
        public static void main(String[] args) {
            Vehicle2 vehicle2 = new Vehicle2();
            vehicle2.run("汽车");
            vehicle2.runAir("飞机");
            vehicle2.runWat("轮船");
        }
    }
    
    //方式三:
    //1.这种修改方法没有对原来的类做大修改,只是修改方法
    //2.这里虽然没有在类这个级别上遵守单一职责原则,但是在方法这个级别上仍然是遵守单一职责原则
    class Vehicle2{
        public void run(String vehicle){
            System.out.println(vehicle + "在公路上跑。。。");
        }
        public void runAir(String vehicle){
            System.out.println(vehicle + "在天空运行");
        }
        public void runWat(String vehicle){
            System.out.println(vehicle + "在水中运行");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    接口隔离原则(Interface Segregation Principle)

    基本介绍

    ​ 客户端不应该依赖它不需要的接口,即一个类对另一个类的依赖应该简历在最小的接口上

    应用实例:

    ​ 类A通过接口依赖于B,类C通过接口依赖于D

    interface Interface1{
        void operation1();
        void operation2();
        void operation3();
        void operation4();
        void operation5();
    }
    
    class B implements Interface1{
    
        @Override
        public void operation1() {
            System.out.println("B中实现了operation1");
        }
    
        @Override
        public void operation2() {
            System.out.println("B中实现了operation2");
        }
    
        @Override
        public void operation3() {
            System.out.println("B中实现了operation3");
    
        }
    
        @Override
        public void operation4() {
            System.out.println("B中实现了operation4");
    
        }
    
        @Override
        public void operation5() {
            System.out.println("B中实现了operation5");
        }
    }
    
    
    class D implements Interface1{
    
        @Override
        public void operation1() {
            System.out.println("D中实现了operation1");
        }
    
        @Override
        public void operation2() {
            System.out.println("D中实现了operation2");
        }
    
        @Override
        public void operation3() {
            System.out.println("D中实现了operation3");
    
        }
    
        @Override
        public void operation4() {
            System.out.println("D中实现了operation4");
    
        }
    
        @Override
        public void operation5() {
            System.out.println("D中实现了operation5");
        }
    }
    
    class A{
        public void depend1(Interface1 i){
            i.operation1();
        }
        public void depend2(Interface1 i){
            i.operation2();
        }
        public void depend3(Interface1 i){
            i.operation3();
        }
    }
    
    
    class C {
        public void depend1(Interface1 i){
            i.operation1();
        }
        public void depend4(Interface1 i){
            i.operation4();
        }
        public void depend5(Interface1 i){
            i.operation5();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93

    应传统方法的问题和使用接口隔离原则改进

    1. 类A通过接口Interface1依赖B , 类C通过接口Interface1依赖D,如果接口Interface1对于类A和类C来说不是最小接口,那么类B和类D必须去实现他们不需要的方法
    2. 将接口Interface1拆分为独立的几个接口,类A和类C分别与他们需要的接口建立依赖关系,也是采用接口隔离原则
    3. 接口Interface1中出现的方法,根据实际情况拆分为三个接口
    public class Segregation2 {
        public static void main(String[] args) {
            A a = new A();
            a.depend1(new B());  //A类通过接口去依赖B类
            a.depend2(new B());
            a.depend3(new B());
        }
    }
    
    interface Interface1{
        void operation1();
    }
    interface Interface2{
        void operation2();
        void operation3();
    }
    
    interface Interface3{
        void operation4();
        void operation5();
    }
    
    class B implements Interface1 , Interface2{
    
        @Override
        public void operation1() {
            System.out.println("B中实现了operation1");
        }
    
        @Override
        public void operation2() {
            System.out.println("B中实现了operation2");
        }
    
        @Override
        public void operation3() {
            System.out.println("B中实现了operation3");
    
        }
    }
    
    
    class D implements Interface1 , Interface3{
    
        @Override
        public void operation1() {
            System.out.println("D中实现了operation1");
        }
    
        @Override
        public void operation4() {
            System.out.println("D中实现了operation4");
        }
        @Override
        public void operation5() {
            System.out.println("D中实现了operation5");
        }
    }
    
    class A{
        public void depend1(Interface1 i){
            i.operation1();
        }
        public void depend2(Interface2 i){
            i.operation2();
        }
        public void depend3(Interface2 i){
            i.operation3();
        }
    }
    
    
    class C {
        public void depend1(Interface1 i){
            i.operation1();
        }
        public void depend4(Interface3 i){
            i.operation4();
        }
        public void depend5(Interface3 i){
            i.operation5();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
  • 相关阅读:
    【数据库迁移系列】从Oracle迁移到openGauss实战分享
    KongA 任意用户登录漏洞分析
    前端基础建设与架构02 Yarn 的安装理念及如何破解依赖管理困境
    TortoiseGit使用教程
    如何选择合适的HTTP代理服务器
    在iPhone上恢复已删除的Safari历史记录的最佳方法
    基于Spring Security添加流控
    webservice接口功能自动化测试准备工作(转)
    [附源码]java毕业设计企业物资信息管理系统
    CANTest 测试软件基本操作介绍
  • 原文地址:https://blog.csdn.net/qq_17514043/article/details/125627949