标签:
别名:handle/body
这个模式体现了组合相对于继承的优势。
当一个抽象可能有多个实现时,通经常使用继承来协调它们。抽象类定义对该抽象的接口,而详细的子类则用不同方式加以实现。可是此方法有时不够灵活。继承机制将抽象部分与它的实现部分固定在一起,使得难以对抽象部分和实现部分独立地进行改动、扩充和重用。而组合方式能够使得通过抽象部分与实现部分进行独立的改动。
Client
— Bridge模式的使用者
?Abstraction
— 定义抽象类的接口。
— 维护一个指向Im p l e m e n t o r 类型对象的指针。
?RefinedAbstraction
— 扩充由Ab s t r a c t i o n定义的接口。
?Implementor
— 定义实现类的接口,该接口不一定要与A b s t r a c t i o n的接口全然一致;其实这两个接口能够全然不同。一般来讲, I m p l e m e n t o r接口仅提供基本操作,而 A b s t r a c t i o n则定义了基于这些基本操作的较高层次的操作。
?ConcreteImplementor
— 实现Im p l e m e n t o r 接口并定义它的详细实现。
1) 分离接口及事实上现部分 一个实现未必不变地绑定在一个接口上。抽象类的实现能够在执行时刻进行配置,一个对象甚至能够在执行时刻改变它的实现。
2) 提高可扩充性 你能够独立地对Ab s t r a c t i o n和Im p l e m e n t o r 层次结构进行扩充。
3 ) 实现细节对客户透明 你能够对客户隐藏实现细节,比如共享I m p l e m e n t o r对象以及对应的引用计数机制(假设有的话) 。
代码片断1:Abstraction.h
//Abstraction.h
#ifndef _ABSTRACTION_H_
#define _ABSTRACTION_H_
class AbstractionImp;
class Abstraction
{
public:
virtual ~Abstraction();
virtual voidOperation() = 0;
protected:
Abstraction();
private:
};
class RefinedAbstraction :public Abstraction
{
public:
RefinedAbstraction(AbstractionImp*
imp);
~RefinedAbstraction();
void Operation();
protected:
private:
AbstractionImp* _imp;
};
#endif //~_ABSTRACTION_H
//Abstraction.cpp
#include"Abstraction.h"
#include"AbstractionImp.h"
#include<iostream>
using namespace std;
Abstraction::Abstraction()
{
}
Abstraction::~Abstraction()
{
}
RefinedAbstraction::RefinedAbstraction(Abstra
ctionImp* imp)
{
_imp = imp;
}
RefinedAbstraction::~RefinedAbstraction()
{
}
void RefinedAbstraction::Operation()
{
_imp->Operation();
}
代码片断3:AbstractionImp.h
//AbstractionImp.h
#ifndef _ABSTRACTIONIMP_H_
#define _ABSTRACTIONIMP_H_
class AbstractionImp
{
public:
virtual ~AbstractionImp();
virtual voidOperation() = 0;
protected:
AbstractionImp();
private:
};
class ConcreteAbstractionImpA :public
AbstractionImp
{
public:
ConcreteAbstractionImpA();
~ConcreteAbstractionImpA();
virtual voidOperation();
protected:
private:
};
class ConcreteAbstractionImpB :public
AbstractionImp
{
public:
ConcreteAbstractionImpB();
~ConcreteAbstractionImpB();
virtual voidOperation();
protected:
private:
};
#endif //~_ABSTRACTIONIMP_H_
代码片断4:AbstractionImp.cpp
//AbstractionImp.cpp
#include"AbstractionImp.h"
#include<iostream>
using namespace std;
AbstractionImp::AbstractionImp()
{
}
AbstractionImp::~AbstractionImp()
{
}
void AbstractionImp::Operation()
{
cout << "AbstractionImp....imp..." << endl;
}
ConcreteAbstractionImpA::ConcreteAbstractio
nImpA()
{
}
ConcreteAbstractionImpA::~ConcreteAbstracti
onImpA()
{
}
void ConcreteAbstractionImpA::Operation()
{
cout << "ConcreteAbstractionImpA...." << e
ndl;
}
ConcreteAbstractionImpB::ConcreteAbstractio
nImpB()
{
}
ConcreteAbstractionImpB::~ConcreteAbstracti
onImpB()
{
}
void ConcreteAbstractionImpB::Operation()
{
cout << "ConcreteAbstractionImpB...." << e
ndl;
}
代码片断5:main.cpp
//main.cpp
#include"Abstraction.h"
#include"AbstractionImp.h"
#include<iostream>
using namespace std;
int main(int argc, char*argv[])
{
AbstractionImp*imp = new
ConcreteAbstractionImpA();
Abstraction*abs = new
RefinedAbstraction(imp);
abs->Operation();
return 0;
}
Implementor类:
1. package com.qianyan.bridge;
2.
3. public interface Engine {
4.
5. /**
6. * 安装发动引擎
7. */
8. public void installEngine();
9. }
ConcreteImplementor类:
1. package com.qianyan.bridge;
2.
3. public class Engine2000 implements Engine {
4.
5. @Override
6. public void installEngine() {
7. System.out.println("安装2000CC发动引擎");
8. }
9.
10.}
1. package com.qianyan.bridge;
2.
3. public class Engine2200 implements Engine {
4.
5. @Override
6. public void installEngine() {
7. System.out.println("安装2200CC发动引擎");
8. }
9.
10.}
Abstraction类:
1. package com.qianyan.bridge;
2.
3. public abstract class Vehicle {
4.
5. private Engine engine;
6.
7. public Vehicle(Engine engine) {
8. this.engine = engine;
9. }
10.
11. public Engine getEngine() {
12. return engine;
13. }
14.
15. public void setEngine(Engine engine) {
16. this.engine = engine;
17. }
18.
19. public abstract void installEngine();
20.}
Refined Abstraction类:
1. package com.qianyan.bridge;
2.
3. public class Bus extends Vehicle {
4.
5. public Bus(Engine engine) {
6. super(engine);
7. }
8.
9. @Override
10. public void installEngine() {
11. System.out.print("Bus:");
12. this.getEngine().installEngine();
13. }
14.
15.}
1. package com.qianyan.bridge;
2.
3. public class Jeep extends Vehicle {
4.
5. public Jeep(Engine engine) {
6. super(engine);
7. }
8. @Override
9. public void installEngine() {
10. System.out.print("Jeep:");
11. this.getEngine().installEngine();
12. }
13.
14.}
1. package com.qianyan.bridge;
2.
3. public class MainClass {
4. public static void main(String[] args) {
5. Engine engine2000 = new Engine2000();
6. Engine engine2200 = new Engine2200();
7.
8. Vehicle bus = new Bus(engine2000);
9. bus.installEngine();
10.
11. Vehicle jeep = new Jeep(engine2200);
12. jeep.installEngine();
13. }
14.}
Result:
1. Bus:安装2000CC发动引擎
标签:
原文地址:http://www.cnblogs.com/gcczhongduan/p/4186958.html