最常用的模式之一。
GOF:將抽象部分與它的實現部分分離,使它們都可以獨立地變化。
#include "createElement.h"
extern"C" DLLEXPORT int MdlMain
(
int argc,
char** argv
)
{
DPoint3d pts[] = {
{0,0,0},
{10000,0,0},
{10000,10000,0},
{0,10000,0}
};
Transform tr;
DPoint3d translate = {15000,0,0};
mdlTMatrix_getIdentity (&tr);
mdlTMatrix_setTranslation (&tr, &translate);
Drawing* draw1 = new v1Drawing;
Drawing* draw2 = new v2Drawing;
shape* r1 = new Rectangle (draw1, pts[0], pts[1], pts[2], pts[3]);
mdlTMatrix_transformPointArray (pts, &tr, 4);
shape* r2 = new Rectangle (draw2, pts[0], pts[1], pts[2], pts[3]);
r1->draw ( );
r2->draw ( );
delete r1;
delete r2;
shape* c1 = new Circle (draw1, pts[0], 5000);
shape* c2 = new Circle (draw2, pts[1], 5000);
c1->draw ( );
c2->draw ( );
delete c1;
delete c2;
delete draw1;
delete draw2;
return EXIT_SUCCESS;
}
H文件
#ifndef CREATEELEMENT_H
#define CREATEELEMENT_H
#include <MicroStationAPI.h>
class ElementDraw1
{
public:
static void drawline (const DPoint3d orig, const DPoint3d& end);
static void drawCircle (const DPoint3d& center, double radius);
};
class ElementDraw2
{
public:
static void draw_a_line (const DPoint3d orig, const DPoint3d& end);
static void draw_a_Circle (const DPoint3d& center, double radius);
};
//
class Drawing
{
public:
virtual void drawLine (const DPoint3d& orig, const DPoint3d& end) = 0;
virtual void drawCircle (const DPoint3d& center, double radius) = 0;
};
class v1Drawing : public Drawing
{
public:
virtual void drawLine (const DPoint3d& orig, const DPoint3d& end);
virtual void drawCircle (const DPoint3d& center, double radius);
};
class v2Drawing : public Drawing
{
public:
virtual void drawLine (const DPoint3d& orig, const DPoint3d& end);
virtual void drawCircle (const DPoint3d& center, double radius);
};
//
class shape
{
Drawing* _draw;
public:
virtual void drawLine(const DPoint3d& orig,const DPoint3d& end )
{
_draw->drawLine (orig, end);
}
virtual void drawCircle(const DPoint3d& center,double radius)
{
_draw->drawCircle (center, radius);
}
virtual void draw ( ) = 0;
protected:
shape (Drawing* drawp) :_draw (drawp) {};
};
class Rectangle : public shape
{
DPoint3d pt [4];
public:
Rectangle (Drawing* d,
const DPoint3d& pt0,
const DPoint3d& pt1,
const DPoint3d& pt2,
const DPoint3d& pt3) : shape (d)
{
pt[0] = pt0;
pt[1] = pt1;
pt[2] = pt2;
pt[3] = pt3;
};
virtual void draw ( );
};
class Circle : public shape
{
DPoint3d m_orig;
double m_radius;
public:
Circle (Drawing* d,const DPoint3d orig,double radius) :
shape (d),
m_orig(orig),
m_radius(radius) {};
virtual void draw ( );
};
#endif // CREATEELEMENT_H
cpp
#include "createElement.h"
inline void _drawLine (const DPoint3d& orig, const DPoint3d& end, UInt32* color, Int32* sty)
{
MSElement li;
DPoint3d pts [] = { orig, end };
mdlLine_create (&li, NULL, pts);
mdlElement_setSymbology (&li, color, 0, sty);
mdlElement_add (&li);
}
inline void _drawCircle(const DPoint3d& center,double radius,UInt32* color,Int32* sty)
{
MSElement arc;
mdlArc_create (&arc, NULL, (DPoint3d*) ¢er, radius, radius, NULL, 0, fc_2pi);
mdlElement_setSymbology (&arc, color, 0, sty);
mdlElement_add (&arc);
}
void ElementDraw1::drawline (const DPoint3d orig, const DPoint3d& end)
{
UInt32 color = 2;
Int32 sty = 1;
_drawLine (orig, end,&color,&sty);
}
void ElementDraw1::drawCircle (const DPoint3d& center, double radius)
{
UInt32 color = 2;
Int32 sty = 1;
_drawCircle (center, radius, &color, &sty);
}
void ElementDraw2::draw_a_line (const DPoint3d orig, const DPoint3d& end)
{
UInt32 color = 1;
Int32 sty = 2;
_drawLine (orig, end, &color, &sty);
}
void ElementDraw2::draw_a_Circle (const DPoint3d& center, double radius)
{
UInt32 color = 1;
Int32 sty = 2;
_drawCircle(center,radius,&color,&sty);
}
void v1Drawing::drawLine (const DPoint3d& orig, const DPoint3d& end)
{
ElementDraw1::drawline (orig, end);
}
void v1Drawing::drawCircle (const DPoint3d& center, double radius)
{
ElementDraw1::drawCircle (center, radius);
}
void v2Drawing::drawLine (const DPoint3d& orig, const DPoint3d& end)
{
ElementDraw2::draw_a_line (orig, end);
}
void v2Drawing::drawCircle (const DPoint3d& center, double radius)
{
ElementDraw2::draw_a_Circle (center, radius);
}
void Rectangle::draw ( )
{
drawLine (pt[0], pt[1]);
drawLine (pt[1], pt[2]);
drawLine (pt[2], pt[3]);
drawLine (pt[3], pt[0]);
}
void Circle::draw ( )
{
drawCircle (m_orig, m_radius);
}