当前位置: 首页 > news >正文

C#设计模式之--六大原则 开闭原则

设计模式六大原则是单一职责原则、里氏替换原则、依赖倒置原则、接口隔离原则、迪米特法则、开闭原则。它们不是要我们刻板的遵守,而是根据实际需要灵活运用。只要对它们的遵守程度在一个合理的范围内,努为做到一个良好的设计。本文主要介绍一下.NET(C#) 开闭原则。

 

开闭原则(Open Closed Principle)

开闭原则(Open-Closed Principle,OCP)是指一个软件实体(如类、模块和函数)应该对扩展开放,对修改关闭。如当一个模块需要修改的时,不应该直接修改源代码,这样有可能对现有的工作造成影响。应该通过拓展来实现新需求。

例如,

1)一般的反面设计实现

using System;
namespace ConsoleApplication
{/// <summary>/// 矩形(Shape.cs)/// </summary>public class Shape{private double _width;private double _height;private double _radius;private string _name;public Shape(string name, double width, double height){this._width = width;this._height = height;_name = name;}public double AreaRectangle(){return _width * _height;}public void DisplayRectangle(){Console.WriteLine("{0} 长:{1},宽:{2},面积:{3}", _name, _width, _height, this.AreaRectangle());}public Shape(string name, double radius){this._radius = radius;this._name = name;}public double AreaCircle(){return Math.Round(Math.PI * _radius * _radius);}public void DisplayCircle(){Console.WriteLine("{0} 半径:{1},面积:{2}", _name, _radius, this.AreaCircle());}}class Program{static void Main(string[] args){Shape circle = new Shape("圆", 1);circle.DisplayCircle();Shape rectangle = new Shape("正方形", 100, 100);rectangle.DisplayRectangle();Console.ReadKey();}}
}

 2)开闭原则的实现

using System;
namespace ConsoleApplication
{//Shape.cspublic 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();}/// <summary>/// 矩形(Rectangle.cs)/// </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());}}/// <summary>/// 圆形(Circle.cs)/// </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);}public override void Display(){Console.WriteLine("{0} 半径:{1},面积:{2}", _name, _radius, this.Area());}}class Program{static void Main(string[] args){   Shape circle = new Circle("圆", 1);circle.Display();Shape rectangle = new Rectangle("正方形", 100, 100);rectangle.Display();Console.ReadKey();}}
}

 

http://www.lryc.cn/news/137196.html

相关文章:

  • 编写Dockerfile制作自己的镜像并推送到私有仓库
  • 华为OD-分积木/分苹果
  • Mysql的引擎有哪些?支持事物么?DB储存引擎有哪些?
  • 【懒加载】js实现懒加载、vue实现图片懒加载指令
  • 微信小程序教学系列(7)
  • Android 9.0 kenel和frameworks中修改ram运行内存的功能实现
  • PHP实践:获取网络上图片的长宽以及图片类型
  • 使用 DPO 微调 Llama 2
  • 数据库——事务,事务隔离级别
  • 对《VB.NET通过VB6 ActiveX DLL调用PowerBasic及FreeBasic动态库》的改进
  • 【PHP】数据类型运算符位运算
  • 使用 Nacos 作为 Spring Boot 配置中心
  • 微服务 Eureka
  • Spring Boot 事务和事务传播机制
  • 计算机组成原理(巨巨巨基础篇)
  • C语言:选择+编程(每日一练Day7)
  • leetcode做题笔记93. 复原 IP 地址
  • HTTPS 中间人攻击
  • MATLAB打开excel读取写入操作例程
  • [C语言]分支与循环
  • 绘制区块链之链:解码去中心化、安全性和透明性的奇迹
  • 4G工业路由器的功能与选型!详解工作原理、关键参数、典型品牌
  • c与c++中struct的主要区别和c++中的struct与class的主要区别
  • mysql中char_length()和length()
  • Numpy学习笔记
  • LAMP配置与应用
  • Dockerfile搭建LNMP运行Wordpress平台
  • 数据库第十五课-------------非关系型数据库----------Redis
  • BM2 链表内指定区间反转,为什么链表要new一个结点?
  • SQL阶段性优化