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;
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值