• 设计模式笔记:开闭原则(OCP,The Open-Closed Principle)


    1. 开闭原则概述

      开闭原则(OCP,The Open-Closed Principle)两个主要特征:

      (1)对扩展开放(open for extension):模块的行为的可以扩展的,当应用的需求改变时,可以对模块进行扩展。

      (2)对修改关闭(closed for modification):对模块进行扩展时,不必改动模块的源代码

      开闭原则是面向对象设计中可复用设计的基石。

    2. 开闭原则的实现

      开闭原则实现关键:抽象。

      抽象基类:把系统的所有可能的行为抽象成一个抽象底层,这个抽象底层规定出所有的具体实现必须提供的方法的特征。作为系统设计的抽象层,要预见所有可能的扩展,从而使得在任何扩展情况下,系统的抽象底层不需修改。

      派生类:从抽象基类派生一个或多个新的具体实现,可以扩展基类的行为,系统设计对扩展开放。

    3. 如何使用开闭原则

      抽象约束:

      (1)通过接口或者抽象类约束扩展,对扩展进行边界限定,不允许出现在接口或抽象类中不存在的public方法

      (2)参数类型、引用对象尽量使用接口或者抽象类,而不是实现类;

      (3)抽象层尽量保持稳定,一旦确定即不允许修改。

      让设计对于最有可能发生的变化遵循OCP原则。遵循OCP原则的代价是很昂贵的,创建适当的对象需要开发时间和精力,抽象增加软件复杂度。把OCP应用限定在最有可能发生的变化上。

    4. 开闭原则的优点

      (1)可复用性

      (2)可维护性

    5.开闭原则重构

      违反开闭原则(OCP)原则的重构可采取设计模式:策略模式(Strategy)模板方法模式(Template Method)

    6. 开闭原则示例

      Shape.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace DesignPatterns.OpenClosedPrinciple
    {
        public abstract class Shape
        {
            protected string _name;
    
            public Shape(string name)
            {
                this._name = name;
            }
    
            /// <summary>
            /// 面积
            /// </summary>
            /// <returns></returns>
            public abstract double Area();
    
            /// <summary>
            /// 显示
            /// </summary>
            public abstract void Display();
        }
    }

      Rectangle.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace DesignPatterns.OpenClosedPrinciple
    {
        /// <summary>
        /// 矩形
        /// </summary>
        public class Rectangle : Shape
        {
            private double _width;
            private double _height;
    
            public Rectangle(string name, double width, double height)
                : base(name)
            {
                this._width = width;
                this._height = height;
            }
    
            public override double Area()
            {
                return _width * _height;
            }
    
            public override void Display()
            {
                Console.WriteLine("{0} 长:{1},宽:{2},面积:{3}", _name, _width, _height, this.Area());
            }
        }
    }

      Circle.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace DesignPatterns.OpenClosedPrinciple
    {
        /// <summary>
        /// 圆形
        /// </summary>
        public class Circle : Shape
        {
            private double _radius;
    
            public Circle(string name, double radius)
                : base(name)
            {
                this._radius = radius;
            }
    
            public override double Area()
            {
                return Math.Round(Math.PI * _radius * _radius, 2);
            }
    
            public override void Display()
            {
                Console.WriteLine("{0} 半径:{1},面积:{2}", _name, _radius, this.Area());
            }
        }
    }
  • 相关阅读:
    取消Git每次拉取、提交推送都要输入密码
    input输入框的oninput和onchange事件
    NPM学习笔记
    命令行语法格式中常用符号的含义
    npm卸载模块报错
    软件的Alpha、Beta、GM、OEM、LTS等版本的含义(转)
    MySQL常用指令
    Git常用指令
    .net Core 解决Form value count limit 1024 exceeded. (文件上传过大)
    EF Core 迁移整理
  • 原文地址:https://www.cnblogs.com/libingql/p/3804655.html
Copyright © 2020-2023  润新知