Open closed principle
#include <iostream> using namespace std; class Book
{
public:
string getContents()
{
return "Long time ago,There is a temple in the mountain";
}
}; class Paper
{
public:
string getContents()
{
return "I am handsome";
}
}; class Mother
{
public:
void tellstory(Book* b)
{
cout << b->getContents() << endl;
}
void tellstory(Paper* p)
{
cout << p->getContents() << endl;
}
}; int main(int argc, char *argv[])
{
Book b;
Mother m;
Paper p;
m.tellstory(&b);
m.tellstory(&p);
return ;
}
#include <iostream> using namespace std;
class iReader
{
public:
virtual string getContents() = 0;//only provide a interface
};
class Book : public iReader
{
public:
string getContents()
{
return "Long time ago,There is a temple in the mountain";
}
}; class Paper : public iReader
{
public:
string getContents()
{
return "I am handsome";
}
}; class Mother
{
public:
void tellstory(iReader * r)
{
cout << r->getContents() << endl;
}
}; int main(int argc, char *argv[])
{
Book b;
Mother m;
Paper p;
m.tellstory(&b);
m.tellstory(&p);
return 0;
}

Open closed principle的更多相关文章
- 开放封闭原则(Open Closed Principle)
在面向对象的设计中有很多流行的思想,比如说 "所有的成员变量都应该设置为私有(Private)","要避免使用全局变量(Global Variables)",& ...
- 面向对象设计原则 开放封闭原则(Open Closed Principle)
开放封闭原则(OCP,Open Closed Principle) 开放封闭原则是所有面向对象原则的核心. 软件设计本身所追求的目标就是封装变化.降低耦合,而开放封闭原则正是对这一目标的最直接体现. ...
- 北风设计模式课程---开放封闭原则(Open Closed Principle)
北风设计模式课程---开放封闭原则(Open Closed Principle) 一.总结 一句话总结: 抽象是开放封闭原则的关键. 1."所有的成员变量都应该设置为私有(Private)& ...
- 开闭原则(Open Closed Principle,OCP)
遵循开闭原则设计出的模块具有两个主要特征: 对于扩展是开放的(Open for extension).这意味着模块的行为是可以扩展的.当应用的需求改变时,我们可以对模块进行扩展,使其具有满足那些改变的 ...
- 开放封闭原则(OCP,Open Closed Principle)
tks:http://www.cnblogs.com/Benjamin/p/3251987.html
- Open Closed Principle(OCP)开闭原则
面向对象的最基本原则 Software entites like classes,modules and functions should be open for extension but cloa ...
- 设计模式六大原则(六): 开闭原则(Open Closed Principle)
定义: 一个软件实体如类.模块和函数应该对扩展开放,对修改关闭. 问题由来: 在软件的生命周期内,因为变化.升级和维护等原因需要对软件原有代码进行修改时,可能会给旧代码中引入错误,也可能会使我们不得不 ...
- Atitit.软件开发的几大规则,法则,与原则Principle v3
Atitit.软件开发的几大规则,法则,与原则Principle v31.1. 修改历史22. 设计模式六大原则22.1. 设计模式六大原则(1):单一职责原则22.2. 设计模式六大原则(2):里 ...
- 最少知识原则(Least Knowledge Principle)
最少知识原则(Least Knowledge Principle),或者称迪米特法则(Law of Demeter),是一种面向对象程序设计的指导原则,它描述了一种保持代码松耦合的策略.其可简单的归纳 ...
随机推荐
- Prestashop后台模块(中英转译)
=======================Prestashop-Modules========================= Pretashop运行起来非常慢,这个大家应该都知道,最近要给这个 ...
- Mvc内建功能(DefaultModelBinder)自动绑定。
在做Asp.Net MVC项目中,都知道View负责页面展示数据或者提供页面收集数据,而所展示的数据或者收集的数据都是从Controller的Action中获取或提交到Controller的Actio ...
- js获取日期:前天、昨天、今天、明天、后天
前天,昨天,今天,明天,后天 <html> <head> <meta http-equiv="Content-Type" content=" ...
- EF框架
Linq to EF 添加: //用户注册int IUserDAO.Register(Users user) { ; using (EF.ddgwDBEntities context = new EF ...
- 处理IIS 255错误,和相关信息
不知不觉已经注册博客一年多了,当初看见博客园的大神,自己也想像他们一样多写一些有用的,结果,,,不言而喻.在这里又感慨了一下,进入正题了.. 最近在公司服务器接触的比较多, 遇到了一个255 的问题 ...
- 结合IL和Windbg来看.Net调用继承虚方法的执行顺序
先上测试代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; using ...
- JAVA ------ 大牛
李学凯 :http://blog.csdn.net/qq_27093465/article/details/51750535 码农场:http://www.hankcs.com/program/ 徐刘 ...
- Python常用模块(四)
一.re模块 正则表达式时计算机科学的一个概念,正则表达式通常被用来检索,替换那些符合某个模式的文本,大多数程序设计语言都支持利用正则表达式进行字符串操作. 正则就是用一些具有特殊含义的符号组合到一起 ...
- vue 钩子函数
beforeRouteEnter 方法名称: beforeRouteEnter 调用时机: 切换路由之前,调用该方法时,页面还没有切换 next调用时机: activated 之后 注意事项: thi ...
- Android中快速实现自定义字体!
前言:我们都知道,Android中默认的字体是黑体,而大多数app也都是使用的这种字体,但我们发现,大多数app中,个别地方字体非常好看,例如app的标题栏,菜单栏等地方,那他们是怎么做到的呢?有两种 ...
