// adapter_class.cpp
#include <stdio.h>
#ifdef _X_RELEASE_
#define TRACE(str)
#else
#define TRACE(str) printf("%s/n", str)
#endif
// 被适配者
class CAdaptee
{
public:
CAdaptee()
{
TRACE("CAdaptee");
}
virtual ~CAdaptee()
{
TRACE("~CAdaptee");
}
void SpecialRequest()
{
TRACE("SpecialRequest");
}
} ;
// 客户操作的接口
class CTarget
{
public:
CTarget()
{
TRACE("CTarget");
}
virtual ~CTarget()
{
TRACE("~CTarget");
}
virtual void Request() = 0;
} ;
// 类适配器,继承方式
class CAdapter : public CTarget, private CAdaptee
{
public:
CAdapter()
{
TRACE("CAdapter");
}
virtual ~CAdapter()
{
TRACE("~CAdapter");
}
virtual void Request()
{
TRACE("CAdapter::Request");
SpecialRequest(); // 直接转发请求
}
} ;
int main()
{
CTarget* pTarget = new CAdapter();
if (!pTarget)
{
TRACE("error 1");
return 1;
}
pTarget->Request();
delete pTarget;
pTarget = 0;
return 0;
}
/*
root@ubuntu:/home/hgc/test# ./adapter_class
CTarget
CAdaptee
CAdapter
CAdapter::Request
SpecialRequest
~CAdapter
~CAdaptee
~CTarget
root@ubuntu:/home/hgc/test#
*/
// adapter_object.cpp
#include <stdio.h>
#ifdef _X_RELEASE_
#define TRACE(str)
#else
#define TRACE(str) printf("%s/n", str)
#endif
// 被适配者
class CAdaptee
{
public:
CAdaptee()
{
TRACE("CAdaptee");
}
virtual ~CAdaptee()
{
TRACE("~CAdaptee");
}
void SpecialRequest()
{
TRACE("SpecialRequest");
}
} ;
// 客户操作的接口
class CTarget
{
public:
CTarget()
{
TRACE("CTarget");
}
virtual ~CTarget()
{
TRACE("~CTarget");
}
virtual void Request() = 0;
} ;
// 类适配器,继承方式
class CAdapter : public CTarget
{
private:
CAdaptee* m_pAdaptee;
public:
CAdapter(CAdaptee* pAdaptee)
{
TRACE("CAdapter");
m_pAdaptee = pAdaptee;
}
virtual ~CAdapter()
{
TRACE("~CAdapter");
if (m_pAdaptee)
{
delete m_pAdaptee;
m_pAdaptee = 0;
}
}
virtual void Request()
{
TRACE("CAdapter::Request");
m_pAdaptee->SpecialRequest(); // 直接转发请求
}
} ;
int main()
{
CTarget* pTarget = new CAdapter(new CAdaptee());
if (!pTarget)
{
TRACE("error 1");
return 1;
}
pTarget->Request();
delete pTarget;
pTarget = 0;
return 0;
}
/*
root@ubuntu:/home/hgc/test# g++ adapter_object.cpp -o adapter_object -g -Wall
root@ubuntu:/home/hgc/test# ./adapter_object
CAdaptee
CTarget
CAdapter
CAdapter::Request
SpecialRequest
~CAdapter
~CAdaptee
~CTarget
root@ubuntu:/home/hgc/test#
*/
// adapter_demo_class.cpp
#include <stdio.h>
#ifdef _X_RELEASE_
#define TRACE(str)
#else
#define TRACE(str) printf("%s/n", str)
#endif
// 注意: x轴向右延伸,y轴向下延伸,原点在左上角
class Point
{
private:
int m_x, m_y;
public:
Point()
{
}
Point(int x, int y)
{
m_x = x;
m_y = y;
}
int GetX() const
{
return m_x;
}
int GetY() const
{
return m_y;
}
void Display()
{
printf("(%d, %d)/n", m_y, m_x);
}
};
// target
class Shape
{
public:
Shape()
{
}
virtual void BoundingBox(Point& topLeft, Point& bottomRight) const = 0; // 客户期待的接口
virtual bool IsEmpty() const = 0; // 客户期待的接口
};
// adaptee
class TextView
{
private:
int m_top, m_left, m_width, m_height;
public:
TextView(int top, int left, int width, int height)
{
m_top = top;
m_left = left;
m_width = width;
m_height = height;
}
// 获取原点的接口,需要适配
void GetOrigin(int& top, int& left) const
{
top = m_top;
left = m_left;
}
// 获取长(向右延伸)和高(向下延伸)的接口,需要适配
void GetExtent(int& width, int& height) const
{
width = m_width;
height = m_height;
}
virtual bool IsEmpty() const
{
if ((0 == m_width) || (0 == m_height))
{
return true;
}
return false;
}
};
// adapter (class)
class TextShape : public Shape, private TextView
{
public:
TextShape(int top, int left, int width, int height)
: TextView(top, left, width, height)
{
}
virtual void BoundingBox(Point& topLeft, Point& bottomRight) const;
virtual bool IsEmpty() const; // 该接口无需适配,可以直接使用
};
void TextShape::BoundingBox(Point& topLeft, Point& bottomRight) const
{
int top, left, width, height;
// 适配过程
GetOrigin(top, left);
GetExtent(width, height);
topLeft = Point(top, left);
bottomRight = Point(top + height, left + width);
}
bool TextShape::IsEmpty() const
{
// 因为是private继承的,所以定义一个相同的public接口,然后转发请求
return TextView::IsEmpty();
}
int main()
{
Shape* pShape = new TextShape(1, 0, 5, 4);
if (!pShape)
{
TRACE("error 1");
return 1;
}
if (!pShape->IsEmpty())
{
Point topLeft, bottomRight;
pShape->BoundingBox(topLeft, bottomRight);
topLeft.Display();
bottomRight.Display();
}
delete pShape;
pShape = 0;
return 0;
}
/*
root@ubuntu:/home/hgc/test# g++ adapter_demo_class.cpp -o adapter_demo_class -g -Wall
root@ubuntu:/home/hgc/test# ./adapter_demo_class
(0, 1)
(5, 5)
root@ubuntu:/home/hgc/test#
*/
// adapter_demo_object.cpp
#include <stdio.h>
#ifdef _X_RELEASE_
#define TRACE(str)
#else
#define TRACE(str) printf("%s/n", str)
#endif
// 注意: x轴向右延伸,y轴向下延伸,原点在左上角
class Point
{
private:
int m_x, m_y;
public:
Point()
{
}
Point(int x, int y)
{
m_x = x;
m_y = y;
}
int GetX() const
{
return m_x;
}
int GetY() const
{
return m_y;
}
void Display()
{
printf("(%d, %d)/n", m_y, m_x);
}
};
// target
class Shape
{
public:
Shape()
{
}
virtual void BoundingBox(Point& topLeft, Point& bottomRight) const = 0; // 客户期待的接口
virtual bool IsEmpty() const = 0; // 客户期待的接口
};
// adaptee
class TextView
{
private:
int m_top, m_left, m_width, m_height;
public:
TextView(int top, int left, int width, int height)
{
m_top = top;
m_left = left;
m_width = width;
m_height = height;
}
// 获取原点的接口,需要适配
void GetOrigin(int& top, int& left) const
{
top = m_top;
left = m_left;
}
// 获取长(向右延伸)和高(向下延伸)的接口,需要适配
void GetExtent(int& width, int& height) const
{
width = m_width;
height = m_height;
}
virtual bool IsEmpty() const
{
if ((0 == m_width) || (0 == m_height))
{
return true;
}
return false;
}
};
// adatper (object)
class TextShape : public Shape
{
private:
TextView* m_pTextView;
public:
TextShape(TextView* pTextView);
virtual ~TextShape();
virtual void BoundingBox(Point& topLeft, Point& bottomRight) const;
virtual bool IsEmpty() const; // 该接口无需适配,可以直接使用
};
TextShape::TextShape(TextView* pTextView)
{
m_pTextView = pTextView;
}
TextShape::~TextShape()
{
if (m_pTextView)
{
delete m_pTextView;
m_pTextView = 0;
}
}
void TextShape::BoundingBox(Point& topLeft, Point& bottomRight) const
{
int top, left, width, height;
// 适配过程
m_pTextView->GetOrigin(top, left);
m_pTextView->GetExtent(width, height);
topLeft = Point(top, left);
bottomRight = Point(top + height, left + width);
}
bool TextShape::IsEmpty() const
{
// 因为是private继承的,所以定义一个相同的public接口,然后转发请求
return m_pTextView->IsEmpty();
}
int main()
{
Shape* pShape = new TextShape(new TextView(1, 0, 5, 4));
if (!pShape)
{
TRACE("error 1");
return 1;
}
if (!pShape->IsEmpty())
{
Point topLeft, bottomRight;
pShape->BoundingBox(topLeft, bottomRight);
topLeft.Display();
bottomRight.Display();
}
delete pShape;
pShape = 0;
return 0;
}
/*
root@ubuntu:/home/hgc/test# g++ adapter_demo_object.cpp -o adapter_demo_object -g -Wall
root@ubuntu:/home/hgc/test# ./adapter_demo_object
(0, 1)
(5, 5)
root@ubuntu:/home/hgc/test#
*/