pola-pola perancangan (ppp) - wordpress.com · bahan kuliah ppp - iterator pattern | tri a....

Post on 27-Jul-2018

229 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

POLA-POLA

PERANCANGAN (PPP)

Behavioral pattern: Iterator

Bahan Kuliah PPP - Iterator pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 2/26

Tujuan perkuliahan

• Memahami behavioral pattern: Iterator

Bahan Kuliah PPP - Iterator pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 3/26

Intent and Known As• Intent:

– Provide a way to access the elements of an

aggregate object sequentially without

exposing its underlying representation

• Know As:

– Cursor

Bahan Kuliah PPP - Iterator pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 4/26

Motivation

• Objectville diner and Objectville pancake

house are merging into one entity two

menus of them need to merged.

• The problem is that the menu items have

been stored in an ArrayList for the pancake

house and an Array for the diner. Neither of

the owners are willing to change their

implementation

Bahan Kuliah PPP - Iterator pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 5/26

Motivation (1)

Bahan Kuliah PPP - Iterator pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 6/26

Motivation (2)

Bahan Kuliah PPP - Iterator pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 7/26

Motivation (3)

Bahan Kuliah PPP - Iterator pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 8/26

Motivation (4)

• Suppose we are required to print every item on both menus two loops will be needed instead of one (ArrayList and Array).

• If a third restaurant is included in the merger, three loops will be needed.

• Design principles that would be violated:– Coding to implementation rather than interface

– The program implementing the joint print_menu() needs to know the internal structure of the collection of each set of menu items.

– Duplication of code

Bahan Kuliah PPP - Iterator pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 9/26

Motivation (5)

• Encapsulate what varies, i.e. encapsulate the iteration.

• An iterator is used for this purpose.

• The DinerMenu class and the PancakeMenu class need to implement a method called createIterator().

• The Iterator is used to iterate through each collection without knowing its type (i.e. Array or ArrayList)

Bahan Kuliah PPP - Iterator pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 10/26

Bahan Kuliah PPP - Iterator pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 11/26

Bahan Kuliah PPP - Iterator pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 12/26

Motivation (8)

Bahan Kuliah PPP - Iterator pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 13/26

Motivation (9)

Bahan Kuliah PPP - Iterator pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 14/26

Structure

Bahan Kuliah PPP - Iterator pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 15/26

Participants

• Iterator– defines an interface for accessing and traversing elements

• ConcreteIterator– implements the Iterator interface

– Keeps track of the current position in the traversal of the aggregate

• Aggregate– defines an interface for creating an Iterator object

• ConcreteAggregate– implements the Iterator creation interface to return an instance

of the proper ConcreteIterator

Bahan Kuliah PPP - Iterator pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 16/26

Sample code – first version

Bahan Kuliah PPP - Iterator pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 17/26

Sample code – first version (1)

Bahan Kuliah PPP - Iterator pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 18/26

Sample code – first version (2)

Bahan Kuliah PPP - Iterator pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 19/26

Sample code – first version (3)

Bahan Kuliah PPP - Iterator pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 20/26

Sample code – first version (4)

Bahan Kuliah PPP - Iterator pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 21/26

Sample code – first version (5)

Bahan Kuliah PPP - Iterator pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 22/26

Sample code – first version (6)

Bahan Kuliah PPP - Iterator pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 23/26

Sample code – using java.util.Iterator

Bahan Kuliah PPP - Iterator pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 24/26

Sample code – using java.util.Iterator (1)

Bahan Kuliah PPP - Iterator pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 25/26

Sample code – using java.util.Iterator (2)

Bahan Kuliah PPP - Iterator pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 26/26

top related