C++两个原则

#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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值