Bridge桥接模式C++

Bridge桥接模式C++_第1张图片

 

Bridge桥接模式C++_第2张图片

 

#pragma once
//os.h
#ifndef OS_H
#define OS_H

class os
{
public:
	virtual ~os(){	}

	virtual void on() = 0;
	virtual void off() = 0;
};

#endif // !OS_H

 

#include 
using namespace std;

class wins :public os
{
public:
	virtual void on() override 
	{
		cout <<"启动windows" << endl;
	}
	virtual void off() override
	{
		cout << "关闭windows" << endl;
	}
};

class linux :public os
{
public:
	virtual void on() override
	{
		cout << "启动linux" << endl;
	}
	virtual void off() override
	{
		cout << "关闭linux" << endl;
	}
};

#endif // !CONCRETEOS_H
#pragma once
//computer.h
#ifndef COMPUTER_H
#define COMPUTER_H

#include "os.h"
class computer
{
public:
	computer(os *oss) { os1 = oss; }
	virtual ~computer(){	}

	virtual void on() = 0;
	virtual void off() = 0;
protected:
	os *os1;
};



#endif // !COMPUTER_H
#pragma once
//concretecomputer.h
#ifndef CONCRETECOMPUTER_H
#define CONCRETECOMPUTER_H

#include "computer.h"
#include 
using namespace std;

class dellcomputer :public computer
{
public:
	dellcomputer(os *oss) :computer(oss) {}
	virtual void on()override
	{
		cout << "dell物理开机"<< endl;
		os1->on();
	}
	virtual void off()override
	{
		os1->off();
		cout << "dell物理关机" << endl;
	}
};

class asuscomputer :public computer
{
public:
	asuscomputer(os *oss) :computer(oss) {}
	virtual void on()override
	{
		cout << "asus物理开机" << endl;
		os1->on();
	}
	virtual void off()override
	{
		os1->off();
		cout << "asus物理关机" << endl;
	}
};

#endif // !CONCRETECOMPUTER_H
//main.cpp


#include "concretecomputer.h"
#include "concreteos.h"

int main()
{
	os *Wins = new wins();
	os *Linux = new linux();

	computer *Dell = new dellcomputer(Wins);
	computer *Asus = new asuscomputer(Linux);

	Dell->on();
	Dell->off();

	Asus->on();
	Asus->off();

	delete (Asus);
	Asus = NULL;
	delete (Dell);
	Dell = NULL;
	delete (Linux);
	Linux = NULL;
	delete (Asus);
	Asus = NULL;

	system("pause");
	return 0;
}

 

Bridge桥接模式C++_第3张图片

 

你可能感兴趣的:(Bridge桥接模式C++)