设计模式笔记:开闭原则(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()); } } }
分类:
设计模式
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构