结构图:
结构型模式涉及到如何组合类和对象以获得更大的结构。
结构型对象模式不是对接口和实现进行组合,而是描述了如何对一些对象进行组合,从而
实现新功能的一些方法。因为可以在运行时刻改变对象组合关系,所以对象组合方式具有
更大的灵活性,而这种机制用静态类组合是不可能实现的。
定义:Adapter:将一个类的接口转换成客户希望的另外一个接口。Adapter模式使得原本
由于接口不兼容而不能一起工作的那些类可以一起工作。
尽管Adapter模式的实现方式通常简单直接,但是仍需要注意以下一些问题:
在使用C++实现适配器类时,Adapter类应该采用公共方式继承Target类,并且用私有方式
继承Adaptee类。因此,Adapter类应该是Target的子类型,但不是Adaptee的子类型。
适配器模式有两种方法实现,一种是类适配器,一种是对象适配器。
类适配器和对象适配器有不同的权衡。类适配器
1、用一个具体的Adapter类对Adaptee和Target进行匹配。结果是当我们想要匹配生个类以
及所有它的子类时,类Adapter将不能胜任工作。
2、使得Adapter可以重定义Adapter的部分行为,因为Adapter是Adaptee的一个子类。
3、仅仅引入了一个对象,并不需要额外的指针以间接得到adaptee.
对象适配器则
1、允许一个Adapter与多个Adaptee,即Adaptee本身以及它的所有子类(如果有子类的话)
同时工作。Adapter也可以一次给所有的Adaptee添加功能。
2、使得重定义Adaptee的行为比较困难。这就需要生成Adaptee的子类并且使得Adapter引用
这个子类而不是引用adaptee本身。
本章用两个实例分别讲述类适配器模式和对象适配器模式。
对于结构型模式,由于需要用到一些结构,因此这里先贴出来,部分只有申明无实现是因为还未被用到,用到时会逐步补上去的。
一、类适配器
//BasicClass.h:interfacefortheBasicClassclass.
//

/**///////////////////////////////////////////////////////////////////////
#if!defined(AFX_BASICCLASS_H__459C19CB_0047_4A50_855E_B7FDCFC6F3B0__INCLUDED_)
#defineAFX_BASICCLASS_H__459C19CB_0047_4A50_855E_B7FDCFC6F3B0__INCLUDED_

#if_MSC_VER>1000
#pragmaonce
#endif//_MSC_VER>1000
//
//#include<iostream>
//usingnamespacestd;
#include<iostream.h>
#defineDEFAULT_LIST_CAPACITY1000
template<classItem>classList

...{
public:
List(longsize=DEFAULT_LIST_CAPACITY);
List(List&);
~List();
List&operator=(constList&);

longCount()const;
Item&Get(longindex)const;
Item&First()const;
Item&Last()const;
boolIncludes(constItem&)const;

voidAppend(constItem&);
voidPrepend(constItem&);

voidRemove(constItem&);
voidRemoveLast();
voidRemoveFirst();
voidRemoveAll();

Item&Top()const;
voidPush(constItem&);
Item&Pop();
};

template<classItem>classiterator

...{
public:
virtualvoidFirst()=0;
virtualvoidNext()=0;
virtualboolIsDone()const=0;
virtualItemCurrentItem()const=0;
protected:
iterator();
};

template<classItem>classListIterator:publiciterator<Item>

...{
public:
ListIterator(constList<Item>*aList);

virtualvoidFirst();
virtualvoidNext();
virtualboolIsDone();
virtualItemCurrentItem()const;
};
//CPoint

typedeffloatCoord;

classPoint...{
public:
staticconstPointZero;

Point(Coordx=0.0,Coordy=0.0);

CoordX()const;//得到x值
CoordY()const;
voidX(Coordx);//设置x值
voidY(Coordy);

Pointoperator+(constPoint&);
Pointoperator-(constPoint&);
Pointoperator*(constPoint&);
Pointoperator/(constPoint&);

voidoperator+=(constPoint&);
voidoperator-=(constPoint&);
voidoperator*=(constPoint&);
voidoperator/=(constPoint&);

booloperator==(constPoint&);
booloperator!=(constPoint&);

friendostream&operator<<(ostream&,constPoint&);
friendistream&operator>>(istream&,Point&);
private:
Coordm_x;
Coordm_y;
};

classRect

...{
public:
staticconstRectZero;

Rect(Coordx,Coordy,Coordw,Coordh);
Rect(constPoint&origin,constPoint&extent);

CoordWidth()const;
voidWidth(Coord);
CoordHeight()const;
voidHeight(Coord);
CoordLeft()const;
voidLeft(Coord);
CoordBottom()const;
voidBottom(Coord);

Point&Origin()const;
voidOrigin(constPoint&);
Point&Extent()const;
voidExtent(constPoint&);

voidMoveTo(constPoint&);
voidMoveBy(constPoint&);

boolIsEmpty()const;
boolContains(constPoint&)const;
};





















#endif//!defined(AFX_BASICCLASS_H__459C19CB_0047_4A50_855E_B7FDCFC6F3B0__INCLUDED_)


//BasicClass.cpp:implementationoftheBasicClassclass.
//

/**///////////////////////////////////////////////////////////////////////
#include"stdafx.h"
#include"BasicClass.h"


Point::Point(Coordx/**//*=0.0*/,Coordy/**//*=0.0*/)

...{
m_x=x;
m_y=y;
}

CoordPoint::X()const

...{
returnm_x;
}

CoordPoint::Y()const

...{
returnm_y;
}

voidPoint::X(Coordx)

...{
m_x=x;
}

voidPoint::Y(Coordy)

...{
m_y=y;
}

PointPoint::operator+(constPoint&p)

...{
returnPoint(m_x+p.m_x,m_y+p.m_y);
}

PointPoint::operator-(constPoint&p)

...{
returnPoint(m_x-p.m_x,m_y-p.m_y);
}

PointPoint::operator*(constPoint&p)

...{
returnPoint(m_x*p.m_x,m_y*p.m_y);
}

PointPoint::operator/(constPoint&p)

...{
returnPoint(m_x/p.m_x,m_y/p.m_y);
}


voidPoint::operator+=(constPoint&p)

...{
m_x+=p.m_x;
m_y+=p.m_y;
}
//CPoint
voidPoint::operator-=(constPoint&p)

...{
m_x-=p.m_x;
m_y-=p.m_y;
}

voidPoint::operator*=(constPoint&p)

...{
m_x*=p.m_x;
m_y*=p.m_y;
}

voidPoint::operator/=(constPoint&p)

...{
m_x/=p.m_x;
m_y/=p.m_y;
}

boolPoint::operator==(constPoint&p)

...{
return(m_x==p.m_x&&m_y==p.m_y);
}

boolPoint::operator!=(const