C++的依赖颠倒原则

个人理解,即面向接口编程,即抽象基类做接口,使用这个的时候,不在乎具体怎么实现,使用抽象基类声明,接口部分由派生类具体实现。

读下面文章写的一个小例子

23个小案例带你吃透23种设计模式 | C++实现-云社区-华为云

cpp 复制代码
#include <iostream>

using namespace std;

//电脑:屏幕,cpu,内存,显卡,硬盘,


class AbstractScreen {
public:
	virtual void information() = 0;
};

class AbstractCpu {
public:
	virtual void information() = 0;
};

class AbstractMemory {
public:
	virtual void information() = 0;
};

class AbstractGraphics {
public:
	virtual void information() = 0;
};

class AbstractHardDisk {
public:
	virtual void information() = 0;
};

class computer {
public:
	computer(AbstractScreen* _screen = nullptr, AbstractCpu* _cpu = nullptr, AbstractMemory* _memory = nullptr,
		AbstractGraphics* _graphics = nullptr, AbstractHardDisk* _disk = nullptr);

	void showInformation();

	void changeScreen(AbstractScreen* _screen) {
		screen = _screen;		//更换屏幕
	}

	
private:
	AbstractScreen* screen = nullptr;
	AbstractCpu* cpu = nullptr;
	AbstractMemory* memory = nullptr;
	AbstractGraphics* graphics = nullptr;
	AbstractHardDisk* hardDisk = nullptr;
};


class sanxingScreen : public AbstractScreen {
public:
	void information() {
		cout << "三星的屏幕" << endl;
	}
};

class LgScreen : public AbstractScreen {
public:
	void information() {
		cout << "罗技的屏幕" << endl;
	}
};

class coreCpu : public AbstractCpu {
public:
	void information() {
		cout << "酷睿的CPU" << endl;
	}
};

class Jinston : public AbstractMemory {
public:
	void information() {
		cout << "金士顿的内存条" << endl;
	}
};

class Asus : public AbstractGraphics {
public:
	void information() {
		cout << "华硕的显卡" << endl;
	}
};

class seagate :public AbstractHardDisk {
public:
	void information() {
		cout << "希捷的硬盘" << endl;
	}
};






int main()
{
	sanxingScreen myscreen;		//屏幕
	coreCpu myCpu;				//CPU
	Jinston mymemory;			//内存
	Asus myGraphics;			//显卡
	seagate myHardDisk;			//硬盘

	computer myComputer(&myscreen,&myCpu,&mymemory,&myGraphics,&myHardDisk);
	myComputer.showInformation();

	LgScreen myScreen2;			//第二块屏幕
	myComputer.changeScreen(&myScreen2);
	myComputer.showInformation();	//更换屏幕后的信息

	return 0;
}

computer::computer(AbstractScreen* _screen, AbstractCpu* _cpu, AbstractMemory* _memory, AbstractGraphics* _graphics, AbstractHardDisk* _disk)
	:screen(_screen),cpu(_cpu),memory(_memory),graphics(_graphics),hardDisk(_disk)
{

}

//展示电脑配置信息
void computer::showInformation()
{
	if (screen != nullptr)
	{
		screen->information();
	}

	if (cpu != nullptr)
	{
		cpu->information();
	}

	if (memory != nullptr)
	{
		memory->information();
	}

	if (graphics != nullptr)
	{
		graphics->information();
	}

	if (hardDisk != nullptr)
	{
		hardDisk->information();
	}

	cout << "\n" << "\n" << endl;
}
相关推荐
软件黑马王子32 分钟前
C#初级教程(4)——流程控制:从基础到实践
开发语言·c#
闲猫36 分钟前
go orm GORM
开发语言·后端·golang
黑不溜秋的2 小时前
C++ 设计模式 - 策略模式
c++·设计模式·策略模式
李白同学2 小时前
【C语言】结构体内存对齐问题
c语言·开发语言
黑子哥呢?3 小时前
安装Bash completion解决tab不能补全问题
开发语言·bash
青龙小码农3 小时前
yum报错:bash: /usr/bin/yum: /usr/bin/python: 坏的解释器:没有那个文件或目录
开发语言·python·bash·liunx
大数据追光猿4 小时前
Python应用算法之贪心算法理解和实践
大数据·开发语言·人工智能·python·深度学习·算法·贪心算法
Dream it possible!4 小时前
LeetCode 热题 100_在排序数组中查找元素的第一个和最后一个位置(65_34_中等_C++)(二分查找)(一次二分查找+挨个搜索;两次二分查找)
c++·算法·leetcode
柠石榴4 小时前
【练习】【回溯No.1】力扣 77. 组合
c++·算法·leetcode·回溯
王老师青少年编程4 小时前
【GESP C++八级考试考点详细解读】
数据结构·c++·算法·gesp·csp·信奥赛