原网址:https
://blog
.csdn
.net
/mrailence
/article
/details
/53103284
#include <iostream>
using namespace std
;
class AbstractionImlement {
public:
AbstractionImlement() {}
virtual ~AbstractionImlement() {}
virtual void Operation() = 0;
};
class ConcreteAbstractionImplemetA:public AbstractionImlement
{
public:
ConcreteAbstractionImplemetA() {}
~ConcreteAbstractionImplemetA() {}
void Operation() {
cout
<< "TongXunLu Operation" << endl
;
}
};
class ConcreteAbstractionImplemetB:public AbstractionImlement
{
public:
ConcreteAbstractionImplemetB() {}
~ConcreteAbstractionImplemetB() {}
void Operation() {
cout
<< "RuanJian Operation" << endl
;
}
};
class Abstraction {
public:
Abstraction() {}
virtual ~Abstraction() {}
virtual void Operation() = 0;
};
class RefinedAbstrctionA : public Abstraction
{
public:
RefinedAbstrctionA(AbstractionImlement
*Imp
):m_Imp(Imp
) {}
~RefinedAbstrctionA() {}
void Operation() {
cout
<< "ShouJiPingPaiA::Operation" << endl
;
m_Imp
->Operation();
}
private:
AbstractionImlement
* m_Imp
;
};
class RefinedAbstrctionB : public Abstraction
{
public:
RefinedAbstrctionB(AbstractionImlement
*Imp
):m_Imp(Imp
) {}
~RefinedAbstrctionB() {}
void Operation() {
cout
<< "ShouJiPingPaiB::Operation" << endl
;
m_Imp
->Operation();
}
private:
AbstractionImlement
* m_Imp
;
};
int main(){
AbstractionImlement
* imp
= new ConcreteAbstractionImplemetA();
Abstraction
* abs
= new RefinedAbstrctionA(imp
);
abs
->Operation();
cout
<< "-------------------" << endl
;
AbstractionImlement
* imp1
= new ConcreteAbstractionImplemetB();
Abstraction
* abs1
= new RefinedAbstrctionA(imp1
);
abs1
->Operation();
cout
<< "-------------------" << endl
;
AbstractionImlement
* imp2
= new ConcreteAbstractionImplemetA();
Abstraction
* abs2
= new RefinedAbstrctionB(imp2
);
abs2
->Operation();
cout
<< "----------------------" << endl
;
AbstractionImlement
* imp3
= new ConcreteAbstractionImplemetB();
Abstraction
* abs3
= new RefinedAbstrctionB(imp3
);
abs3
->Operation();
return 0;
}
输出结果:
ShouJiPingPaiA
::Operation
TongXunLu Operation
-------------------
ShouJiPingPaiA
::Operation
RuanJian Operation
-------------------
ShouJiPingPaiB
::Operation
TongXunLu Operation
----------------------
ShouJiPingPaiB
::Operation
RuanJian Operation