150 likes | 405 Views
Engr 691 Special Topics in Engineering Science Software Architecture Spring Semester 2004 Lecture Notes. Template Method Pattern. This is a set of slides to accompany chapter 8 of Mark Grand’s book Patterns in Java : a catalog of reusable design patterns illustrated with UML
E N D
Engr 691Special Topics in Engineering ScienceSoftware ArchitectureSpring Semester 2004Lecture Notes
Template Method Pattern This is a set of slides to accompany chapter 8 of Mark Grand’s book Patterns in Java : a catalog of reusable design patterns illustrated with UML (John Wiley & Sons, 1998) Created: 19 August 2004
Context • Family of “algorithms” for some set of related tasks • Some logic is common to all algorithms • Some logic differs for each 1
General Approach • Represent family by abstract base class • Make common logic concrete methods of class – final • Make variable parts • Abstract methods • Concrete methods with default definitions • Some concrete methods call hook methods to do part of work • Subclass to define hook methods 2
AbstractTemplate templateMethod() … #operation1 #operation2 … A class in this role has a concrete method that contains the class’s top-level logic. ConcreteTemplate #operation1 #operation2 A class in this role overrides the abstract methods defined by its superclass to provide the logic needed to complete the logic of the templateMethod method. Solution 3
Triangle (from default) +draw +draw2ndLine +drawLine abstract method RightTriangle (from default) IsocelesTriangle (from default) stdTriangle (from default) Example 4
Example (cont.) public abstract class Triangle { private Point p1, p2, p3; public Triangle (Point a, Point b, Point c) { p1 = a; p2 = b; p3 = c; } public void draw (Graphics g) { // draws a general triangle drawLine (g, p1, p2); Point current = draw2ndLine(g, p2, p3); closeTriangle(g, current); } public void drawLine(Graphics g, Point a, Point b) { g.drawLine(a.x, a.y, b.x, b.y); } // hookmethod abstract public Point draw2ndLine(Graphics g, Point a, Point b); public void closeTriangle(Graphics g, Point c) { g. drawLine(c.x, c.y, p1.x, p1.y); } } 5
Example (cont.) public class stdTriangle extends Triangle { public stdTriangle(Point a, Point b, Point c) { super(a, b, c); } // define hook public Point draw2ndLine(Graphics g, Point a, Point b) { g. drawLine (a.x, a.y, b.x, b.y); return b; } } 6
Example (cont.) public class IsocelesTriangle extends Triangle { private Point newcl private int nexcx, newcy; private int incr; 7
Example (cont.) public IsocelesTriangle(Point a, Point b, Point c) { super(a, b, c); double dx1 = b.x – a.x; double dy1 = b.y – a.y; double dx2 = c.y – b.y; double dy2 = c.y – b.y; double side1 = calcSide(dx1, dy1); double side2 = calcSide(dx2, dy2); if (side2 < side1) incr = -1; else incr = 1; double slope = dy2 / dx2; double intercept = c.y – slope * c.x; newcx = c.x; newcy = c.y; while (Math.abs(side1 – side2) >1) { newcx += incr; newcy = (int)(slope * newcs + intercept); dx2 = newcx – b.x; dy2 = newcy – b.y; side2 = calcSide(dx2, dy2); } newc = new Point (newcx, newcy); } 8
Example (cont.) private double calcSide (double dx, double dy) { return Math.sqrt (dx*dx + dy*dy); } //define hook public Point draw2ndLine(Graphics g, Point b, Point c) { g. drawLine (b.x, b.y, newc.x, newc.y); return newc; } } 9
Acknowledgement This work was supported by a grant from Acxiom Corporation titled “The Acxiom Laboratory for Software Architecture and Component Engineering (ALSACE).” 10