• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
gooliugle
博客园    首页    新随笔    联系   管理    订阅  订阅
大话设计模式读书笔记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 }


 

posted on 2010-04-23 23:20  gooliugle  阅读(440)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3