情景:接到一個(gè)新任務(wù)再菊,需求是實(shí)現(xiàn)一個(gè)簡(jiǎn)單的繪圖編輯器,工具需要基本滿足繪制常用圖元(線宏邮,多邊形训裆,正文等)以生成圖片和圖表。
分析:繪圖編輯器的關(guān)鍵抽象是圖形對(duì)象蜀铲,圖形對(duì)象需要滿足能夠繪制自身,且擁有一個(gè)可編輯的形狀(繪制時(shí)的狀態(tài)保存)属百。常見(jiàn)的记劝,圖形對(duì)象的接口可以由一個(gè)稱(chēng)為Shape的抽象類(lèi)定義,那么接下來(lái)我們便可以對(duì)每一種具象的圖像對(duì)象定義成Shape的子類(lèi)族扰,比如直線可以是LineShape厌丑,多邊形可以是PolygonShape等。
問(wèn)題:現(xiàn)在看來(lái)一切都在正常進(jìn)行的渔呵,LineShape跟PolygonShape的工作得很好怒竿,基本滿足了我們得需求,但當(dāng)我們開(kāi)始著手“正文”圖形對(duì)象時(shí)扩氢,問(wèn)題出現(xiàn)了耕驰,因?yàn)榧词故腔镜恼木庉嬕惨婕暗綇?fù)雜的屏幕刷新和緩沖區(qū)管理,同時(shí)录豺,成品的用戶工具箱可能已經(jīng)提供了一個(gè)復(fù)雜的TextView類(lèi)用于顯示和編輯正文朦肘,一種可行的做法是使用TextView類(lèi)兼容Shape的接口以實(shí)現(xiàn)TextShape類(lèi)饭弓,這看起來(lái)不錯(cuò),但事實(shí)上我們不應(yīng)該僅僅為了實(shí)現(xiàn)一個(gè)應(yīng)用(繪圖編輯器)媒抠,就讓成品工具箱內(nèi)的TextView去采用一些與特定領(lǐng)域相關(guān)的接口弟断。
解決方案:使用TextShape來(lái)適配TextView跟Shape接口(在不改變TextView的類(lèi)結(jié)構(gòu)的情況下),我們可以有兩種方法做這件事:
1.TextShape繼承Shape接口跟TextView的實(shí)現(xiàn)趴生,
2.將整個(gè)TextView實(shí)例作為T(mén)extShape的組成部分阀趴,并且使用TextView接口實(shí)現(xiàn)TextShape。
這兩種方法其實(shí)就是Adapter模式的類(lèi)和對(duì)象版本苍匆,我們將TextShape稱(chēng)之為適配器Adapter刘急。
上圖主要說(shuō)明了在Shape類(lèi)中聲明的BoundingBox請(qǐng)求(獲取邊框兩點(diǎn)坐標(biāo))是如何被轉(zhuǎn)換成TextView類(lèi)中定義的GetExtent請(qǐng)求(獲取Text長(zhǎng)和寬)+GetOrigin請(qǐng)求(獲取Text左下坐標(biāo)-->原點(diǎn)),這樣TextShape將TextView接口與Shape接口進(jìn)行了匹配锉桑,繪圖編輯器就可以復(fù)用原先不兼容的TextView類(lèi)排霉。此外,Shape對(duì)象提供CreateManipulator請(qǐng)求民轴,Manipulator是一個(gè)抽象類(lèi)攻柠,其知道如何驅(qū)動(dòng)Shape類(lèi)響應(yīng)用戶的各類(lèi)操作,例如將圖形拖動(dòng)到一個(gè)新的位置后裸。Manipulator有不同的子類(lèi)瑰钮,例如子類(lèi)TextManipulator對(duì)應(yīng)TextShape。
關(guān)鍵字:
Target--定義Client使用的與特定領(lǐng)域相關(guān)的接口(Shape)
Client--與符合Target接口的對(duì)象協(xié)同(繪圖編輯器)
Adaptee--定義一個(gè)已經(jīng)存在的接口微驶,這個(gè)接口需要適配(TextView)
Adpater--對(duì)Adaptee接口與Target接口進(jìn)行適配(TextShape)
實(shí)現(xiàn):
struct Point
{
public:
Point(int x, int y) :m_iX(x), m_iY(y) {};
int m_iX;
int m_iY;
};
class Manipulator
{
public:
Manipulator(Shape *pShape) :m_pShape(pShape) {};
virtual ~Manipulator();
virtual bool MoveToPos(const Point &pointStart, const Point &pointEnd) = 0;
private:
Shape *m_pShape;
};
class Shape
{
public:
Shape();
virtual void BoundingBox(Point &bottomLeft, Point &topRight) const;
virtual Manipulator *CreateManipulator();
};
class TextView
{
public:
TextView();
void GetOrigin(int &x, int &y) const;
void GetExtent(int &width, int &height) const;
virtual bool IsEmpty() const;
};
class TextManipulator :public Manipulator
{
public:
TextManipulator(Shape *pShape);
virtual ~TextManipulator();
virtual bool MoveToPos(const Point &pointStart, const Point &pointEnd);
};
類(lèi)適配器模式(多繼承適配)
class TextShape : public Shape, private TextView
{
public:
TextShape();
virtual void BoundingBox(Point &bottomLeft, Point &topRight) const;
virtual bool IsEmpty() const;
virtual Manipulator *CreateManipulator();
};
void TextShape::BoundingBox(Point &bottomLeft, Point &topRight) const
{
int x, y, width, height = 0;
GetOrigin(x, y);
GetExtent(width, height);
bottomLeft = Point(x, y);
topRight = Point(x + width, y + height);
}
bool TextShape::IsEmpty() const
{
return TextView::IsEmpty();
}
Manipulator *TextShape::CreateManipulator()
{
return new TextManipulator(this);
}
對(duì)象適配器模式(對(duì)象組合)
class TextShape : public Shape
{
public:
TextShape(TextView *textView);
virtual void BoundingBox(Point &bottomLeft, Point &topRight) const;
virtual bool IsEmpty() const;
virtual Manipulator *CreateManipulator();
private:
TextView *m_pTextView;
};
TextShape::TextShape(TextView *textView)
{
m_pTextView = textView;
}
void TextShape::BoundingBox(Point &bottomLeft, Point &topRight) const
{
int x, y, width, height = 0;
m_pTextView->GetOrigin(x, y);
m_pTextView->GetExtent(width, height);
bottomLeft = Point(x, y);
topRight = Point(x + width, y + height);
}
bool TextShape::IsEmpty() const
{
return m_pTextView->IsEmpty();
}
Manipulator *TextShape::CreateManipulator()
{
return new TextManipulator(this);
}
適用性:
- 你想使用一個(gè)已經(jīng)存在的類(lèi)浪谴,而它的接口不符合你的要求(TextView)
- 你想使用一些已經(jīng)存在的子類(lèi),但是不可能對(duì)每一個(gè)都進(jìn)行子類(lèi)化以匹配它們的接口因苹,對(duì)象適配器模式可以適配它們的父類(lèi)接口
選擇:
選擇類(lèi)適配器與對(duì)象適配器有不同的權(quán)衡苟耻。
類(lèi)適配器
- 使得Adapter可以重定義Adaptee的部分行為,因?yàn)锳dapter是Adaptee的一個(gè)子類(lèi)(多繼承)扶檐。
- 不需要額外的指針以間接得到Adaptee凶杖。
對(duì)象適配器
- 允許一個(gè)Adapter與多個(gè)Adaptee--即Adaptee本身以及它的所有子類(lèi)(如果有的話)一同工作。也可以一次給所有的Adaptee添加功能款筑。
應(yīng)用:
1.在C++ STL中廣泛使用了Adapter模式智蝠,主要有container adapter、iterator adapter奈梳、functor adapter:
*container adapter: stack, queue(數(shù)據(jù)結(jié)構(gòu))
*iterator adapter: front_insert_iterator, back_insert_iterator, istream_iteator, ostream_iterator
*functor adapter: bind, negate, compose (與對(duì)一般函數(shù)或者成員函數(shù)的修飾)
2.Android ListView中Adapter模式
等等