C++笔记--模板类Traits的用法探讨

需求:设计一个绘制引擎,可以绘制不同的图形(比如:Rectangle、Circle、Polygon等)。

目标:绘制引擎具有拓展性,易维护等特点。

思路:绘制不同的图形的基本流程是一样的,首先需要将图形数据复制到特定的容器内,每个图形类提供了一个绘制器,调用每个类型的绘制方法,传入数据。绘制后的数据在特定的容器内,并且显示的方法都是一样(调用显卡的驱动程序,把数据复制进去即可)

RectangleTraits.h

#include <iostream>
class RectangleType
{
public:
	RectangleType()
	{

	}
	static void GetDrawingType()
	{
		std::cout << "This a Renctangle!\n";
	}
	static RectangleType Clone()
	{
		return RectangleType();
	}
	static void PrepareData(char* ch )
	{
		std::cout << "Prepare Data  for Rectangle Done!" << ch << "\n";
	}
};

class RectangleDrawer
{
public:
	static int Drawing()
	{
		std::cout << "Drawing a Renctangle!\n";
		return 1;
	}
};

class RectangleTraits
{
public:
	typedef RectangleType shape_type;
	typedef RectangleDrawer shape_drawer;
};

CircleTraits.h

#include <iostream>
class CircleType
{
public:
	CircleType()
	{

	}
	static void GetDrawingType()
	{
		std::cout << "This a Circle!\n";
	}
	static CircleType Clone()
	{
		return CircleType();
	}
	static void PrepareData(char* ch)
	{
		std::cout << "Prepare Data  for Circle Done!"<< ch << "\n";
	}
};

class CircleDrawer
{
public:
	static int Drawing()
	{
		std::cout << "Drawing a Circle!\n";
		return 2;
	}
};

class CircleTraits
{
public:
	typedef CircleType shape_type;
	typedef CircleDrawer shape_drawer;
};

 

PolygonTraits.h

#include <iostream>
class PolygonType
{
public:
	PolygonType()
	{

	}
	static void GetDrawingType()
	{
		std::cout << "This a Polygon!\n";
	}
	static PolygonType Clone()
	{
		return PolygonType();
	}
	static void PrepareData(char* ch)
	{
		std::cout << "Prepare Data  for Polygon Done!" << ch << "\n";
	}
};

class PolygonDrawer
{
public:
	static int Drawing()
	{
		std::cout << "Drawing a Polygon!\n";
		return 0;
	}
};

class PolygonTraits
{
public:
	typedef PolygonType shape_type;
	typedef PolygonDrawer shape_drawer;
};

main.cpp

#include <iostream>
#include "RectangleTraits.h"
#include "CircleTraits.h"
#include "PolygonTraits.h"
template <class ShapeTraits>
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 << "\n";
        // display
	}
};



class Shape
{
public:
	void Draw()
	{
		OnDraw();
	}
protected:
	virtual void OnDraw() = 0;
};

class RenctangleShape : public Shape
{
protected:
	void OnDraw()
	{
		DrawShape<RectangleTraits>::Drawing();
	}
};

class CircleShape : public Shape
{
protected:
	void OnDraw()
	{
		DrawShape<CircleTraits>::Drawing();
	}
};


class PolygonShape : public Shape
{
protected:
	void OnDraw()
	{
		DrawShape<PolygonTraits>::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;
}

 

 

展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 技术黑板 设计师: CSDN官方博客
应支付0元
点击重新获取
扫码支付

支付成功即可阅读