C++两个原则
来源:互联网 发布:vb进度条控件使用 编辑:程序博客网 时间:2024/06/05 18:57
#include <iostream>
using namespace std;
/*2.抽象层业务逻辑*/
class IReader {// InterfaceReader
public:
virtual string getContent() = 0;
};
/*3.底层业务逻辑*/
//接口 细节
class Book :public IReader{
public:
string getContent(){
return "Book的内容";
}
};
//接口 细节
class NewsPaper :public IReader{
public:
string getContent(){
return "NewsPaper的内容";
}
};
//接口 细节
class ElectricBook:public IReader{
public:
string getContent(){
return"ElectricBook的内容";
}
};
/*
大项目,架构动,则是重构,大的责任
1.高层业务逻辑
*/
class Mother{
public:
/*
1.C++原则1:开闭原则:
闭:高层业务的接口要高度抽象,不要试图修改。
开:如果有新的功能增加的话,通过增加类来实现 。
C++原则2:依赖倒置原则:
高层和底层依赖于其抽象,细节应依赖于抽象
*/
void tellStroy(IReader *i){
cout<<i->getContent()<<endl;
}
/*
1.高层:每次新读一种,都要修改高层业务的内容
void tellStroy(Book *i){
cout<<i->getContent()<<endl;
}
void tellStroy(NewsPaper *i){
cout<<i->getContent()<<endl;
}
*/
};
int main(){
Mother m;
Book b; // Book *b = new Book;
cout<<"Mother start to tell 书:"<<endl;
m.tellStroy(&b);
NewsPaper n;
cout<<"Mother start to tell 报纸:"<<endl;
m.tellStroy(&n);//m.tellStroy(b);
ElectricBook e ;
cout<<"Mother start to tell 电子书:"<<endl;
m.tellStroy(&e);//m.tellStroy(b);
//delete b;
return 0;
}
using namespace std;
/*2.抽象层业务逻辑*/
class IReader {// InterfaceReader
public:
virtual string getContent() = 0;
};
/*3.底层业务逻辑*/
//接口 细节
class Book :public IReader{
public:
string getContent(){
return "Book的内容";
}
};
//接口 细节
class NewsPaper :public IReader{
public:
string getContent(){
return "NewsPaper的内容";
}
};
//接口 细节
class ElectricBook:public IReader{
public:
string getContent(){
return"ElectricBook的内容";
}
};
/*
大项目,架构动,则是重构,大的责任
1.高层业务逻辑
*/
class Mother{
public:
/*
1.C++原则1:开闭原则:
闭:高层业务的接口要高度抽象,不要试图修改。
开:如果有新的功能增加的话,通过增加类来实现 。
C++原则2:依赖倒置原则:
高层和底层依赖于其抽象,细节应依赖于抽象
*/
void tellStroy(IReader *i){
cout<<i->getContent()<<endl;
}
/*
1.高层:每次新读一种,都要修改高层业务的内容
void tellStroy(Book *i){
cout<<i->getContent()<<endl;
}
void tellStroy(NewsPaper *i){
cout<<i->getContent()<<endl;
}
*/
};
int main(){
Mother m;
Book b; // Book *b = new Book;
cout<<"Mother start to tell 书:"<<endl;
m.tellStroy(&b);
NewsPaper n;
cout<<"Mother start to tell 报纸:"<<endl;
m.tellStroy(&n);//m.tellStroy(b);
ElectricBook e ;
cout<<"Mother start to tell 电子书:"<<endl;
m.tellStroy(&e);//m.tellStroy(b);
//delete b;
return 0;
}
阅读全文
0 0
- C++两个原则
- 关于继承的两个原则
- dBm一个基准两个原则
- 代码原则:c/c++
- 单一职责原则(C++)
- 开闭原则(C++)
- 依赖倒转原则(C++)
- 关于Oracle中的rownum两个使用原则
- 关于Oracle中的rownum两个使用原则
- 提高程序运行效率的两个原则
- 【C#】开发的两个基本编程原则的深入讨论--为什么要用get,set,直接用public string Name不行吗
- c语言声明优先原则
- C头文件编写原则
- Objective-C内存管理原则
- Liskov替换原则(C#)
- C语言之 短路原则
- 【C#】设计模式六大原则
- Objective-C - 里氏替换原则
- 浅析机器学习中的模型选择与调参(cross validation + grid search)
- 数据结构笔记(二)线性表的链式表示和基本操作
- ACM 字符串综合一
- C语言——Keywords&printf&scanf
- 欢迎使用CSDN-markdown编辑器
- C++两个原则
- IOS开发之——objectForKey与valueForKey在NSDictionary中的差异
- HDU
- 【设计模式】装饰者模式
- redis-3.2.10 单机安装
- Mysql常用函数
- 【系列推荐】Android自定义控件三部曲文章索引
- 梯度提升树(GBDT)原理小结
- poj2785 折半枚举 双向查找