• 设计模式-开闭原则和迪米特法则


    开闭原则

    基本介绍

    1. 开闭原则(Open Closed Principle) 是编程中最基础、最重要的设计原则
    2. 一个软件实体如类,模块和函数应该对扩展开放(对提供方)对修改关闭(对使用方)。用抽象构建框架,用实现扩展细节。
    3. 当软件需要变化时,尽量通过扩展软件实体的行为来实现变化,而不是通过修改已有的代码来实现变化。
    4. 编程中遵循其它原则,以及使用设计模式的目的就是遵循开闭原则

    应用实例

    1. public class OCP {
    2. public static void main(String[] args) {
    3. // 使用看看存在的问题
    4. GraphEditor graphEditor = new GraphEditor();
    5. graphEditor.drawShape(new Rectangle());
    6. graphEditor.drawShape(new Circle());
    7. graphEditor.drawShape(new Triangle());
    8. }
    9. }
    10. // 这是一个用于绘图的类[使用方]
    11. class GraphEditor {
    12. // 接收Shape对象,然后根据type,来绘制不同的图形
    13. public void drawShape(Shape s){
    14. if (s.mType == 1){
    15. drawRectangle(s);
    16. }else if (s.mType == 2){
    17. drawCircle(s);
    18. }else if (s.mType == 3){
    19. drawTriangle(s);
    20. }
    21. }
    22. // 绘制矩形
    23. public void drawRectangle(Shape r){
    24. System.out.println("绘制矩形");
    25. }
    26. // 绘制圆形
    27. public void drawCircle(Shape r){
    28. System.out.println("绘制圆形");
    29. }
    30. // 绘制三角形
    31. public void drawTriangle(Shape r){
    32. System.out.println("绘制三角形");
    33. }
    34. }
    35. // Shape类,基类
    36. class Shape {
    37. int mType;
    38. }
    39. // 矩形类
    40. class Rectangle extends Shape {
    41. public Rectangle() {
    42. super.mType = 1;
    43. }
    44. }
    45. // 圆形类
    46. class Circle extends Shape {
    47. public Circle() {
    48. super.mType = 2;
    49. }
    50. }
    51. // 新增三角形
    52. class Triangle extends Shape{
    53. public Triangle(){
    54. super.mType = 3;
    55. }
    56. }

    改进

    1. public class OCP {
    2. public static void main(String[] args) {
    3. // 使用看看存在的问题
    4. GraphEditor graphEditor = new GraphEditor();
    5. graphEditor.drawShape(new Rectangle());
    6. graphEditor.drawShape(new Circle());
    7. graphEditor.drawShape(new Triangle());
    8. graphEditor.drawShape(new OtherGraphic());
    9. }
    10. }
    11. // 这是一个用于绘图的类[使用方]
    12. class GraphEditor {
    13. // 接收Shape对象,调用draw方法
    14. public void drawShape(Shape s){
    15. s.draw();
    16. }
    17. }
    18. // Shape类,基类
    19. abstract class Shape {
    20. int mType;
    21. public abstract void draw();
    22. }
    23. // 矩形类
    24. class Rectangle extends Shape {
    25. public Rectangle() {
    26. super.mType = 1;
    27. }
    28. @Override
    29. public void draw() {
    30. System.out.println("绘制矩形");
    31. }
    32. }
    33. // 圆形类
    34. class Circle extends Shape {
    35. public Circle() {
    36. super.mType = 2;
    37. }
    38. @Override
    39. public void draw() {
    40. System.out.println("绘制圆形");
    41. }
    42. }
    43. // 新增三角形
    44. class Triangle extends Shape{
    45. public Triangle(){
    46. super.mType = 3;
    47. }
    48. @Override
    49. public void draw() {
    50. System.out.println("绘制三角形");
    51. }
    52. }
    53. // 新增一个图形
    54. class OtherGraphic extends Shape{
    55. public OtherGraphic(){
    56. super.mType = 4;
    57. }
    58. @Override
    59. public void draw() {
    60. System.out.println("绘制其它图形");
    61. }
    62. }

    迪米特法则 

    基本介绍

    1. 一个对象应该对其他对象保持最少的了解
    2. 类与类关系越密切,耦合度越大
    3. 迪米特法则(Demeter Principle)又叫最少知道原则,即-一个类对自已依赖的类知道的越少越好。也就是说,对于被依赖的类不管多么复杂,都尽量将逻辑封装在类的内部。对外除了提供的public方法,不对外泄露任何信息
    4. 迪米特法则还有个更简单的定义:只与直接的朋友通信
    5. 直接的朋友:每个对象都会与其他对象有耦合关系,只要两个对象之间有耦合关系,我们就说这两个对象之间是朋友关系。耦合的方式很多,依赖,关联,组合,聚合等。其中,我们称出现成员变量,方法参数,方法返回值中的类为直接的朋友,而出现在局部变量中的类不是直接的朋友。也就是说,陌生的类最好不要以局部变量的形式出现在类的内部。

    注意事项和细节

    1. 迪米特法则的核心是降低类之间的耦合
    2. 但是注意:由于每个类都减少了不必要的依赖,因此迪米特法则只是要求降低类间(对象间)耦合关系,并不是要求完全没有 依赖关系
  • 相关阅读:
    Linux下的网络编程——C/S模型TCP(二)
    基于PaddleOCR的多视角集装箱箱号检测识别
    Spark RDD、DataFrame和Dataset的区别和联系
    cadence SPB17.4 - allegro - DRC ERROR - Soldermask to Shape Spacing
    分布式消息通信之Kafka的实现原理
    力扣:165. 比较版本号(Python3)
    JDBC的基本使用(mysql与java)
    私有化轻量级持续集成部署方案--05-持续部署服务-Drone(下)
    【Linux】多路IO复用技术③——epoll详解&如何使用epoll模型实现简易的一对多服务器(附图解与代码实现)
    JS操作字符串面试题系列(2)-每天做5题
  • 原文地址:https://blog.csdn.net/z2698751935/article/details/137677171