C++ノート--テンプレート類Traitsの使い方検討
3786 ワード
要件:Rectangle、Circle、Polygonなど、異なるグラフィックを描くペイントエンジンを設計します.
目標:ペイントエンジンは開拓性があり、メンテナンスしやすいなどの特徴がある.
考え方:異なるグラフィックを描く基本的な流れは同じで、まずグラフィックデータを特定のコンテナにコピーする必要があります.各グラフィッククラスはペイントを提供し、各タイプのペイント方法を呼び出し、データを転送します.描画したデータは特定のコンテナ内にあり、表示方法は同じです(グラフィックスカードのドライバを呼び出し、データをコピーすればいいです)
RectangleTraits.h
CircleTraits.h
PolygonTraits.h
main.cpp
目標:ペイントエンジンは開拓性があり、メンテナンスしやすいなどの特徴がある.
考え方:異なるグラフィックを描く基本的な流れは同じで、まずグラフィックデータを特定のコンテナにコピーする必要があります.各グラフィッククラスはペイントを提供し、各タイプのペイント方法を呼び出し、データを転送します.描画したデータは特定のコンテナ内にあり、表示方法は同じです(グラフィックスカードのドライバを呼び出し、データをコピーすればいいです)
RectangleTraits.h
#include
class RectangleType
{
public:
RectangleType()
{
}
static void GetDrawingType()
{
std::cout << "This a Renctangle!
";
}
static RectangleType Clone()
{
return RectangleType();
}
static void PrepareData(char* ch )
{
std::cout << "Prepare Data for Rectangle Done!" << ch << "
";
}
};
class RectangleDrawer
{
public:
static int Drawing()
{
std::cout << "Drawing a Renctangle!
";
return 1;
}
};
class RectangleTraits
{
public:
typedef RectangleType shape_type;
typedef RectangleDrawer shape_drawer;
};
CircleTraits.h
#include
class CircleType
{
public:
CircleType()
{
}
static void GetDrawingType()
{
std::cout << "This a Circle!
";
}
static CircleType Clone()
{
return CircleType();
}
static void PrepareData(char* ch)
{
std::cout << "Prepare Data for Circle Done!"<< ch << "
";
}
};
class CircleDrawer
{
public:
static int Drawing()
{
std::cout << "Drawing a Circle!
";
return 2;
}
};
class CircleTraits
{
public:
typedef CircleType shape_type;
typedef CircleDrawer shape_drawer;
};
PolygonTraits.h
#include
class PolygonType
{
public:
PolygonType()
{
}
static void GetDrawingType()
{
std::cout << "This a Polygon!
";
}
static PolygonType Clone()
{
return PolygonType();
}
static void PrepareData(char* ch)
{
std::cout << "Prepare Data for Polygon Done!" << ch << "
";
}
};
class PolygonDrawer
{
public:
static int Drawing()
{
std::cout << "Drawing a Polygon!
";
return 0;
}
};
class PolygonTraits
{
public:
typedef PolygonType shape_type;
typedef PolygonDrawer shape_drawer;
};
main.cpp
#include
#include "RectangleTraits.h"
#include "CircleTraits.h"
#include "PolygonTraits.h"
template
class DrawShape
{
public:
typedef typename ShapeTraits::shape_type ShapeType;//
typedef typename ShapeTraits::shape_drawer ShapeDrawer;//
static void Drawing()
{
// pearperData
char ch[] = "Yes";
ShapeType::PrepareData(ch);
// drawing
int ret = ShapeDrawer::Drawing();
// sucessful
std::cout << ret << "
";
// display
}
};
class Shape
{
public:
void Draw()
{
OnDraw();
}
protected:
virtual void OnDraw() = 0;
};
class RenctangleShape : public Shape
{
protected:
void OnDraw()
{
DrawShape::Drawing();
}
};
class CircleShape : public Shape
{
protected:
void OnDraw()
{
DrawShape::Drawing();
}
};
class PolygonShape : public Shape
{
protected:
void OnDraw()
{
DrawShape::Drawing();
}
};
enum ShapeType
{
ShapeType_Rectangle = 0,
ShapeType_Circle = 1,
ShapeType_Polygon = 2,
ShapeType_Count = ShapeType_Polygon + 1,
};
int main()
{
Shape* sh[ShapeType_Count - 1];
sh[ShapeType_Rectangle] = new RenctangleShape();
sh[ShapeType_Circle] = new CircleShape();
sh[ShapeType_Polygon] = new PolygonShape();
for (int i = 0; i < ShapeType_Count; ++i)
{
sh[i]->Draw();
}
return 0;
}