标签:
AbstractionImp.h
#ifndef _ABSTRACTIONIMP_H #define _ABSTRACTIONIMP_H class AbstractionImp { public : virtual ~AbstractionImp(); virtual void Operation() = 0; protected : AbstractionImp(); }; class ConcreateAbstractionImpA : public AbstractionImp { public : ConcreateAbstractionImpA(); ~ConcreateAbstractionImpA(); virtual void Operation(); }; class ConcreateAbstractionImpB : public AbstractionImp { public : ConcreateAbstractionImpB(); ~ConcreateAbstractionImpB(); virtual void Operation(); }; #endif
AbstractionImp.cpp
#include "AbstractionImp.h" #include <iostream> using namespace std; AbstractionImp::AbstractionImp() {} AbstractionImp::~AbstractionImp() {} void AbstractionImp::Operation() { cout << "AbstractionImp...imp" << endl; } ConcreateAbstractionImpA::ConcreateAbstractionImpA() {} ConcreateAbstractionImpA::~ConcreateAbstractionImpA() {} void ConcreateAbstractionImpA::Operation() { cout << "ConcreateAbstractionImpA..." << endl; } ConcreateAbstractionImpB::ConcreateAbstractionImpB() {} ConcreateAbstractionImpB::~ConcreateAbstractionImpB() {} void ConcreateAbstractionImpB::Operation() { cout << "ConcreateAbstractionImpB..." << endl; }
Abstraction.h
#ifndef _ABSTRACTION_H #define _ABSTRACTION_H class AbstractionImp; class Abstraction { public : virtual ~Abstraction(); virtual void Operation() = 0; protected : Abstraction(); }; class RefinedAbstraction : public Abstraction { public : RefinedAbstraction(AbstractionImp *imp); ~RefinedAbstraction(); void Operation(); private : AbstractionImp *_imp; }; #endif
Abstraction.cpp
#include "Abstraction.h" #include "AbstractionImp.h" #include <iostream> using namespace std; Abstraction::Abstraction() {} Abstraction::~Abstraction() {} RefinedAbstraction::RefinedAbstraction(AbstractionImp *imp) { _imp = imp; } RefinedAbstraction::~RefinedAbstraction() {} void RefinedAbstraction::Operation() { _imp->Operation(); }
Main.cpp
#include "Abstraction.h" #include "AbstractionImp.h" #include <iostream> using namespace std; int main(int argc, char *argv[]) { AbstractionImp *imp = new ConcreateAbstractionImpA(); Abstraction *abs = new RefinedAbstraction(imp); abs->Operation(); return 0; }
标签:
原文地址:http://www.cnblogs.com/Susake/p/4525355.html