#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的更多相关文章

  1. 开放封闭原则(Open Closed Principle)

    在面向对象的设计中有很多流行的思想,比如说 "所有的成员变量都应该设置为私有(Private)","要避免使用全局变量(Global Variables)",& ...

  2. 面向对象设计原则 开放封闭原则(Open Closed Principle)

    开放封闭原则(OCP,Open Closed Principle) 开放封闭原则是所有面向对象原则的核心. 软件设计本身所追求的目标就是封装变化.降低耦合,而开放封闭原则正是对这一目标的最直接体现. ...

  3. 北风设计模式课程---开放封闭原则(Open Closed Principle)

    北风设计模式课程---开放封闭原则(Open Closed Principle) 一.总结 一句话总结: 抽象是开放封闭原则的关键. 1."所有的成员变量都应该设置为私有(Private)& ...

  4. 开闭原则(Open Closed Principle,OCP)

    遵循开闭原则设计出的模块具有两个主要特征: 对于扩展是开放的(Open for extension).这意味着模块的行为是可以扩展的.当应用的需求改变时,我们可以对模块进行扩展,使其具有满足那些改变的 ...

  5. 开放封闭原则(OCP,Open Closed Principle)

    tks:http://www.cnblogs.com/Benjamin/p/3251987.html

  6. Open Closed Principle(OCP)开闭原则

    面向对象的最基本原则 Software entites like classes,modules and functions should be open for extension but cloa ...

  7. 设计模式六大原则(六): 开闭原则(Open Closed Principle)

    定义: 一个软件实体如类.模块和函数应该对扩展开放,对修改关闭. 问题由来: 在软件的生命周期内,因为变化.升级和维护等原因需要对软件原有代码进行修改时,可能会给旧代码中引入错误,也可能会使我们不得不 ...

  8. Atitit.软件开发的几大规则,法则,与原则Principle v3

    Atitit.软件开发的几大规则,法则,与原则Principle  v31.1. 修改历史22. 设计模式六大原则22.1. 设计模式六大原则(1):单一职责原则22.2. 设计模式六大原则(2):里 ...

  9. 最少知识原则(Least Knowledge Principle)

    最少知识原则(Least Knowledge Principle),或者称迪米特法则(Law of Demeter),是一种面向对象程序设计的指导原则,它描述了一种保持代码松耦合的策略.其可简单的归纳 ...

随机推荐

  1. [转]JAVA Iterator 的用法

    java.util包中包含了一系列重要的集合类.本文将从分析源码入手,深入研究一个集合类的内部结构,以及遍历集合的迭代模式的源码实现内幕. 下面我们先简单讨论一个根接口Collection,然后分析一 ...

  2. AWS的redhat7中安装jdk8

    下载jdk8 wget https://download.oracle.com/otn-pub/java/jdk/8u191-b12/2787e4a523244c269598db4e85c51e0c/ ...

  3. Hibernate课程 初探一对多映射3-4 双向多对一的测试

    1 单向多对一和单向多对一的区别 比如部门和员工,一个部门下有很多员工,如果只查一个员工属于哪个部门,就用单向的,如果还要查一个部门下的所有员工,就用双向的. 2 双向多对一的配置 除了单向xml和双 ...

  4. 超链接导致window.location.href失效的解决办法

    通常我们采用 window.location.href 执行页面间的跳转,比如下面的语句 window.location.href = 'https://www.baidu.com/'; 一般执行上面 ...

  5. easyui datagrid 动态加载数据 渲染问题,表格错位问题

    $('#dg').datagrid({ url:'datagrid_data.json', columns:[[ {field:'code',title:'Code',width:100}, {fie ...

  6. C++ Knowledge series Template & Class

    Function Function is composed of name, parameter (operand, type of operand), return value, body with ...

  7. jquery 滑块导航菜单

    带滑块的导航菜单,鼠标悬浮时,滑块会移动至鼠标位置,离开时,滑块会回到原来的位置,点击菜单之后滑块会停留在被点击菜单位置,等待下一次的鼠标悬浮事件或者点击事件,效果图: 图片效果不行,直接上代码: & ...

  8. Python基础学习之语句和语法

    语句和语法 python语句中有一些基本规则和特殊字符: 井号键“#”表示之后的字符为python注释: 三引号(‘‘‘ ’’’)可以多行注释 换行“\n”是标准的行分隔符(通常一个语句一行): 反斜 ...

  9. 从C++起步到MFC实战VC++软件工程师高端培训 视频保存在 播音员的网盘中

    从C++起步到MFC实战VC++软件工程师高端培训(服务器端开发方向)[共332课时]视频保存在 播音员的网盘中http://www.it1352.com/VideoTutorial/Details? ...

  10. Vim中根据正则对选中文本对齐(比如ini文件的=号对齐)

    vimrc增加如下内容即可: vnoremap <M-=> :call Duiqi('\v(^\s*\S+)\s+(.*)')<CR> "reg匹配的第2段文字对齐 ...