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),是一种面向对象程序设计的指导原则,它描述了一种保持代码松耦合的策略.其可简单的归纳 ...
随机推荐
- Murano Weekly Meeting 2016.05.31
Meeting time: 2016.May.31 1:00~2:00 Chairperson: Kirill Zaitsev, from Mirantis Meeting summary: 1.A ...
- SSIS 错误代码 DTS_E_CANNOTACQUIRECONNECTIONFROMCONNECTIONMANAGER
[OLE DB 源 [2]] 错误: SSIS 错误代码 DTS_E_CANNOTACQUIRECONNECTIONFROMCONNECTIONMANAGER.对连接管理器“test.trade_sh ...
- 日期API
Java 8 在包java.time下包含了一组全新的时间日期API.下面的例子展示了这组新API里最重要的一些部分: 1.Clock 时钟 Clock类提供了访问当前日期和时间的方法,Clock是时 ...
- iOS重用宏定义
iOS 多快好省的宏(转) 原文地址:http://my.oschina.net/yongbin45/blog/150149 // 字符串: #ifndef nilToEmpty #define ni ...
- c语言 Implement strStr()【Leetcode】
实现在一个母字符串中找到第一个子字符串的位置. #include <stdio.h> #include <string.h> #define _IRON_TRUE 1 #def ...
- 跨平台移动开发phonegap/cordova 3.3全系列教程-helloworld
1. 建立专案(cordova) 打开cmd命令行 cordova create ACESMobile aces.mobile ACES cd aces mobile 如图 2. 安装插件 ...
- sudo使用
/etc/sudo.conf /etc/sudoers /etc/sudoers.d/ /etc/sudo-ldap.conf /etc/sudoer sudo安全策略配置文件 Defaults re ...
- python实现各种排序
1.冒泡排序: # -*- coding: utf-8 -*- def BubbleSort(a): n=len(a) for i in range(0,n-1): swapped=False for ...
- DEEP LEARNING 大满贯课程表
Reinforcement Learning post by ISH GIRWAN Courses/Tutorials Deep Reinforcement Learning, Spring 2017 ...
- GitLab-Runner 安装配置
https://docs.gitlab.com/runner/install/linux-repository.html 直接看官方教程 systemctl status gitlab-runner. ...
