实例代码:
业务层用计算机代替,计算机使用硬盘、CPU、内存,但不依赖于具体厂家,设计Comouter框架和具体的厂家进行
解耦合。
#include <iostream>
using namespace std;
class HardDisk
{
public:
virtual void work() = 0;
};
class Memory
{
public:
virtual void work() = 0;
};
class Cpu
{
public:
virtual void work() = 0;
};
//Computer框架和具体的厂商进行解耦合
class Computer
{
public:
Computer(HardDisk *harddisk, Memory *memory, Cpu *cpu)
{
m_harddisk = harddisk;
m_memory = memory;
m_cpu = cpu;
}
void work()
{
m_harddisk->work();
m_memory->work();
m_cpu->work();
}
private:
HardDisk *m_harddisk;
Memory *m_memory;
Cpu *m_cpu;
};
class InterCpu : public Cpu
{
public:
void work()
{
cout << "InterCpu ..." << endl;
}
};
class XSDisk : publicHardDisk
{
public:
void work()
{
cout << "XSDisk ..." << endl;
}
};
class JSDMem : public Memory
{
public:
void work()
{
cout << "JSDMem ..." << endl;
}
};
int main()
{
HardDisk *harddisk = NULL;
Memory *memory = NULL;
Cpu *cpu = NULL;
memory = new JSDMem;
harddisk = new XSDisk;
cpu = new InterCpu;
Computer *mycomputer = new Computer(harddisk, memory, cpu);
mycomputer->work();
delete mycomputer;
delete cpu;
delete mycomputer;
delete memory;
return 0;
}