CSharp: Template Method Pattern
/// <summary> /// Summary description for Triangle. /// Template Method Pattern 模板方法模式 ///20220918 /// geovindu,Geovin Du,涂聚文 /// </summary> public abstract class Triangle { /// <summary> /// /// </summary> private Point p1, p2, p3; /// <summary> /// /// </summary> protected Pen pen; /// <summary> /// /// </summary> /// <param name="a"></param> /// <param name="b"></param> /// <param name="c"></param> public Triangle(Point a, Point b, Point c) { p1 = a; p2 = b; p3 = c; pen = new Pen(Color.Black, 1); } /// <summary> /// /// </summary> /// <param name="g"></param> public virtual void draw(Graphics g) { g.DrawLine(pen, p1, p2); Point c = draw2ndLine(g, p2, p3); closeTriangle(g, c); } /// <summary> /// /// </summary> /// <param name="g"></param> /// <param name="a"></param> /// <param name="b"></param> /// <returns></returns> public abstract Point draw2ndLine(Graphics g, Point a, Point b); /// <summary> /// /// </summary> /// <param name="g"></param> /// <param name="c"></param> public void closeTriangle(Graphics g, Point c) { g.DrawLine(pen, c, p1); } }
/// <summary> /// Summary description for StdTriangle. /// Template Method Pattern 模板方法模式 ///20220918 /// geovindu,Geovin Du,涂聚文 /// </summary> public class StdTriangle : Triangle { /// <summary> /// /// </summary> /// <param name="a"></param> /// <param name="b"></param> /// <param name="c"></param> public StdTriangle(Point a, Point b, Point c) : base(a, b, c) { } /// <summary> /// /// </summary> /// <param name="g"></param> /// <param name="a"></param> /// <param name="b"></param> /// <returns></returns> public override Point draw2ndLine(Graphics g, Point a, Point b) { g.DrawLine(pen, a, b); return b; } }
/// <summary> /// Summary description for IsocelesTriangle. /// Template Method Pattern 模板方法模式 ///20220918 /// geovindu,Geovin Du,涂聚文 /// </summary> public class IsocelesTriangle : Triangle { private Point newc; private int newcx, newcy; /// <summary> /// /// </summary> /// <param name="a"></param> /// <param name="b"></param> /// <param name="c"></param> public IsocelesTriangle(Point a, Point b, Point c) : base(a, b, c) { float dx1, dy1, dx2, dy2, side1, side2; float slope, intercept; int incr; dx1 = b.X - a.X; dy1 = b.Y - a.Y; dx2 = c.X - b.X; dy2 = c.Y - b.Y; side1 = calcSide(dx1, dy1); side2 = calcSide(dx2, dy2); if (side2 < side1) incr = -1; else incr = 1; slope = dy2 / dx2; intercept = c.Y - slope * c.X; //move point c so that this is an isoceles triangle newcx = c.X; newcy = c.Y; while (Math.Abs(side1 - side2) > 1) { newcx = newcx + incr; //iterate a pixel at a time until close newcy = (int)(slope * newcx + intercept); dx2 = newcx - b.X; dy2 = newcy - b.Y; side2 = calcSide(dx2, dy2); } newc = new Point(newcx, newcy); } /// <summary> /// /// </summary> /// <param name="a"></param> /// <param name="b"></param> /// <returns></returns> private float calcSide(float a, float b) { return (float)Math.Sqrt(a * a + b * b); } /// <summary> /// /// </summary> /// <param name="g"></param> /// <param name="b"></param> /// <param name="c"></param> /// <returns></returns> public override Point draw2ndLine(Graphics g, Point b, Point c) { g.DrawLine(pen, b, newc); return newc; } }
调用:
/// <summary> /// Template Method Pattern 模板方法模式 ///20220918 /// geovindu,Geovin Du,涂聚文 /// </summary> public partial class TemplateMethodPatternForm : Form { private ArrayList triangles; /// <summary> /// /// </summary> private void init() { triangles = new ArrayList(); StdTriangle t1 = new StdTriangle(new Point(10, 10), new Point(150, 50), new Point(100, 75)); IsocelesTriangle t2 = new IsocelesTriangle( new Point(150, 100), new Point(240, 40), new Point(175, 150)); triangles.Add(t1); triangles.Add(t2); Pic.Paint += new PaintEventHandler(TPaint); } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void TPaint(object sender, System.Windows.Forms.PaintEventArgs e) { Graphics g = e.Graphics; for (int i = 0; i < triangles.Count; i++) { Triangle t = (Triangle)triangles[i]; t.draw(g); } } /// <summary> /// /// </summary> public TemplateMethodPatternForm() { InitializeComponent(); init(); } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void TemplateMethodPatternForm_Load(object sender, EventArgs e) { } }
输出:
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)