งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

Burapha University, 2001 Object-Oriented Analysis and Design Design Patterns Iterator Week #11 Jarungjit Parnjai.

งานนำเสนอที่คล้ายกัน


งานนำเสนอเรื่อง: "Burapha University, 2001 Object-Oriented Analysis and Design Design Patterns Iterator Week #11 Jarungjit Parnjai."— ใบสำเนางานนำเสนอ:

1 Burapha University, 2001 Object-Oriented Analysis and Design Design Patterns Iterator Week #11 Jarungjit Parnjai

2 Burapha University, 2001 Object-Oriented Analysis and Design The Iterator Pattern Intent เข้าถึง element ของ collection ( วัตถุที่เป็น Aggregate) สามารถเข้าถึงได้โดยไม่ต้อง เปิดเผย implementation ภายใน Motivation ListIteratorList Count ( ) Append(Element) Remove(Element) index list

3 Burapha University, 2001 Object-Oriented Analysis and Design The Iterator Pattern Motivation Client Iterator List First( ) Next( ) IsDone( ) CurrentItem( ) AbstractList CreateIterator( ) Count( ) Append(Element) Remove(Element) … ListIterator

4 Burapha University, 2001 Object-Oriented Analysis and Design The Iterator Pattern Structure Iterator First( ) Next( ) isDone( ) getCurrent( ) ConcreteCreator First( ) Next( ) isDone( ) getCurrent( ) ConcreteProduct Aggregate Client CreateIterator( ) Count( ) Append(Element) Remove(Element) …

5 Burapha University, 2001 Object-Oriented Analysis and Design The Iterator Pattern Participants Iterator กำหนด interface สำหรับ access หรือเข้าถึง ทุก element Concrete Iterator implement ส่วน interface ของ Iterator และใช้ ในการ keep track ของ ตำแหน่งล่าสุดที่ใช้ ในการเข้าถึงแต่ละ element ของวัตถุที่เป็น Aggregate Aggregate กำหนด interface สำหรับสร้าง Iterator Object Concrete Aggregate implement ส่วนของ Interface ที่ใช้ในการ สร้าง Iterator และคืนค่า (return) instance ของ ConcreteIterator

6 Burapha University, 2001 Object-Oriented Analysis and Design The Iterator Pattern Collaborations Concrete Iterator ทำการ keep track ของวัตถุ และทำการประมวลผล หาค่าวัตถุถัดไป ที่จะเข้าถึงได้

7 Burapha University, 2001 Object-Oriented Analysis and Design Iterator Demo Application

8 Burapha University, 2001 Object-Oriented Analysis and Design Structrue Enumeration hasMoreElements( ) nextElement( ) SentenceIterator hasMoreElements( ) nextElement( ) Paragraph getSentenceIterator( ) Client

9 Burapha University, 2001 Object-Oriented Analysis and Design Paragraph and Sentence import java.util.Enumeration; public class Paragraph { private String theText; public Paragraph(String text) { theText = text; } public Enumeration getSentence() { return new SentenceIterator(theText); } } public class Sentence { private String theText; public Sentence(String text) { theText = text; } public String toString() { return theText; } }

10 Burapha University, 2001 Object-Oriented Analysis and Design SentenceIterator import java.util.*; public class SentenceIterator implements Enumeration { private String theText; private int theCursor; private int nextSentencePos; public SentenceIterator(String text) { theText = text; theCursor = 0; nextSentencePos = findEnd(); } private int findEnd() { int posNL, posSP; posSP = theText.indexOf(". ", theCursor); posNL = theText.indexOf(".\n", theCursor); if (posNL > 0 && posSP > 0) return (posNL > posSP ? posSP : posNL); if (posNL > 0) return posNL; if (posSP > 0) return posSP; return -1; }

11 Burapha University, 2001 Object-Oriented Analysis and Design public boolean hasMoreElements() { return (nextSentencePos > 0); } public Object nextElement() throws NoSuchElementException { if ( ! hasMoreElements() ) throw new NoSuchElementException(); nextSentencePos++; String next = theText.substring(theCursor, nextSentencePos); theCursor = nextSentencePos; nextSentencePos = findEnd(); int i=0; char ch; while ((ch=next.charAt(i)) == ' ' || ch == '\n' || ch == '\t') i++; return new Sentence(next.substring(i)); } public String toString() { return theText; }

12 Burapha University, 2001 Object-Oriented Analysis and DesignIteratorTest import java.awt.*; import java.util.*; import java.awt.event.*; public class IteratorTest extends Frame implements ActionListener { Button iterate = new Button("Find Sentences"); TextArea input = new TextArea(15,30); TextArea output = new TextArea(15,30); Label report = new Label("", Label.CENTER); public IteratorTest() { super("Iterator Test"); setLayout(new BorderLayout()); setBackground(Color.lightGray); Panel p = new Panel();p.add(iterate); p.setFont(new Font("TimesRoman", Font.PLAIN, 12)); add(p,BorderLayout.NORTH); add(report, BorderLayout.SOUTH); p = new Panel(); p.setLayout(new GridLayout(1,2,5,5)); p.setFont(new Font("TimesRoman", Font.PLAIN, 12)); p.add(input);p.add(output); add(p, BorderLayout.CENTER);

13 Burapha University, 2001 Object-Oriented Analysis and Design addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent e) { setVisible(false); dispose(); System.exit(0); } ); iterate.addActionListener(this); pack(); show(); } public void actionPerformed(ActionEvent ae) { Paragraph p = new Paragraph(input.getText()); Enumeration e = p.getSentence(); output.setText("");int nSentence = 0; while (e.hasMoreElements()) { Sentence s = (Sentence) e.nextElement(); nSentence++; output.append("\n"+nSentence+": "+s); } report.setText("Sentences read ="+nSentence); } public static void main(String[] args) { new IteratorTest(); }

14 Burapha University, 2001 Object-Oriented Analysis and Design The Iterator Pattern Consequences สนับสนุนรูปแบบที่หลากหลายของการ เข้าถึงวัตถุที่เป็น Aggregate Iterator ช่วยให้ interface ของ Aggregate ทำได้ ง่ายขึ้น สามารถมีการเข้าถึงวัตถุที่เป็น Aggregate ได้ มากกว่า 1 การเข้าถึง

15 Burapha University, 2001 Object-Oriented Analysis and Design Summary Iterator Pattern


ดาวน์โหลด ppt Burapha University, 2001 Object-Oriented Analysis and Design Design Patterns Iterator Week #11 Jarungjit Parnjai.

งานนำเสนอที่คล้ายกัน


Ads by Google