设计模式7-桥接模式

引用: 2. 桥接模式 — Graphic Design Patterns

桥接模式(Bridge Pattern):将抽象部分与它的实现部分分离,使它们都可以独立地变化。它是一种对象结构型模式,又称为柄体(Handle and Body)模式或接口(Interface)模式。

桥接模式包含如下角色:

  • Abstraction:抽象类
  • RefinedAbstraction:扩充抽象类
  • Implementor:实现类接口
  • ConcreteImplementor:具体实现类

    ../_images/Bridge.jpg

     一、go语言版

  • package main
    import "fmt"
    type Abstraction interface {
        Operation()
    }
    type Implementor interface {
        Operation()
    }
    type RefineAbstractionA struct {
        imp Implementor
    }
    type RefineAbstractionB struct {
        imp Implementor
    }
    type ConcreteImplementorA struct {}
    type ConcreteImplementorB struct {}
    func (ref* RefineAbstractionA) Operation() {
        fmt.Printf("RefineAbstractionA Operation\n")
        ref.imp.Operation()
    }
    func (ref* RefineAbstractionB) Operation() {
        fmt.Printf("RefineAbstractionB Operation\n")
        ref.imp.Operation()
    }
    func (* ConcreteImplementorA) Operation() {
        fmt.Printf("ConcreteImplementorA Operation\n")
    }
    func (* ConcreteImplementorB) Operation() {
        fmt.Printf("ConcreteImplementorB Operation\n")
    }
    func NewRefineAbstractionA(imp Implementor) * RefineAbstractionA{
        return  &RefineAbstractionA{
            imp : imp,
        }
    }
    func NewRefineAbstractionB(imp Implementor) * RefineAbstractionB{
        return  &RefineAbstractionB{
            imp : imp,
        }
    }
    func NewConcreteImplementorA() Implementor{
        return &ConcreteImplementorA{}
    }
    func NewConcreteImplementorB() Implementor{
        return &ConcreteImplementorB{}
    }
    func main() {
        refineAbstractionA := NewRefineAbstractionA(NewConcreteImplementorB())
        refineAbstractionA.Operation()
    }

  • 二、c++语言版

  • #include <iostream>
    using namespace std;
    class Abstraction
    {
    public:
        virtual void Operation() = 0;
    };
    class Implementor
    {
    public:
        virtual void Operation()=0;
    };
    class RefineAbstractionA : public Abstraction
    {
    public:
        RefineAbstractionA(Implementor *imp) {
            imp_ = imp;
        }
        void Operation() {
            cout << "RefineAbstractionA Operation" << endl;
            imp_->Operation();
        }
    private:
        Implementor *imp_;
    };
    class RefineAbstractionB : public Abstraction
    {
    public:
        RefineAbstractionB(Implementor *imp) {
            imp_ = imp;
        }
        void Operation() {
            cout << "RefineAbstractionB Operation" << endl;
            imp_->Operation();
        }
    private:
        Implementor *imp_;
    };
    class ConcreteImplementorA : public Implementor
    {
    public:
         void Operation() {
             cout << "ConcreteImplementorA Operation" << endl;
         }
    };
    class ConcreteImplementorB : public Implementor
    {
    public:
         void Operation() {
             cout << "ConcreteImplementorB Operation" << endl;
         }
    };
    int main()
    {
        Implementor *implementor = new ConcreteImplementorB;
        Abstraction *abstraction = new RefineAbstractionA(implementor);
        abstraction->Operation();
        return 0;
    }

  • 三、c语言版

  • #include <stdio.h>
    typedef struct _Implementor {
        char *type;
        void (*Operation)();
    }Implementor;
    typedef struct _Abstraction {
        Implementor *imp;
        char *type;
        void (*Operation)(struct _Abstraction *abs);
    }Abstraction;
    void Abstraction_show(struct _Abstraction *abs)
    {
        printf("%s\n",abs->type);
        abs->imp->Operation(abs->imp->type);
    }
    void Implementor_show(char *type)
    {
        printf("Implementor_show %s\n", type);
    }
    Abstraction RefineAbstractionA = {
        .type = "RefineAbstractionA",
        .Operation = Abstraction_show,
    };
    Abstraction RefineAbstractionB = {
         .type = "RefineAbstractionB",
        .Operation = Abstraction_show,
    };
    Implementor ConcreteImplementorA = {
         .type = "ConcreteImplementorA",
        .Operation = Implementor_show,
    };
    Implementor ConcreteImplementorB = {
        .type = "ConcreteImplementorB",
        .Operation = Implementor_show,
    };
    int main()
    {
        Abstraction *abs = &RefineAbstractionA;
        abs->imp = &ConcreteImplementorB;  
        abs->Operation(abs);
        return 0;
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值