add Bridge Pattern

master
FengJungle 2019-10-23 22:19:36 +08:00
parent bb9400b945
commit deca28dddb
6 changed files with 114 additions and 1 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 512 KiB

View File

@ -0,0 +1,86 @@
#ifndef __BRIDGE_PATTERN_H__
#define __BRIDGE_PATTERN_H__
#include <iostream>
#include <string.h>
#include <mutex>
using namespace std;
//实现类接口
class Game
{
public:
Game(){}
virtual void play() = 0;
private:
};
//具体实现类GameA
class GameA:public Game
{
public:
GameA(){}
void play(){
printf("Jungle玩游戏A\n");
}
};
//具体实现类GameB
class GameB :public Game
{
public:
GameB(){}
void play(){
printf("Jungle玩游戏B\n");
}
};
//抽象类Phone
class Phone
{
public:
Phone(){
}
//安装游戏
virtual void setupGame(Game *igame) = 0;
virtual void play() = 0;
private:
Game *game;
};
//扩充抽象类PhoneA
class PhoneA:public Phone
{
public:
PhoneA(){
}
//安装游戏
void setupGame(Game *igame){
this->game = igame;
}
void play(){
this->game->play();
}
private:
Game *game;
};
//扩充抽象类PhoneB
class PhoneB :public Phone
{
public:
PhoneB(){
}
//安装游戏
void setupGame(Game *igame){
this->game = igame;
}
void play(){
this->game->play();
}
private:
Game *game;
};
#endif //__BRIDGE_PATTERN_H__

View File

@ -0,0 +1,23 @@
#include <iostream>
#include "BridgePattern.h"
int main()
{
Game *game;
Phone *phone;
//Jungle买了PhoneA品牌的手机想玩游戏A
phone = new PhoneA();
game = new GameA();
phone->setupGame(game);
phone->play();
printf("++++++++++++++++++++++++++++++++++\n");
//Jungle想在这个手机上玩游戏B
game = new GameB();
phone->setupGame(game);
phone->play();
system("pause");
return 0;
}

View File

@ -39,4 +39,8 @@
10.设计模式(十)——适配器模式 10.设计模式(十)——适配器模式
博客地址https://blog.csdn.net/sinat_21107433/article/details/102656617 博客地址https://blog.csdn.net/sinat_21107433/article/details/102656617
11.设计模式(十一)——桥接模式
博客地址https://blog.csdn.net/sinat_21107433/article/details/102694306