U E D R , A S I H C RSS

Gof/Adapter


1. Adapter

1.1. Intent

ด๋ž˜Šค˜ „Ž˜Šค๋ฅผ ๋‹ค๋ฅธ •„š”•œ ด๋ž˜Šค˜ „Ž˜Šค— ๋งžฒŒ ๋ณ€™˜•ค€๋‹ค. Adapter ๋Š” „œ๋กœ ˜™˜„—†๋Š” „Ž˜Šค๋“ค๋ผ๋ฆฌ๋ผ๋„ ฐ™ž‘๋™•  ˆ˜ žˆฒŒ๋” •ค€๋‹ค.

1.2. Also Known As

Wrapper (˜žˆ๋ ด ๋‹–ดฐ€ ๋” œˆ™•  ฒƒด๋‹ค.)

1.4. Applicability

‹Œณผ ฐ™€ ฒฝšฐ— AdapterPattern๋ฅผ šฉ•œ‹ค.
  • ด๋ฏธ ๋งŒ๋“ค–ด žˆ๋Š” ด๋ž˜Šค๋ฅผ ‚ฌšฉ•˜  ‹ง€๋งŒ, „Ž˜Šคฐ€ ›•˜๋Š” ๋ฐฉ‹ณผ ˜•˜ง€ •Š„•Œ.
  • ด€๋ „—†ฑฐ๋‚˜, ˜ˆธก•˜ง€ ๋ชป•œ ด๋ž˜Šค๋“คณผ ˜‘๋™•˜๋Š” žฌ‚ฌšฉฐ€๋Šฅ•œ ด๋ž˜Šค๋ฅผ ƒ„•˜ธฐ ›• •Œ. ฒฝšฐ ด๋ž˜Šค๋“ค˜™˜„„ ฐ€ง€๋Š” „Ž˜Šค๋ฅผ •„š”๋กœ •˜ง€ •Š๋Š”๋‹ค.
  • (object adapter ˜ ฒฝšฐ—๋งŒ •ด๋‹น) ˜„žฌ ด๋ฏธ ๋งŒ๋“ค–ด„ —ฌ๋Ÿฌœ˜ subclassฐ€ •„š”•œ ฒฝšฐ, •˜ง€๋งŒ ฐฐ˜ „œ๋ธŒด๋ž˜Šค๋“ค— ๋Œ€•œ „Ž˜Šค๋ฅผ •˜๋Š” ฒƒ€ ๋น„šจœ ด๋‹ค. ฒฝšฐ parent class˜ „Ž˜Šค๋ฅผ adapt •  ˆ˜ žˆ๋‹ค.

1.5. Structure

adapter ด๋ž˜Šค๋Š” •˜๋‚˜˜ interface๋ฅผ ๋‹ค๋ฅธ interface —  ••˜ฒŒ ๋งžถฐฃผธฐ œ„•ด (๋ง ธ๋Œ€๋กœ –ด๋ށ„—ญ• ~) ๋‹‘ƒ†„ šฉ•œ‹ค.


adapter ฒด๋Š” object composition — ˜กด•œ‹ค.

1.6. Participants

  • Target (Shape)
    - ด๋–ธŠธฐ€ šฉ•œ domain-specific•œ „Ž˜Šค๋ฅผ  •˜•œ‹ค.
  • Client (DrawingEditor)
    - Target˜ „Ž˜Šค— ๋”ฐ๋ฒด๋“คณผ ƒ˜ž‘šฉ„ •œ‹ค.
  • Adaptee (TextView)
    - adapting•„š”•œ ˜„กด•˜๋Š” interface๋ฅผ  •˜•œ‹ค.
  • Adapter (TextShape)
    - Adpatee ˜ „Ž˜Šค๋ฅผ Target ˜ „Ž˜Šค— adapt ‹œ‚จ๋‹ค.

1.7. Collaborations

  • •ด๋‹ด๋ž˜Šค๋ฅผ šฉ•˜๋Š” Client๋“ค€ Adapter Šค„Šค˜ operation๋“ค„ ˜œ•œ‹ค. adapter๋Š” •ด๋‹น Client˜ š”ฒญ„ ˆ˜–‰•˜ธฐ œ„•ด Adaptee ˜ operation„ ˜œ•œ‹ค.

1.9. Implementation



~cpp 
directoryDisplay :=
	(TreeDisplay on: treeRoot)
		getChildrenBlock:
			[:node | node getSubdirectories]
		createGraphicNodeBlock:
			[:node | node createGraphicNode].

1.10. Sample Code

We'll give a brief sketch of the implementation of class and object adapters for the Motivation example beginning with the classes Shape and TextView.
~cpp 
class Shape {
public:
	Shape ();
	virtual void BoundingBox(Point& bottomLeft, Point& topRight) const;
	virtual Manipulator* CreateManipulator () const;
};

class TextView {
public:
	TextView ();
	void GetOrigin (Coord& x, Coord& y) const;
	void GetExtent (Coord& width, Coord& height) const;
	virtual bool IsEmpty () const;

Shape assumes a bounding box defined by its opposing corners. In contrast, TextView is defined by an origin, height, and width. Shape also defines a CreateManipulator operation for creating a Manipulator object, which knowns how to animate a shape when the user manipulates it. TextView has no equivalent operation. The class TextShape is an adapter between these different interfaces.

A class adapter uses multiple inheritance to adapt interfaces. The key to class dapters is to use one inheritance branch to inherit the interface and another branch to inherit the implementation. The usual way to make this distinction in C++ is to inherit the interface publicly and inherit the implementation privately. We'll use this convention to define the TextShape adapter.
~cpp 
class TextShape : public Shape, private TextView {
public:
	TextShape ();

	virtual void BoundingBox (Point& bottomLeft, Point& topRight) const;
	virtual bool IsEmpty () const;
	virtual Manipulator* CreateManipulator () const;
};

The BoundingBox operation converts TextView's interface to conform to Shape's.
~cpp 
void TextShape::boundingBox (Point& bottomLeft, Point& topRight) const {
	Coord bottom, left, width, height;

	GetOrigin (bottom, left);
	GetExtent (width, height);

	bottomLeft = Point (bottom, left);
	topRight = Point (bottom + height, left + width);

The IsEmpty operations demonstrates the direct forwarding of requests common in adapter implementations:
~cpp 
bool TextShape::ImEmpty () const {
	return TextView::IsEmpty ();
}

Finally, we define CreateManipulator (which isn't supported by TextView) from scratch. Assume we've already implemented a TextManipulator class that supports manipulation of a TextShape.
~cpp 
Manipulator* TextShape::CreateManipulator () const {
	return new TextManipulator (this);

The object adapter uses object composition to combine classes with different interfaces. In this approach, the adapter TextShape maintains a pointer to TextView.
~cpp 
class TextShape : public Shape {
public:
	TextShape (TextView*);

	virtual void BoundingBox (Point& bottomLeft, Point& topRight) const;

	virtual bool IsEmpty () const;
	virtual Manipulator* CreateManipulator () const;
private:
	TextView* _text;
};

TextShape must initialize the pointer to the TextView instance, and it does so in the constructor. It must also call operations on its TextView object whenever its own operations are called. In this example, assume that the client creates the TextView object and passes it to the TextShape constructor.
~cpp 
TextShape::TextShape (TextView* t) {
	_text = t;
}

void TextShape::BoundingBox (
	Point& bottomLeft, Point& topRight
) const {
	Coord bottom, left, width, height;

	_text->GetOrigin (bottom, left);
	_text->GetExtent (width, height);

	bottomLeft = Point (bottom, left);
	topRight = Point (bottom + height, left + width);
}

bool TextShape::IsEmpty () const {
	return _text->IsEmpty ();
}

CreateManipulator's implementation doesn't change from the class adapter version, since it's implemented from scratch and doesn't reuse any existing TextView functionality.

~cpp 
Manipulator* TextShape::CreateManipulator () const {
	return new TextManipulator (this);
}

Compare this code the class adapter case. The object adapter requires a little more effort to write, but it's more flexible. For example, the object adapter version of TextShape will work equally well with subclasses of TextView -- the client simply passes an instance of a TextView subclass to the TextShape constructor.

1.12. Related Patterns

BridgePattern € adapter object™€ ๋น„Š•œ ตฌกฐ๋ฅผ ฐ€„‹ค. •˜ง€๋งŒ BridgePattern˜ AdapterPatternณผ ˜๋„ฐ€ ๋‹ค๋ฅด๋‹ค. BridgePattern€ ‹ œ ตฌ˜„๋ถ€™€ interface๋ถ€๋ถ„„ ๋ถ„๋ฆฌ‹œœ ‹ œ ตฌ˜„ ๋ถ€๋ถ„ด ๋‹–‘•˜  ๋…๋ฆฝ ˆ˜ žˆ๋„๋ก •˜ธฐ œ„•œ ฒƒด๋‹ค. adapter๋Š” ˜„žฌ ด๋ฏธ กดžฌ•˜๋Š” ฒด— ๋Œ€•œ interface๋ฅผ ๋ฐ”พธธฐ œ„•šฉ๋œ‹ค.

DecoratorPattern€ ฒด— ๋Œ€•œ „Ž˜Šค˜ ๋ณ€™”—†ฒด๋ฅผ ™•žฅ‹œ‚จ๋‹ค. Decorator ๋Š” adapter๋ณด๋‹ค ๋” application— ๋Œ€•ˆฌ๋ช… ด๋‹ค. ฒฐ๋ก  œผ๋กœ DecoratorPattern€ žฌ€ ธ composition„  œณต•œ‹ค. ฒƒ€ ˆœˆ˜•œ adapter๋กœ„œ๋Š” ๋ถˆฐ€๋Šฅ•˜‹ค.

ProxyPattern€ •ด๋‹ฒด— ๋Œ€•œ ๋Œ€๋ฆฌž —ญ• „ •˜๋ฉฐ, ‹ œ ฒด— ๋Œ€•œ interface๋ฅผ ๋ณ€™”‹œ‚คง€ •Š๋Š”๋‹ค.


Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:23:18
Processing time 0.0222 sec