Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Implement Internal Iterator pattern in Group Figure

introduce local interface FigureAction and a protected method traverse(FigureAction)
reimplement draw, getBounds, and move with internal iterator
  • Loading branch information...
commit 0384ed99146df1c9dbfbda9879906aed999507f3 1 parent 3d45941
Daniel James authored March 29, 2012
42  smallerdraw/src/uk/ac/ox/softeng/dpa/smallerdraw/figures/GroupFigure.java
@@ -59,6 +59,12 @@ public GroupFigure clone() {
59 59
 		return subFigures;
60 60
 	}
61 61
 	
  62
+	protected void traverse(FigureAction action) {
  63
+		for (Figure figure : subFigures) {
  64
+			action.execute(figure);
  65
+		}
  66
+	}
  67
+
62 68
 	/** 
63 69
 	 * Draw this group figure by drawing all of its subfigures,
64 70
 	 * and then draw the group figureÕs handle.
@@ -67,27 +73,36 @@ public GroupFigure clone() {
67 73
 	 * @see AbstractFigure#draw(Graphics2D)
68 74
 	 */
69 75
 	@Override
70  
-	public void draw(Graphics2D g) {
71  
-		for (Figure figure : subFigures) {
72  
-			figure.draw(g);
73  
-		}
  76
+	public void draw(final Graphics2D g) {
  77
+		traverse(new FigureAction() {
  78
+			@Override
  79
+			public void execute(Figure figure) {
  80
+				figure.draw(g);
  81
+			}
  82
+		});
74 83
 		drawHandles(g);
75 84
 	}
76 85
 
77 86
 	@Override
78 87
 	public Rectangle getBounds() {
79  
-		Rectangle r = new Rectangle(0, 0, -1, -1);
80  
-		for (Figure figure : subFigures) {
81  
-			r.add(figure.getBounds());
82  
-		}
  88
+		final Rectangle r = new Rectangle(0, 0, -1, -1);
  89
+		traverse(new FigureAction() {
  90
+			@Override
  91
+			public void execute(Figure figure) {
  92
+				r.add(figure.getBounds());
  93
+			}
  94
+		});
83 95
 		return r;
84 96
 	}
85 97
 
86 98
 	@Override
87  
-	public void move(Dimension offset) {
88  
-		for (Figure figure : subFigures) {
89  
-			figure.move(offset);
90  
-		}
  99
+	public void move(final Dimension offset) {
  100
+		traverse(new FigureAction() {
  101
+			@Override
  102
+			public void execute(Figure figure) {
  103
+				figure.move(offset);
  104
+			}
  105
+		});
91 106
 	}
92 107
 
93 108
 	@Override
@@ -157,4 +172,7 @@ public Point getLocation() {
157 172
 		}
158 173
 	}
159 174
 
  175
+	private interface FigureAction {
  176
+		void execute(Figure figure);
  177
+	}
160 178
 }

0 notes on commit 0384ed9

Please sign in to comment.
Something went wrong with that request. Please try again.