zoukankan      html  css  js  c++  java
  • 第五节:开闭原则

    开闭原则Open Closed Principle

    一、基本介绍

      1、开闭原则是编程中最基础、最重要的设计原则;

      2、一个软件实体如类,模块和函数应该对外扩展开发(对提供方),对修改关闭(对适用房)。用抽象构建框架,用实现扩展细节。

      3、当软件需要变化时,尽量通过扩展软件实体的行为来实现变化,而不是通过修改已有的代码来实现变化;

      4、编程中遵循其他原则,以及使用设计模式的目的就是遵循开闭原则

    二、引用案例

      1、下面来讨论一个画图形的功能:

        类图设计,如下:

        

      2、方式一实现:

     1 public class Ocp {
     2     public static void main(String[] args) {
     3         //测试一下,看一下存在的问题
     4         GraphicEditor graphicEditor = new GraphicEditor();
     5         graphicEditor.drawShape(new Rectangle());
     6         graphicEditor.drawShape(new Circle());
     7         graphicEditor.drawShape(new Triangle());
     8     }
     9 }
    10 
    11 /**
    12  * 分析:
    13  *  优点:
    14  *  缺点:
    15  */
    16 
    17 /**
    18  * 用于绘图的类[使用方]
    19  */
    20 class GraphicEditor {
    21     //接收  Shape对象,然后根据 type,绘制不同的图形
    22     public void drawShape(Shape s) {
    23         if (s.m_type == 1)
    24             drawRectangle(s);
    25         else if (s.m_type == 2)
    26             drawCircle(s);
    27         else if (s.m_type == 3)
    28             drawTriangle(s);
    29     }
    30 
    31     public void drawRectangle(Shape r) {
    32         System.out.println("矩形");
    33     }
    34 
    35     public void drawCircle(Shape r) {
    36         System.out.println("圆形");
    37     }
    38 
    39     //绘制三角形
    40     public void drawTriangle(Shape r) {
    41         System.out.println("三角形");
    42     }
    43 
    44 }
    45 
    46 /**
    47  * 基类
    48  */
    49 class Shape {
    50     int m_type;
    51 }
    52 
    53 class Rectangle extends Shape {
    54     Rectangle() {
    55         super.m_type = 1;
    56     }
    57 }
    58 
    59 class Circle extends Shape {
    60     Circle() {
    61         super.m_type = 2;
    62     }
    63 }
    64 
    65 /**
    66  * 新增绘画三角形
    67  */
    68 class Triangle extends Shape {
    69     Triangle() {
    70         super.m_type = 3;
    71     }
    72 }

        分析:

        (1)优点是比较好理解,简单易操作;

        (2)缺点是违反了设计模式的 OCP 原则,即对扩展开放(提供方),对修改关闭(使用方)。即当我们给类增加新功能的时候,尽量不要修改代码,或者尽可能少修改代码。

        (3)比如我们这时要新增一个图形种类 三角形,需要做的修改比较多。

      3、方式二

        思路:把创建 Shape 类做成抽象类,并提供一个抽象的 draw 方法,让子类去实现即可,这样我们有新的图形种类时,只需要让新的图形类继承 Shape,并实现 draw 方法即可,使用方的代码就不需要修改。——>满足了开闭原则

        改进后的代码:

     1 public class Ocp {
     2     public static void main(String[] args) {
     3         GraphicEditor graphicEditor = new GraphicEditor();
     4         graphicEditor.drawShape(new Circle());
     5         graphicEditor.drawShape(new OtherGraphic());
     6     }
     7 
     8 }
     9 
    10 class GraphicEditor {
    11     public void drawShape(Shape s) {
    12         s.draw();
    13     }
    14 }
    15 
    16 abstract class Shape {
    17     int m_type;
    18 
    19     //抽象方法
    20     public abstract void draw();
    21 }
    22 
    23 class Rectangle extends Shape {
    24     Rectangle() {
    25         super.m_type = 1;
    26     }
    27 
    28 
    29     @Override
    30     public void draw() {
    31         System.out.println("绘制矩形");
    32     }
    33 }
    34 
    35 class Circle extends Shape {
    36     Circle() {
    37         super.m_type = 2;
    38     }
    39 
    40 
    41     @Override
    42     public void draw() {
    43         System.out.println("绘制圆形");
    44     }
    45 }
    46 
    47 class Triangle extends Shape {
    48     Triangle() {
    49         super.m_type = 3;
    50     }
    51 
    52 
    53     @Override
    54     public void draw() {
    55         System.out.println("绘制三角形");
    56     }
    57 }
    58 
    59 /**
    60  * 新增一个图形
    61  */
    62 class OtherGraphic extends Shape {
    63     OtherGraphic() {
    64         super.m_type = 4;
    65     }
    66 
    67 
    68     @Override
    69     public void draw() {
    70         System.out.println("绘制其他形状");
    71     }
    72 }
  • 相关阅读:
    蓝桥杯 全球变暖(dfs)
    Bzoj3196 Tyvj 1730 二逼平衡树
    Bzoj3110 [Zjoi2013]K大数查询
    Bzoj4004 [JLOI2015]装备购买
    Bzoj2115 [Wc2011] Xor
    Bzoj1257 [CQOI2007]余数之和sum
    HDU1724 Ellipse
    Bzoj2178 圆的面积并
    SPOJ CIRU The area of the union of circles
    CodeForces 232E.Quick Tortoise
  • 原文地址:https://www.cnblogs.com/niujifei/p/14208653.html
Copyright © 2011-2022 走看看