C++设计模式之桥接模式(七)

bridge pattern

简介

  桥接模式(bridge pattern)是一种结构型模式;它是将抽象部分和实现部分进行分离,使它们都可以独立变化,互不干扰。常用于一个对象有多个变化因素,以及多个对象有共同的变化因素时,可以考虑桥接模式。
在这里插入图片描述

  • Abstract: 用于定义抽象类的借口,并维护一个指向ImplementAbstract类实现的指针。
  • RefineAbstractA/B: 扩充Abstract抽象类;在RefineAbstractA/B中可以调用在ImplementAbstract中实现的业务方法,即Operation()。
  • ConcreteImplementA/B: 实现ImplementAbstract中定义的接口,在不同的ConcreteImplementA/B中提供不同的实现方法。

优点: 将实现抽离出来,再实现抽象,使得对象的具体实现依赖与抽象,满足依赖倒转原则。将共享的部分,抽离出来,降低代码的冗余、提高代码的灵活性。
缺点: 必须要知道选择哪一种类需要实例化。

代码

// bridge_patern.h
class Abstract {
public:
    virtual void Operation() = 0;
}; // Abstract

class ImpelmentAbstract{
public:
    virtual void Operation();
}; // ImpelmentAbstract

class ConcreteImpelmentA: public ImpelmentAbstract{
public:
    void Operation() override;
}; // ConcretImpelmentA

class ConcreteImpelmentB: public ImpelmentAbstract{
public:
    void Operation() override;
}; // ConcretImpelmentB

class RefineAbstractA: public Abstract{
public:
    void Operation() override;
    RefineAbstractA(std::shared_ptr<ImpelmentAbstract>& impelment);
private:
    std::shared_ptr<ImpelmentAbstract> impelment_;
}; // RefineAbstractA

class RefineAbstractB: public Abstract{
public:
    void Operation() override;
    RefineAbstractB(std::shared_ptr<ImpelmentAbstract>& impelment);
private:
    std::shared_ptr<ImpelmentAbstract> impelment_;
}; // RefineAbstractB
// bridge_pattern.cpp
void ImpelmentAbstract::Operation() {

}

void ConcreteImpelmentA::Operation() {
    std::cout << "ConcretImpelmentA::Operation()" << std::endl;
}

void ConcreteImpelmentB::Operation() {
    std::cout << "ConcretImpelmentB::Operation()" << std::endl;
}

RefineAbstractA::RefineAbstractA(std::shared_ptr<patterns::ImpelmentAbstract>& impelment) {
    this->impelment_ = impelment;
}

void RefineAbstractA::Operation() {
    std::cout << "RefineAbstractA::Operation()" << std::endl;
    this->impelment_->Operation();
}

RefineAbstractB::RefineAbstractB(std::shared_ptr<patterns::ImpelmentAbstract>& impelment)
    : impelment_(impelment){

}

void RefineAbstractB::Operation() {
    std::cout << "RefineAbstractB::Operation()" << std::endl;
    this->impelment_->Operation();
}

int main(){
    std::shared_ptr<ImpelmentAbstract> impelment_a = std::make_shared<ConcreteImpelmentA>();
    std::shared_ptr<Abstract> abstract_a = std::make_shared<RefineAbstractA>(impelment_a);
    abstract_a->Operation();
    std::cout << "*******************************" << std::endl;

    std::shared_ptr<ImpelmentAbstract> impelment_b = std::make_shared<ConcreteImpelmentB>();
    abstract_a.reset(new RefineAbstractA(impelment_b));
    abstract_a->Operation();
    std::cout << "-------------------------------" << std::endl;

    std::shared_ptr<Abstract> abstract_b = std::make_shared<RefineAbstractB>(impelment_a);
    abstract_b->Operation();
    std::cout << "*******************************" << std::endl;

    abstract_b.reset(new RefineAbstractB(impelment_b));
    abstract_b->Operation();
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值