1 / 2

AmidaInterface

Design for Change Keep implementation details hidden behind well-defined interface Expose meaningful methods that serve a distinct purpose. AmidaInterface. What about? numEdges(int line) getEdge (int line, int num)

aloha
Télécharger la présentation

AmidaInterface

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Design for Change • Keep implementation details hidden behind well-defined interface • Expose meaningful methods that serve a distinct purpose AmidaInterface What about?numEdges(int line) getEdge (int line, int num) They have nothing to do withthe game and are artificialcontrivances numLines()addLine()removeLine()removeAll() addEdge(int, int)numEdges(int)getEdge(int, int)removeEdge(Edge e)tooClose(int, int) exitLine(int) linesedges No external entity wants to get the 3rdhorizontal edge for the 2nd vertical line. Instead, we want to do: for lines 1 through numberLines { get each of the Edges for the line draw the Edge} for (int line = 1; line < n; line++) {int num = board.numEdges (line); // draw each edge, which are always to the right for (int j = 1 ; j <= num; j++) { Edge e = board.getEdge (line, j); // Draw Edge now … }} original for (int line = 1; line < n; line++) { Iterator it = board.edges (line); while (it.hasNext()) { Edge e = (Edge) it.next(); // Draw Edge now … } } with Iterator Review TicTacToe example. Added: public char getMark (int r, int c); Iterator Interface and Usage

  2. import java.util.Iterator; import amida.Edge; public class EdgeIterator implements Iterator { /** Current Node in the Iteration. */ EdgeNode node; /** * Construct Iterator with reference to first one. * @param n First one to start off the iteration */ public EdgeIterator (EdgeNode n) { node = n; } /** * If more to return, then node references that object * @return true once the Iteration has ended. */ public boolean hasNext() { return (node != null); } /** * Return the next object in the Iteration. */ public Object next() { Edge e = node.edge; // Extract Edge to return node = node.next; // Advance Iterator for next time return e; } /** * Optional remove not supported method. */ public void remove() { throw new UnsupportedOperationException ("remove not supported on LineNode"); }} Q: Who constructs Iterator object? A: The entity responsible for the collection. In this case: LineNode /** * Begin with the first one. * @return Iterator object */ public Iterator edges() { return new EdgeIterator (head); } Q: Who uses Iterator? A: Anyone who wants to iterate over all elements Iterator Interface and Usage

More Related