设计模式之桥接模式(Bridge)

1、定义

桥接(Bridge)是用于把抽象化与实现化解耦,使得二者可以独立变化。这种类型的设计模式属于结构型模式,它通过提供抽象化和实现化之间的桥接结构,来实现二者的解耦。

这种模式涉及到一个作为桥接的接口,使得实体类的功能独立于接口实现类。这两种类型的类可被结构化改变而互不影响。

2、介绍

优点: 1、抽象和实现的分离。 2、优秀的扩展能力。 3、实现细节对客户透明。

缺点:桥接模式的引入会增加系统的理解与设计难度,由于聚合关联关系建立在抽象层,要求开发者针对抽象进行设计与编程。

使用场景: 1、如果一个系统需要在构件的抽象化角色和具体化角色之间增加更多的灵活性,避免在两个层次之间建立静态的继承联系,通过桥接模式可以使它们在抽象层建立一个关联关系。 2、对于那些不希望使用继承或因为多层次继承导致系统类的个数急剧增加的系统,桥接模式尤为适用。 3、一个类存在两个独立变化的维度,且这两个维度都需要进行扩展。

注意事项:对于两个独立变化的维度,使用桥接模式再适合不过了。

3、源码

类关系图:

具体思路:1、IProduct类与CNewCorp类作为具体产品类与公司类桥,CNewCorp类有成员IProduct类。2、建立具体产品类对象与公司类对象,构造公司类对象为有参构造,产品做为构造参数。3、调用公司类的对象方法,实际调用父类的方法,父类根据IProduct指针的指向,确定具体调用的方法。

3.1、头文件

IProduct.h

#pragma once
class IProduct
{
public:
    IProduct(void)
    {
    }
    virtual ~IProduct(void)
    {
    }
    virtual void BeProducted() = 0;
    virtual void BeSelled() = 0;
};

House.h

#pragma once
#include "iproduct.h"
class CHouse :
    public IProduct
{
public:
    CHouse(void);
    ~CHouse(void);
    void BeProducted();
    void BeSelled();
};

 Clothes.h

#pragma once
#include "iproduct.h"
class CClothes :
    public IProduct
{
public:
    CClothes(void);
    ~CClothes(void);
    void BeProducted();
    void BeSelled();
};

 IPod.h

#pragma once
#include "iproduct.h"
class CIPod :
    public IProduct
{
public:
    CIPod(void);
    ~CIPod(void);
    void BeProducted();
    void BeSelled();
};

 NewCorp.h

#pragma once
#include "IProduct.h"
class CNewCorp
{
public:
    CNewCorp(IProduct *pproduct);
    virtual ~CNewCorp(void);
    void MakeMoney();
private:
    IProduct *m_pProduct;
};

 NewHouseCorp.h

#pragma once
#include "newcorp.h"
#include "House.h"
class CNewHouseCorp :
    public CNewCorp
{
public:
    CNewHouseCorp(CHouse *pHouse);
    ~CNewHouseCorp(void);
    void MakeMoney();
};

ShanZhaiCorp.h

 

#pragma once
#include "newcorp.h"
#include "IProduct.h"
class CShanZhaiCorp :
    public CNewCorp
{
public:
    CShanZhaiCorp(IProduct *pproduct);
    ~CShanZhaiCorp(void);
    void MakeMoney();
};

3.2、实现

House.cpp

#include "House.h"
CHouse::CHouse(void)
{
}
CHouse::~CHouse(void)
{
}
void CHouse::BeProducted()
{
    cout << "生产出的房子是这个样子的..." << endl;
}
void CHouse::BeSelled()
{
    cout << "生产出的房子卖出去了..." << endl;
}

Clothes.cpp

#include "Clothes.h"

CClothes::CClothes(void)
{
}
CClothes::~CClothes(void)
{
}
void CClothes::BeProducted()
{
	cout << "生产出的衣服是这个样子的..." << endl;
}
void CClothes::BeSelled()
{
	cout << "生产出的衣服卖出去了..." << endl;
}

IPod.cpp

#include "IPod.h"
CIPod::CIPod(void)
{
}
CIPod::~CIPod(void)
{
}
void CIPod::BeProducted()
{
	cout << "生产出的ipod是这个样子的..." << endl;
}
void CIPod::BeSelled()
{
	cout << "生产出的ipod卖出去了..." << endl;
}

NewCorp.cpp

#include "NewCorp.h"
CNewCorp::CNewCorp( IProduct *pproduct )
{
    this->m_pProduct = pproduct;
}
CNewCorp::~CNewCorp(void)
{
}
void CNewCorp::MakeMoney()
{
    //每个公司都是一样,先生产
    this->m_pProduct->BeProducted();

    //然后销售
    this->m_pProduct->BeSelled();
}

NewHouseCorp.cpp

#include "NewHouseCorp.h"

CNewHouseCorp::CNewHouseCorp(CHouse *pHouse) : CNewCorp(pHouse)
{
}
CNewHouseCorp::~CNewHouseCorp(void)
{
}
void CNewHouseCorp::MakeMoney()
{
    this->CNewCorp::MakeMoney();
    cout << "房地产公司赚大钱了..." << endl;
}

ShanZhaiCorp.cpp

#include "ShanZhaiCorp.h"

CShanZhaiCorp::CShanZhaiCorp(IProduct *pproduct) : CNewCorp(pproduct)
{
}
CShanZhaiCorp::~CShanZhaiCorp(void)
{
}
void CShanZhaiCorp::MakeMoney()
{
    this->CNewCorp::MakeMoney();
    cout << "我赚钱呀..." << endl;
}

Bridge.cpp

#include "ClothesCorp.h"
#include "NewHouseCorp.h"
#include "Clothes.h"
#include "IPod.h"
#include "ShanZhaiCorp.h"
#include <iostream>
using std::cout;
using std::endl;

void DoNewRun1()
{
	cout << "----------房地产公司是这样运行的----------" << endl;
	CHouse house;
	CNewHouseCorp newHouseCorp(&house);
	newHouseCorp.MakeMoney();
	cout << endl;

	cout << "----------山寨公司是这样运行的----------" << endl;
	CClothes clothes;
	CShanZhaiCorp shanZhaiCorp(&clothes);
	shanZhaiCorp.MakeMoney();
	cout << endl;
}

void DoNewRun2()
{
	cout << "----------房地产公司是这样运行的----------" << endl;
	CHouse house;
	CNewHouseCorp newHouseCorp(&house);
	newHouseCorp.MakeMoney();
	cout << endl;

	cout << "----------山寨公司是这样运行的----------" << endl;
	CIPod ipod;
	CShanZhaiCorp shanZhaiCorp(&ipod);
	shanZhaiCorp.MakeMoney();
	cout << endl;
}

int main()
{
	//只有两家公司,一家是房地产公司,另一家公司是衣服赚钱就生产衣服
	DoNewRun1();

	//只有两家公司,一家是房地产公司,另一家公司是ipod赚钱就生产ipod
	DoNewRun2();

	system("pause");
	return 0;
}

4、运行结果

参考文献:《菜鸟教程》   https://blog.csdn.net/phiall/article/details/52199659博客

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
桥接模式是一种结构型设计模式,它的主要目的是将抽象部分与实现部分分离,使它们能够独立地变化。下面是一个Java桥接模式的示例: 首先定义一个抽象类Shape,它有一个DrawAPI的成员变量,表示它的实现。 ```java public abstract class Shape { protected DrawAPI drawAPI; protected Shape(DrawAPI drawAPI) { this.drawAPI = drawAPI; } public abstract void draw(); } ``` 然后定义具体的形状类,比如Circle和Rectangle,它们继承自抽象类Shape,并实现了draw方法。 ```java public class Circle extends Shape { private int x, y, radius; public Circle(int x, int y, int radius, DrawAPI drawAPI) { super(drawAPI); this.x = x; this.y = y; this.radius = radius; } @Override public void draw() { drawAPI.drawCircle(radius, x, y); } } public class Rectangle extends Shape { private int x, y, width, height; public Rectangle(int x, int y, int width, int height, DrawAPI drawAPI) { super(drawAPI); this.x = x; this.y = y; this.width = width; this.height = height; } @Override public void draw() { drawAPI.drawRectangle(x, y, width, height); } } ``` 最后定义一个DrawAPI接口,它有两个方法drawCircle和drawRectangle,表示画圆和画矩形的实现。 ```java public interface DrawAPI { void drawCircle(int radius, int x, int y); void drawRectangle(int x, int y, int width, int height); } ``` 现在,我们可以使用不同的DrawAPI实现来创建不同的Shape对象,比如: ```java DrawAPI redDrawAPI = new RedDrawAPI(); Shape redCircle = new Circle(100, 100, 10, redDrawAPI); redCircle.draw(); DrawAPI greenDrawAPI = new GreenDrawAPI(); Shape greenRectangle = new Rectangle(50, 50, 100, 200, greenDrawAPI); greenRectangle.draw(); ``` 这样就可以将形状的抽象部分和实现部分分离了。如果需要增加一种新的形状或者实现,只需要创建一个新的类实现Shape或者DrawAPI接口即可,不需要修改原有的代码。 完整的代码示例可以参考以下链接:https://github.com/iluwatar/java-design-patterns/tree/master/bridge

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值