写好的代码就是为了以后少写一些代码。
城里人一般是住公寓吃快餐,而乡村人一般是住农家吃农家饭。但是呢,城里人去郊区旅游也是住农家院吃农家饭;乡村人到城里逛街也是住公寓吃快餐。各种各样的人都有可能离开自己常住的地方去体验其他的生活。假如我们有一个类叫城里人带着两个方法住和吃,就需要根据旅游的目的地来实现好多个住和吃的方案;同样的乡村人到处旅游带着两个方法吃和住,也需要实现好多的方法。桥接模式就是把人和吃住的方法分别实现,灵活组合,实现在不同的目的地各种吃住场景的切换。
例如联想笔记本可以装windows也可以装linux;惠普笔记本可以装windows也可以装linux。就没必要惠普和联想既生产硬件又开发windows和linux吧。所以把软件和硬件分开来做,区分为硬件公司和软件公司,硬件公司只管硬件,软件公司只管软件,桥接模式负责各种组合。
C++代码演示第一个场景;C#代码演示第二种场景。
C++代码:
// 居民.h
#pragma once
#include <string>
using namespace std;
class 居民
{
public:
居民();
virtual ~居民();
virtual void 消费() = 0;
};
// 居民.cpp
#include "stdafx.h"
#include "居民.h"
居民::居民()
{
}
居民::~居民()
{
}
// 城市居民.h
#pragma once
#include "居民.h"
#include "消费模式.h"
class 城市居民 :
public 居民
{
public:
城市居民(消费模式* model);
~城市居民();
void 消费();
private:
消费模式* model;
};
// 城市居民.cpp
#include "stdafx.h"
#include "城市居民.h"
#include <iostream>
using namespace std;
城市居民::城市居民(消费模式* model)
{
this->model = model;
}
城市居民::~城市居民()
{
}
void 城市居民::消费()
{
cout << "城市居民" << model->消费() << endl;
}
// 乡村居民.h
#pragma once
#include "居民.h"
#include "消费模式.h"
class 乡村居民 :
public 居民
{
public:
乡村居民(消费模式* model);
virtual ~乡村居民();
void 消费();
private:
消费模式* model;
};
// 乡村居民.cpp
#include "stdafx.h"
#include "乡村居民.h"
#include <iostream>
using namespace std;
乡村居民::乡村居民(消费模式* model)
{
this->model = model;
}
乡村居民::~乡村居民()
{
}
void 乡村居民::消费()
{
cout << "乡村居民" << model->消费() << endl;
}
// 消费模式.h
#pragma once
#include <string>
using namespace std;
class 消费模式
{
public:
消费模式();
virtual ~消费模式();
virtual string 消费() = 0;
};
// 消费模式.cpp
#include "stdafx.h"
#include "消费模式.h"
消费模式::消费模式()
{
}
消费模式::~消费模式()
{
}
// 城市消费模式.h
#pragma once
#include "消费模式.h"
class 城市消费模式 :
public 消费模式
{
public:
城市消费模式();
~城市消费模式();
string 消费();
private:
string 区域 = "城市";
};
// 城市消费模式.cpp
#include "stdafx.h"
#include "城市消费模式.h"
城市消费模式::城市消费模式()
{
}
城市消费模式::~城市消费模式()
{
}
string 城市消费模式::消费()
{
return "在城市住公寓吃快餐";
}
// 乡村消费模式.h
#pragma once
#include "消费模式.h"
class 乡村消费模式 :
public 消费模式
{
public:
乡村消费模式();
virtual ~乡村消费模式();
string 消费();
};
// 乡村消费模式.cpp
#include "stdafx.h"
#include "乡村消费模式.h"
乡村消费模式::乡村消费模式()
{
}
乡村消费模式::~乡村消费模式()
{
}
string 乡村消费模式::消费()
{
return "在乡村住农家院吃农家菜";
}
// 桥接模式.cpp
// 桥接模式.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "居民.h"
#include "乡村居民.h"
#include "城市居民.h"
#include "消费模式.h"
#include "城市消费模式.h"
#include "乡村消费模式.h"
int _tmain(int argc, _TCHAR* argv[])
{
// 正常的情况下,城市居民生活在城市里
消费模式* city_model = new 城市消费模式();
居民* city_people = new 城市居民(city_model);
city_people->消费();
delete city_people;
// 正常的情况下,乡村居民生活在乡村
消费模式* country_model = new 乡村消费模式();
居民* country_people = new 乡村居民(country_model);
country_people->消费();
delete country_people;
// 城市居民到乡村旅游
居民* city_tour_people = new 城市居民(country_model);
city_tour_people->消费();
delete city_tour_people;
delete country_model;
// 乡村居民到城市消费
居民* country_tour_people = new 乡村居民(city_model);
country_tour_people->消费();
delete country_tour_people;
delete city_model;
getchar();
return 0;
}
C#代码:
/*
* 由SharpDevelop创建。
* 用户: hangba
* 日期: 2016/3/9
* 时间: 21:50
*
* 要改变这种模板请点击 工具|选项|代码编写|编辑标准头文件
*/
using System;
namespace 桥接模式
{
/// <summary>
/// Description of 笔记本电脑.
/// </summary>
public interface 笔记本电脑
{
void 安装();
}
}
/*
* 由SharpDevelop创建。
* 用户: hangba
* 日期: 2016/3/9
* 时间: 21:50
*
* 要改变这种模板请点击 工具|选项|代码编写|编辑标准头文件
*/
using System;
namespace 桥接模式
{
/// <summary>
/// Description of 联想笔记本电脑.
/// </summary>
public class 联想笔记本电脑 : 笔记本电脑
{
public 联想笔记本电脑(操作系统 os)
{
this.os = os;
}
public void 安装()
{
Console.WriteLine("联想笔记本电脑安装了{0}", os.名称);
}
private 操作系统 os = null;
}
}
/*
* 由SharpDevelop创建。
* 用户: hangba
* 日期: 2016/3/9
* 时间: 21:51
*
* 要改变这种模板请点击 工具|选项|代码编写|编辑标准头文件
*/
using System;
namespace 桥接模式
{
/// <summary>
/// Description of 惠普笔记本电脑.
/// </summary>
public class 惠普笔记本电脑 : 笔记本电脑
{
public 惠普笔记本电脑(操作系统 os)
{
this.os = os;
}
public void 安装()
{
Console.WriteLine("惠普笔记本电脑安装了{0}", os.名称);
}
private 操作系统 os = null;
}
}
/*
* 由SharpDevelop创建。
* 用户: hangba
* 日期: 2016/3/9
* 时间: 21:52
*
* 要改变这种模板请点击 工具|选项|代码编写|编辑标准头文件
*/
using System;
namespace 桥接模式
{
/// <summary>
/// Description of 操作系统.
/// </summary>
public interface 操作系统
{
string 名称 {get;}
}
}
/*
* 由SharpDevelop创建。
* 用户: hangba
* 日期: 2016/3/9
* 时间: 21:53
*
* 要改变这种模板请点击 工具|选项|代码编写|编辑标准头文件
*/
using System;
namespace 桥接模式
{
/// <summary>
/// Description of Windows操作系统.
/// </summary>
public class Windows操作系统 : 操作系统
{
public Windows操作系统()
{
}
public string 名称
{
get
{
return "Windows操作系统";
}
}
}
}
/*
* 由SharpDevelop创建。
* 用户: hangba
* 日期: 2016/3/9
* 时间: 21:55
*
* 要改变这种模板请点击 工具|选项|代码编写|编辑标准头文件
*/
using System;
namespace 桥接模式
{
/// <summary>
/// Description of Linux操作系统.
/// </summary>
public class Linux操作系统 : 操作系统
{
public Linux操作系统()
{
}
public string 名称
{
get
{
return "Linux操作系统";
}
}
}
}
/*
* 由SharpDevelop创建。
* 用户: hangba
* 日期: 2016/3/9
* 时间: 21:49
*
* 要改变这种模板请点击 工具|选项|代码编写|编辑标准头文件
*/
using System;
namespace 桥接模式
{
class Program
{
public static void Main(string[] args)
{
// 联想笔记本电脑安装Windows操作系统
笔记本电脑 lenovo_windows = new 联想笔记本电脑(new Windows操作系统());
lenovo_windows.安装();
// 惠普笔记本电脑安装Windows操作系统
笔记本电脑 hp_windows = new 惠普笔记本电脑(new Windows操作系统());
hp_windows.安装();
// 联想笔记本电脑安装Linux操作系统
笔记本电脑 lenovo_Linux = new 联想笔记本电脑(new Linux操作系统());
lenovo_Linux.安装();
// 惠普笔记本电脑安装Windows操作系统
笔记本电脑 hp_Linux = new 惠普笔记本电脑(new Linux操作系统());
hp_Linux.安装();
Console.Write("Press any key to continue . . . ");
Console.ReadKey(true);
}
}
}