~cpp
//CPPStudy_2005_1/Canvas 
//050829
#include <list>
#include <iostream>
#include <vector>
using namespace std;
class Shape{
public:
	virtual void Draw(){};
	virtual void Add(Shape *pObj){};
	virtual void Remove(Shape *pObj){};
};


class Triangle : public Shape{
public:
	void Draw()
	{
		cout << "Triangle" << "\t";
	}
};

class Rectangle : public Shape{
public:
	void Draw()
	{
		cout << "Rectangle" << "\t";
	}
};

class Circle : public Shape{
public:
	void Draw()
	{
		cout << "Circle" << "\t";
	}
};

class CompositeShape : public Shape{
private:
	list<Shape*> m_shape;
public:
	void Draw() {
		for (list<Shape*>::iterator it=m_shape.begin();it!=m_shape.end();it++){
			(*it)->Draw();
		}
	}

	void Add(Shape *pObj) {
		m_shape.push_front(pObj);
	}
	void Remove(Shape *pObj) {
		for (list<Shape*>::iterator it=m_shape.begin(); it!=m_shape.end();it++){
			if (pObj==*it) m_shape.erase(it);
		}		
	}

};


class Palette{
private:
	vector<Shape*> canvas;
public:
	void addNewShape(Shape *pObj)
	{
		canvas.push_back(pObj);
	}
	void Draw()
	{
		for(vector<Shape*>::iterator it=canvas.begin(); it!=canvas.end();++it)
			(*it)->Draw();
	}

};

void main()
{
	Triangle aTriangle;
	Rectangle aRectangle;
	CompositeShape aCompositeShape2;

	aCompositeShape2.Add(&aTriangle);
	aCompositeShape2.Add(&aRectangle);

	Circle aCircle;
	Rectangle aRectangle2;
	CompositeShape aCompositeShape;

	aCompositeShape.Add(&aCompositeShape2);
	aCompositeShape.Add(&aCircle);
	aCompositeShape.Add(&aRectangle2);

	Palette aPalette;
	aPalette.addNewShape(&aCompositeShape);
	aPalette.Draw();


}
Retrieved from http://wiki.zeropage.org/wiki.php/canvas
last modified 2021-02-07 05:31:37