Iterator と Adaptor デザインパターン 第1回

Slides:



Advertisements
Similar presentations
Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
Advertisements

CS18000: Problem Solving and Object-Oriented Programming.
Chapter 6 The Collections API. Simple Container/ Iterator Simple Container Shape [] v = new Shape[10]; Simple Iterator For( int i=0 ; i< v.length ; i++)
Builder と Abstract Factory S. Yukita
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
Grouping Objects 3 Iterators, collections and the while loop.
CPSC150 Inheritance Details Chapter 9. CPSC150 Print in Entertainment ver 2 (with inheritance): public void print() { System.out.print("title: " + title.
What is an Interface? 4 Common in everyday life. –Movie theaters, TVs, VCRs, etc. 4 Java interface definitions are simple: –Method headers. –Field definitions.
Random (1) Random class contains a method to generate random numbers of integer and double type Note: before using Random class, you should add following.
1 Topic 8 Iterators "First things first, but not necessarily in that order " -Dr. Who.
CS 307 Fundamentals of Computer ScienceIterators 1 Topic 14 Iterators "First things first, but not necessarily in that order " -Dr. Who.
16-Aug-15 Java Puzzlers From the book Java Puzzlers by Joshua Bloch and Neal Gafter.
CS2110 Recitation 07. Interfaces Iterator and Iterable. Nested, Inner, and static classes We work often with a class C (say) that implements a bag: unordered.
1 Lecture 09 Iterators and Enumerations Reading for these lectures: Weiss, Section Iterator Interface. Much better is: ProgramLive, Section 12.3.
Problem Solving using the Java Programming Language May 2010 Mok Heng Ngee Day 5: Arrays.
1 Iterators "First things first, but not necessarily in that order " -Dr. Who CS Computer Science II.
1 Building Java Programs Chapter 5 Lecture 5-1: while Loops, Fencepost Loops, and Sentinel Loops; Procedural Design reading: 5.1 – 5.2; 4.5.
Miscellaneous topics Chapter 2 Savitch. Miscellaneous topics Standard output using System.out Input using Scanner class.
Neal Stublen What’s an indexer?  An array has an indexer… int[] myArray = new int[5]; for (int index = 0; index < 5; ++index) {
ASP.NET 2.0 Provider Model 概 要. Agenda ASP.NET 2.0 Provider Model とは カスタムプロバイダ の実装 まとめ.
1 The Stack Class Final Review Fall 2005 CS 101 Aaron Bloomfield.
Factory Method Pattern S. Yukita
6Data structure design (データ構造の設計) Data structure is one of the most important aspects of a program: Program = Data Structure + Algorithm.
Builder Pattern S. Yukita Builder Pattern2 Builder パターン 使われ所 大きなインスタンスを組み上げる場合。 インスタンスの組み上げにパターンが あるとき,パターンをディレクタに固 定する。 組み上げパターンに従って実際の仕事.
Grouping objects Iterators. Iterator type Third variation to iterate over a collection Uses a while loop and Iterator object But NO integer index variable.
Lab 01-2 Objectives:  Writing a Java program.  How to send output to the command line console.  Learn about escape sequences.  Learn how to compile,
FOR LOOP WALK THROUGH public class NestedFor { public static void main(String [] args) { for (int i = 1; i
Print Me Who’s Data? First Executed Name Me My Mistake Q $100 Q $200 Q $300 Q $400 Q $500 Q $100 Q $200 Q $300 Q $400 Q $500 Final Jeopardy.
Review :chapters What is an algorithm? A step by step description of how to accomplish a task. For example, Adding 3 numbers N1, N2 and N3 Add.
Library Books exercise cosc The classes Library class +ArrayList -ArrayList + Library() +initializeDefaultCategories():void +displayAllCategories():void.
A: A: double “4” A: “34” 4.
ITERATOR Патерн поведінки Доповідач: Пропой Ярослав Рецензент: Мельниченко Владислав 0/40 NEXTSTART.
Print Row Function void PrintRow(float x[ ][4],int i) { int j; for(j=0;j
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
L12: A Wumpus World Project To be checked on 16 th January 11:10~12:40 In Lab1.
19-Mar-16 Collections and ArrayLists.. 2 Collections Why use Collections. Collections and Object-Orientation. ArrayLists. Special Features. Creating ArrayLists.
Staples are our staple Building upon our solution.
Iterators. Iterator  An iterator is any object that allows one to step through each element in a list (or, more generally, some collection).
Introduction to Programming using Java Day 3 and 4 Java Language Basics Review The “For” loop Subroutines The “String” class.
EKT472: Object Oriented Programming
Chapter 9 Repetition.
Examples of Classes & Objects
Interfaces.
Sum of natural numbers class SumOfNaturalNumbers {
Sequences and Iterators
Interfaces.
CS 302 Week 10 Jim Williams.
CS Week 8 Jim Williams, PhD.
Review Operation Bingo
"First things first, but not necessarily in that order " -Dr. Who
Chapter 9 Control Structures.
An Introduction to Java – Part I, language basics
Classes & Objects: Examples
CSE 143 Lecture 27: Advanced List Implementation
Lecture 26: Advanced List Implementation
AP Java Warm-up Boolean Array.
"First things first, but not necessarily in that order." -Dr. Who
CS 302 Week 9 Jim Williams.
Java Lesson 36 Mr. Kalmes.
COMPUTER 2430 Object Oriented Programming and Data Structures I
CSE 214 – Computer Science I More on Linked Lists
class PrintOnetoTen { public static void main(String args[]) {
"First things first, but not necessarily in that order " -Dr. Who
Recursion Problems.
JCF Collection classes and interfaces
episthmh MVP for Visual C++
"First things first, but not necessarily in that order " -Dr. Who
slides created by Alyssa Harding
Question 1a) What is printed by the following Java program? int s;
Software Design Lecture : 39.
Presentation transcript:

Iterator と Adaptor デザインパターン 第1回

デザインパターン第1回 2 1. Iterator パターン while ( it.hasNext() ) {... = it.next();... } 配列や Vector などの実装方法に関わらずに, 上のようなことをできるようにしたい。

デザインパターン第1回 3 例題のクラス図 > Aggregate iterator > Iterator hasNext next BookShelf books last getBookAt appendBook getLength iterator BookShelfIterator bookShelf index hasNext next Book name getName Creates

デザインパターン第1回 4 book.java public class Book { private String name = ""; public Book(String name) { this.name = name; } public String getName() { return name; }

デザインパターン第1回 5 BookShelf.java (1) public class BookShelf implements Aggregate{ private Book[] books; private int last = 0; public BookShelf(int maxsize) { this.books = new Book[maxsize]; } public Book getBookAt(int index) { return books[index]; }

デザインパターン第1回 6 BookShelf.java (2) public void appendBook(Book book) { this.books[last] = book; last++; } public int getLength() { return last; } public Iterator iterator() { return new BookShelfIterator(this); }

デザインパターン第1回 7 BookShelfIterator.java (1) public class BookShelfIterator implements Iterator { private BookShelf bookShelf; private int index; public BookShelfIterator(BookShelf bookShelf) { this.bookShelf = bookShelf; this.index = 0; }

デザインパターン第1回 8 BookShelfIterator.java (2) public boolean hasNext() { if (index < bookShelf.getLength()) { return true; } else { return false; }

デザインパターン第1回 9 BookShelfIterator.java (3) public Object next() { Book book = bookShelf.getBookAt(index); index++; return book; }

デザインパターン第1回 10 Aggregate.java public interface Aggregate { public abstract Iterator iterator(); }

デザインパターン第1回 11 Iterator.java public interface Iterator { public abstract boolean hasNext(); public abstract Object next(); }

デザインパターン第1回 12 Main.java (1) public class Main { public static void main(String[] args) { BookShelf bookShelf = new BookShelf(4); bookShelf.appendBook( new Book("Around the World in 80 Days")); bookShelf.appendBook(new Book("Bible")); bookShelf.appendBook(new Book("Cinderella")); bookShelf.appendBook( new Book("Daddy-Long-Legs"));

デザインパターン第1回 13 Main.java (2) Iterator it = bookShelf.iterator(); while (it.hasNext()) { Book book = (Book)it.next(); System.out.println("" + book.getName()); }

デザインパターン第1回 14 実行結果 Around the World in 80 Days Bible Cinderella Daddy-Long-Legs

デザインパターン第1回 15 パターン > Aggregate iterator > Iterator hasNext next ConcreteAggregate iterator ConcreteIterator aggregate hasNext next Creates

デザインパターン第1回 Adaptor パターン(継承を使 う) インタフェースの合わないものを合わ せる。

デザインパターン第1回 17 例題のクラス図 > Print printWeak printStrong Banner showWithParen showWithAster PrintBanner printWeak printStrong Main uses

デザインパターン第1回 18 Banner.java (1) public class Banner { private String string; public Banner(String string) { this.string = string; } public void showWithParen() { System.out.println("(" + string + ")"); }

デザインパターン第1回 19 Banner.java (2) public void showWithAster() { System.out.println("*" + string + "*"); }

デザインパターン第1回 20 PrintBanner.java public class PrintBanner extends Banner implements Print { private Banner banner; public PrintBanner(String string) { this.banner = new Banner(string); } public void printWeak() { banner.showWithParen(); } public void printStrong() { banner.showWithAster(); }

デザインパターン第1回 21 Print.java public interface Print { public abstract void printWeak(); public abstract void printStrong(); }

デザインパターン第1回 22 Main.java public class Main { public static void main(String[] args) { Print p = new PrintBanner("Hello"); p.printWeak(); p.printStrong(); }

デザインパターン第1回 23 実行結果 (Hello) *Hello*

デザインパターン第1回 24 パターン Client > Target targetMethod1 targetMethod2 Adapter targetMethod1 targetMethod2 Adaptee methodA methodB methodC uses implements extends

デザインパターン第1回 Adaptor パターン(委譲を使 う) インタフェースの合わないものを合わ せる。

デザインパターン第1回 26 例題のクラス図 Print printWeak printStrong Banner showWithParen showWithAster PrintBanner banner printWeak printStrong Main uses

デザインパターン第1回 27 PrintBanner.java (1) public class PrintBanner extends Print { private Banner banner; public PrintBanner(String string) { this.banner = new Banner(string); } public void printWeak() { banner.showWithParen(); }

デザインパターン第1回 28 PrintBanner.java (2) public void printStrong() { banner.showWithAster(); }

デザインパターン第1回 29 Print.java public abstract class Print { public abstract void printWeak(); public abstract void printStrong(); }

デザインパターン第1回 30 Main.java public class Main { public static void main(String[] args) { Print p = new PrintBanner("Hello"); p.printWeak(); p.printStrong(); }

デザインパターン第1回 31 パターン Client Target targetMethod1 targetMethod2 Adapter adaptee targetMethod1 targetMethod2 Adaptee methodA methodB methodC uses extends has