桥模式(Bridge)
Bridge模式简介:
将抽象部分与它的实现部分分离,使它们都可以独立的变化。
合成、聚合复用原则:
- 合成/组合(Composition),一种强拥有关系,体现严格的整体与部分的关系,整体与部分的生命周期一样。
- 聚合(Aggregation),一种弱的拥有关系,体现整体包含对象的关系,对象不是整体的某一部分。
Bridge模式结构:
C++代码
Implemantor类 & ConcreteImplementorA和B派生类:
//file: Implementor.h
#pragma once
class Implementor
{
public:
Implementor();
virtual ~Implementor();
virtual void operation();
};
class ConcreteImplementorA : public Implementor
{
public:
ConcreteImplementorA();
virtual ~ConcreteImplementorA();
virtual void operation();
};
class ConcreteImplementorB : public Implementor
{
public:
ConcreteImplementorB();
virtual ~ConcreteImplementorB();
virtual void operation();
};
//file: Implementor.cpp
#include "pch.h"
#include "Implementor.h"
#include <iostream>
using namespace std;
//Implementor
Implementor::Implementor() {}
Implementor::~Implementor() {}
void Implementor::operation() {}
//ConcreteImplementorA
ConcreteImplementorA::ConcreteImplementorA() {}
ConcreteImplementorA::~ConcreteImplementorA() {}
void ConcreteImplementorA::operation()
{
cout << "Concrete Implement A's Func ." << endl;
}
//ConcreteImplementorB
ConcreteImplementorB::ConcreteImplementorB() {}
ConcreteImplementorB::~ConcreteImplementorB() {}
void ConcreteImplementorB::operation()
{
cout << "Concrete Implement B's Func ." << endl;
}
Abstraction类 & ConcreteAbstraction1和2派生类:
//file: Abstraction.h
#pragma once
#include "Implementor.h"
class Abstraction
{
protected:
Implementor * m_pImp;
public:
Abstraction();
virtual ~Abstraction();
Abstraction(Implementor * pImp);
virtual void operation();
};
class ConcreteAbstraction1 : public Abstraction
{
public:
ConcreteAbstraction1();
ConcreteAbstraction1(Implementor *pImp);
virtual ~ConcreteAbstraction1();
virtual void operation();
};
class ConcreteAbstraction2 : public Abstraction
{
public:
ConcreteAbstraction2();
ConcreteAbstraction2(Implementor * pImp);
virtual ~ConcreteAbstraction2();
virtual void operation();
};
//file: Abstraction.cpp
#include "pch.h"
#include "Abstraction.h"
#include <iostream>
//Abstraction
Abstraction::Abstraction() {}
Abstraction::~Abstraction()
{
delete m_pImp;
}
Abstraction::Abstraction(Implementor *pImp)
{
m_pImp = pImp;
}
void Abstraction::operation() {}
//ConcreteAbstraction1
ConcreteAbstraction1::ConcreteAbstraction1() {}
ConcreteAbstraction1::ConcreteAbstraction1(Implementor *pImp) : Abstraction(pImp) {}
ConcreteAbstraction1::~ConcreteAbstraction1() {}
void ConcreteAbstraction1::operation()
{
std::cout << "Concrete Abstraction 1's Addition Action ." << std::endl;
m_pImp->operation();
}
//ConcreteAbstraction2
ConcreteAbstraction2::ConcreteAbstraction2() {}
ConcreteAbstraction2::ConcreteAbstraction2(Implementor *pImp) : Abstraction(pImp) {}
ConcreteAbstraction2::~ConcreteAbstraction2() {}
void ConcreteAbstraction2::operation()
{
std::cout << "Concrete Abstraction 2's Addition Action ." << std::endl;
m_pImp->operation();
}
客户端代码:
//file: BridgePattern.cpp : This file contains the 'main' function. Program execution begins and ends there.
#include "pch.h"
#include "Abstraction.h"
#include <iostream>
using namespace std;
int main()
{
Implementor *imp = new ConcreteImplementorA();
Abstraction *a1 = new ConcreteAbstraction1(imp);
a1->operation();
Abstraction *a2 = new ConcreteAbstraction2(new ConcreteImplementorB());
a2->operation();
char c;
cin >> c;
return 0;
}
C#代码
Implemantor类 & ConcreteImplementorA和B派生类:
public abstract class Implementor
{
public abstract void Operation();
}
public class ConcreteImplementorA : Implementor
{
public override void Operation()
{
Console.WriteLine("Concrete Implementor A's Func .");
}
}
public class ConcreteImplementorB : Implementor
{
public override void Operation()
{
Console.WriteLine("Concrete Implementor B's Func .");
}
}
Abstraction类 & ConcreteAbstraction1和2派生类:
public class Abstraction
{
protected Implementor m_imple;
public Abstraction() { }
public Abstraction(Implementor implementor)
{
this.m_imple = implementor;
}
public void SetImple(Implementor implementor)
{
this.m_imple = implementor;
}
public virtual void Operation()
{
if (m_imple != null)
m_imple.Operation();
}
}
public class ConcreteAbstraction1 : Abstraction
{
public ConcreteAbstraction1(Implementor implementor) : base(implementor) { }
public override void Operation()
{
Console.WriteLine("The ConcreteAbstraction 1's additional action .");
base.Operation();
}
}
public class ConcreteAbstraction2 : Abstraction
{
public ConcreteAbstraction2(Implementor implementor) : base(implementor) { }
public override void Operation()
{
Console.WriteLine("The ConcreteAbstraction 2's additional action .");
base.Operation();
}
}
客户端代码:
class Program
{
static void Main(string[] args)
{
Implementor imp = new ConcreteImplementorA();
Abstraction absA = new ConcreteAbstraction1(imp);
absA.Operation();
Abstraction absB = new ConcreteAbstraction2(new ConcreteImplementorB());
absB.Operation();
Console.ReadKey(false);
}
}
REF
书籍:
设计模式与游戏开发、大话设计模式
GitHub: