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.cs
    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();
    }
    /// <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();
        }
    }
}
相关推荐
ZHE|张恒2 小时前
设计模式实战篇(一):彻底搞懂 Singleton 单例模式
单例模式·设计模式
喝拿铁写前端16 小时前
从面条代码到抽象能力:一个小表单场景里的前端成长四阶段
前端·设计模式·架构
依米_16 小时前
一文带你剖析 Promise.then all 实现原理,状态机、发布订阅模式完美实现异步编程
javascript·设计模式
jzhwolp17 小时前
从基本链表到侵入式链表,体会内核设计思路
c语言·后端·设计模式
李宥小哥1 天前
结构型设计模式1
设计模式
lapiii3581 天前
[智能体设计模式] 第五章 :函数调用
microsoft·设计模式
lapiii3581 天前
[智能体设计模式] 第 1 章:提示链(Prompt Chaining)
设计模式·prompt
昨天的猫1 天前
《拒绝重复代码!模板模式教你优雅复用算法骨架》
后端·设计模式
L.EscaRC1 天前
ArkTS分布式设计模式浅析
分布式·设计模式·arkts
Arva .1 天前
责任链设计模式->规则树
设计模式