引用: 2. 桥接模式 — Graphic Design Patterns
桥接模式(Bridge Pattern):将抽象部分与它的实现部分分离,使它们都可以独立地变化。它是一种对象结构型模式,又称为柄体(Handle and Body)模式或接口(Interface)模式。
桥接模式包含如下角色:
- Abstraction:抽象类
- RefinedAbstraction:扩充抽象类
- Implementor:实现类接口
- ConcreteImplementor:具体实现类
一、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;
}