使用策略模式可以减少分支语句,switch...Case,同时便于策略的扩展。
1. ITransform2D接口的Transform方法:
1 [C#]public void Transform (
2 esriTransformDirection direction,
3 ITransformation transformation);
大部分的Geometry对象都实现了ITransform接口,比如:IPoint,IPolygon的基类
ITransformation是策略的抽象接口,如下:
2. ITransform3D接口的Transform3D方法:
1 [C#]public void Transform3D (
2 esriTransformDirectiondirection,
3 ITransformation3Dtransformation);
ITransformation3D是策略的抽象接口,如下:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 private void RotateAroundPoint() 2 { 3 //Point to be rotated 4 IPoint rotatePoint = new ESRI.ArcGIS.Geometry.Point(); 5 rotatePoint.PutCoords(2, 2); 6 //Point around which to rotate 7 IPoint centerPoint = new ESRI.ArcGIS.Geometry.Point(); 8 centerPoint.PutCoords(1, 1); 9 //Rotation Angle 10 double angle = 45 * Math.PI / 180.0; 11 //Rotate Around pCenter 12 IAffineTransformation2D3GEN affineTransformation = new AffineTransformation2D() as IAffineTransformation2D3GEN; 13 affineTransformation.Move(-centerPoint.X, -centerPoint.Y);//将参考点移动到原点 14 affineTransformation.Rotate(angle);//围绕原点旋转 15 affineTransformation.Move(centerPoint.X, centerPoint.Y);//再平移回原来的位置 16 ITransform2D transformator = rotatePoint as ITransform2D; 17 transformator.Transform(esriTransformDirection.esriTransformForward, affineTransformation as ITransformation); 18 19 20 IPoint rotatePoint2 = new ESRI.ArcGIS.Geometry.Point(); 21 rotatePoint2.PutCoords(2, 2); 22 IAffineTransformation2D affineTransformation2D = new AffineTransformation2D() as IAffineTransformation2D; 23 // affineTransformation2D.Move(-centerPoint.X, -centerPoint.Y);//将参考点移动到原点 24 affineTransformation2D.Rotate(angle);//围绕原点旋转 25 //affineTransformation2D.Move(centerPoint.X, centerPoint.Y);//再平移回原来的位置 26 ITransform2D transformator2 = rotatePoint2 as ITransform2D; 27 transformator2.Transform(esriTransformDirection.esriTransformForward, affineTransformation2D); 28 29 //Set up Comparison Point 30 //This is the point the transformation should result in 31 IPoint comparePoint = new ESRI.ArcGIS.Geometry.Point(); 32 comparePoint.PutCoords(2, 2); 33 transformator = comparePoint as ITransform2D; 34 transformator.Rotate(centerPoint, angle); 35 System.Windows.Forms.MessageBox.Show( 36 "Using IAffineTransformation2D3GEN.Rotate: Point X:" + rotatePoint.X + ", Y:" + rotatePoint.Y + "\n" + 37 "Using IAffineTransformation2D::Rotate, Point X:" + rotatePoint2.X + ", Y:" + rotatePoint2.Y + "\n" + 38 "Using ITransform2D::Rotate, Point X: " + comparePoint.X + ", Y:" + comparePoint.Y + "\n" + 39 "Did X coordinates match? " + (rotatePoint.X == comparePoint.X) + "\n" + 40 "Did Y coordinates match? " + (rotatePoint.Y == comparePoint.Y) 41 ); 42 43 }