结构图:设计模式中结构型模式

结构型模式涉及到如何组合类和对象以获得更大的结构。
结构型对象模式不是对接口和实现进行组合,而是描述了如何对一些对象进行组合,从而
实现新功能的一些方法。因为可以在运行时刻改变对象组合关系,所以对象组合方式具有
更大的灵活性,而这种机制用静态类组合是不可能实现的。
定义: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
设计模式中结构型模式

设计模式中结构型模式typedef
floatCoord;
设计模式中结构型模式设计模式中结构型模式
classPoint...{
设计模式中结构型模式
public:
设计模式中结构型模式
staticconstPointZero;
设计模式中结构型模式
设计模式中结构型模式Point(Coordx
=0.0,Coordy=0.0);
设计模式中结构型模式
设计模式中结构型模式CoordX()
const;//得到x值
设计模式中结构型模式
CoordY()const;
设计模式中结构型模式
voidX(Coordx);//设置x值
设计模式中结构型模式
voidY(Coordy);
设计模式中结构型模式
设计模式中结构型模式Point
operator+(constPoint&);
设计模式中结构型模式Point
operator-(constPoint&);
设计模式中结构型模式Point
operator*(constPoint&);
设计模式中结构型模式Point
operator/(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

相关文章:

  • 2021-09-16
  • 2022-02-10
猜你喜欢
  • 2021-07-10
  • 2022-02-10
  • 2021-12-12
  • 2021-07-08
  • 2021-11-04
  • 2021-06-05
  • 2021-08-25
相关资源
相似解决方案