#include <iostream>
using namespace std; //For brevity.
//Implementor
class DrawingAPI
{
public:
virtual void DrawCircle(const double x, const double y, const double radius) = 0;
virtual void DrawSquare(const double x, const double y) = 0;
};
// ConcreteImplementor 1/2
class OpenGL : public DrawingAPI
{
public:
void DrawCircle(const double x, const double y, const double radius)
{
cout<<"OpenGL based circle drawn at "<<x<<":"<<y<<" radius "<<radius<<endl;
}
void DrawSquare(const double x, const double y)
{
cout<<"OpenGL based square drawn at "<<x<<":"<<y<<endl;
}
};
// ConcreteImplementor 2/2
class DirectX : public DrawingAPI
{
public:
void DrawCircle(const double x, const double y, const double radius)
{
cout<<"DirectX based circle drawn at "<<x<<":"<<y<<" radius "<<radius<<endl;
}
void DrawSquare(const double x, const double y)
{
cout<<"DirectX based square drawn at "<<x<<":"<<y<<endl;
}
};
//Abstraction"
class Shape
{
public: //Constrctor
Shape(double x, double y, double radius, DrawingAPI* da) :
_x(x), _y(y), _radius(radius), _da(da)
{ }
Shape(double x, double y, DrawingAPI* da) :
_x(x), _y(y), _da(da)
{ }
public:
// low-level
virtual void Draw() = 0;
protected:
void DrawCircle()
{
_da->DrawCircle(_x, _y, _radius);
}
void DrawSquare()
{
_da->DrawSquare(_x, _y);
}
// high-level
// This needs to be refined in the Refined Abstraction classes.
virtual void ResizeByPercentage(double const pct) = 0;
double _x, _y, _radius;
DrawingAPI* _da; // Shapes could be displayed by different DrawingAPI's.
};
//Refined Abstraction: define a Circle.
class Circle : public Shape
{
public : //Constructor
Circle(double x, double y, double radius, DrawingAPI* da) :
Shape(x, y, radius, da)
{ }
public: //Behavior
// low-level i.e. Implementation specific.
// Draw doesn't even know what parameters it's using and WHO is drawing it.
void Draw() { DrawCircle(); }
// High-level i.e. Abstraction specific.
// A circle has a radius, but a square has a length (for example).
void ResizeByPercentage(const double pct) { _radius *= pct; }
};
//Refined Abstraction: define a Square.
class Square : public Shape
{
public : //Constructor
Square(double x, double y, DrawingAPI* da) :
Shape(x, y, da)
{ }
public: //Behavior
// Low-level i.e. Implementation specific.
// Draw doesn't even know what parameters it's using and WHO is drawing it.
void Draw() { DrawSquare(); }
// High-level i.e. Abstraction specific.
// A circle has a radius, but a square has a length (for example).
void ResizeByPercentage(const double pct) { _x *= pct; }
};
//The Client.
// -->> int main() merely demonstrates the interchangebility.
int main()
{
Shape* TheShape[4]; //2 shapes x 2 implementations = 4 client-options.
DrawingAPI* DA[2]; //2 Implementations.
DA[0] = new OpenGL;
DA[1] = new DirectX;
TheShape[0] = new Circle(1, 2, 7.5, DA[0]); //Draw circle with OpenGL
TheShape[0]->Draw();
TheShape[1] = new Circle(5, 7, 27.5, DA[1]); //Draw circle with DirectX
TheShape[1]->Draw();
TheShape[2] = new Square(10, 14, DA[0]); //Draw square with OpenGL
TheShape[2]->Draw();
TheShape[3] = new Square(2.9, 80, DA[1]); //Draw square with DirectX
TheShape[3]->Draw();
//Give back allocated memory.
delete DA[0], DA[1];
delete TheShape[0], TheShape[1], TheShape[2], TheShape[3];
return 0;
}
Pytanie :
Skad void Draw() { DrawCircle(); } wie ze musi wywolac
void DrawCircle()
{
_da->DrawCircle(_x, _y, _radius);
}
?
Na liscie inicjalizacyjnej Circle jest Shape tworzony, potrafi mi to ktoś wytłumaczyć ?