类适配器
# include <queue>
# include <iostream>
# include <algorithm>
# include <iterator>
using namespace std;
class Target
{
public:
virtual ~ Target ( ) { }
virtual void method ( ) = 0 ;
} ;
class Adaptee
{
public:
void spec_method ( )
{
std:: cout << "spec_method run" << std:: endl;
}
} ;
class Adapter : public Target, public Adaptee
{
public:
void method ( ) override
{
std:: cout << "call from Adapter: " << std:: endl;
spec_method ( ) ;
}
} ;
int main ( int argc, char * * argv)
{
Adaptee adaptee;
adaptee. spec_method ( ) ;
std:: cout << std:: endl;
Adapter adapter;
adapter. method ( ) ;
return 0 ;
}
对象适配器
# include <queue>
# include <iostream>
# include <algorithm>
# include <iterator>
using namespace std;
class Target
{
public:
virtual ~ Target ( ) { }
virtual void method ( ) = 0 ;
} ;
class Adaptee
{
public:
void spec_method ( )
{
std:: cout << "spec_method run" << std:: endl;
}
} ;
class Adapter : public Target
{
public:
Adapter ( Adaptee* adaptee)
: adaptee_{ adaptee}
{
}
void method ( ) override
{
std:: cout << "call from Adapter: " << std:: endl;
adaptee_-> spec_method ( ) ;
}
private:
Adaptee* adaptee_;
} ;
int main ( int argc, char * * argv)
{
Adaptee adaptee;
adaptee. spec_method ( ) ;
std:: cout << std:: endl;
Adapter adapter ( & adaptee) ;
adapter. method ( ) ;
return 0 ;
}