昊漫玉 发表于 2015-6-17 07:52:04

大话设计模式读书笔记3----开放封闭原则(OCP)

  开放封闭原则(OCP):软件实体(类、模块、函数等)应该是可以扩展的,但是不可修改。
  1、对于扩展是开放的(open for extension)。这意味着模块的行为是可以扩展的。当应用的需求改变时,我们可以对模块扩展,使其满足那些改变的新行为。
  2、对于修改是封闭的(closed for modification)。对模块进行扩展时,不必改动模块的源代码或者二进制代码。

  代码来源:敏捷软件开发(C#版)

代码

1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4
5 namespace Shape
6 {
7   class Program
8   {
9         static void Main(string[] args)
10         {
11         }
12   }
13   public interface Shape
14   {
15         void Draw();
16   }
17   public class Square : Shape
18   {
19
20         public void Draw()
21         {
22             //draw a square
23         }
24   }
25   public class Circle:Shape
26   {
27         public void Draw
28         {
29             //draw a circle
30         }
31   }
32   
33 }  

  
页: [1]
查看完整版本: 大话设计模式读书笔记3----开放封闭原则(OCP)