#pragma once ///////////////////////////////////////////////////////////////////// // ObjectAdapter.h - Declarations for Skeleton Object Adapter // // // // Jim Fawcett, CSE776 - Design Patterns, Fall 2014 // ///////////////////////////////////////////////////////////////////// #include // primary Target Hierarchy struct Target { virtual ~Target() {} virtual std::string doTargetOperation() = 0; }; class TargetImpl : public Target { public: static Target* create() { return new TargetImpl; } virtual std::string doTargetOperation() { return "\n TargetImpl here"; } }; // secondary Adaptee Hierarchy struct Adaptee { virtual ~Adaptee() {} virtual std::string doAdapteeOperation() = 0; }; class AdapteeImpl : public Adaptee { public: static Adaptee* create() { return new AdapteeImpl; } virtual std::string doAdapteeOperation() override { return "\n adapteeImpl here"; } }; // pulling AdapteeImpl into Target Hierarchy by composing with AdapteeImpl class ObjectAdapter : public Target { Adaptee* pAdaptee = 0; // will bind to AdapteeImpl - see below public: static Target* create() { return new ObjectAdapter; } ObjectAdapter() { pAdaptee = AdapteeImpl::create(); }; virtual ~ObjectAdapter() {} virtual std::string doTargetOperation() override { return std::string("\n Object Adapter using Adaptee:") + pAdaptee->doAdapteeOperation(); } private: virtual std::string doAdapteeOperation() { return pAdaptee->doAdapteeOperation(); } }; class Factory { public: static Target* create(int i) { switch (i) { case 0: return TargetImpl::create(); break; case 1: return ObjectAdapter::create(); break; default: return nullptr; } } };