zoukankan      html  css  js  c++  java
  • 七大原则一--->单一职责原则

    单一职责原则

          即一个类应该只负责一项职责

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

    出现违背单一原则的情况  run方法违背了单一职责原则 不同交通工具应该有不同的运行

    package com.wf.zhang.singleresponsibility;
    
    /**
     * 设计模式七大原则           单一职责原则 类级别  一个类负责一个职责
     * 
     * @author wf.zhang
     * 
     *         出现问题===>类出现违背单一职责原则
     */
    public class SingleResponseibility1 {
    
        public static void main(String[] args) {
    
            Vehicle v = new Vehicle();
            v.run("汽车");
            v.run("轮船");
            v.run("飞机");
        }
    
    }
    
    /**
     * 交通工具类
     * 
     * @author wf.zhang
     *
     */
    class Vehicle {
        public void run(String vehicle) {
            System.out.println(vehicle + "在公路上运行...");
        }
    }
    View Code

     解决一   定义多个类 成本高

    package com.wf.zhang.singleresponsibility;
    
    /**
     * 解决 1 定义单独的类 (类分解) 使用不同类的的方法 实现不同需求
     * 
     * @author wf.zhang
     *
     */
    public class SingleResponseibility2 {
    
        public static void main(String[] args) {
    
            RoadVehicle roadVehicle = new RoadVehicle();
            roadVehicle.run("汽车");
            WaterVehicle waterVehicle = new WaterVehicle();
            waterVehicle.run("轮船");
            SkyVehicle skyVehicle = new SkyVehicle();
            skyVehicle.run("飞机");
        }
    
    }
    
    /**
     * 交通工具类
     * 
     * @author wf.zhang
     *
     */
    class RoadVehicle {
        public void run(String vehicle) {
            System.out.println(vehicle + "在公路上运行...");
        }
    }
    
    class WaterVehicle {
        public void run(String vehicle) {
            System.out.println(vehicle + "在水路上运行...");
        }
    }
    
    class SkyVehicle {
        public void run(String vehicle) {
            System.out.println(vehicle + "在天空上运行...");
        }
    }

     解决二   (推荐)

    package com.wf.zhang.singleresponsibility;
    
    /**
     * 解决 2    类 (方法分解) 定义不同方法
     * 
     *       类遵守单一原则    方法没有遵守单一原则
     * @author wf.zhang
     */
    public class SingleResponseibility3 {
    
        public static void main(String[] args) {
    
            CommonVehicle commonVehicle = new CommonVehicle();
            commonVehicle.run("汽车");
            commonVehicle.runWater("轮船");
            commonVehicle.runSky("飞机");
        }
    
    }
    
    /**
     * 交通工具类
     * 
     * @author wf.zhang
     *
     */
    class CommonVehicle {
        public void run(String vehicle) {
            System.out.println(vehicle + "在公路上运行...");
        }
    
        public void runWater(String vehicle) {
            System.out.println(vehicle + "在水路上运行...");
        }
    
        public void runSky(String vehicle) {
            System.out.println(vehicle + "在天空上运行...");
        }
    }

    古人学问无遗力,少壮工夫老始成。 纸上得来终觉浅,绝知此事要躬行。
  • 相关阅读:
    有序向量
    无序向量
    设计模式入门
    策略模式
    面向对象相关知识点
    MySQL数据库知识培训
    数据库业务规范
    go最小路径
    go求质数
    CSS3
  • 原文地址:https://www.cnblogs.com/wf-zhang/p/12291792.html
Copyright © 2011-2022 走看看