POLA-POLA PERANCANGAN (PPP) - WordPress.com · Bahan Kuliah PPP - Iterator pattern | Tri A....

26
POLA-POLA PERANCANGAN (PPP) Behavioral pattern: Iterator

Transcript of POLA-POLA PERANCANGAN (PPP) - WordPress.com · Bahan Kuliah PPP - Iterator pattern | Tri A....

Page 1: POLA-POLA PERANCANGAN (PPP) - WordPress.com · Bahan Kuliah PPP - Iterator pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 4/26 Motivation •Objectville diner and Objectville pancake

POLA-POLA

PERANCANGAN (PPP)

Behavioral pattern: Iterator

Page 2: POLA-POLA PERANCANGAN (PPP) - WordPress.com · Bahan Kuliah PPP - Iterator pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 4/26 Motivation •Objectville diner and Objectville pancake

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

Tujuan perkuliahan

• Memahami behavioral pattern: Iterator

Page 3: POLA-POLA PERANCANGAN (PPP) - WordPress.com · Bahan Kuliah PPP - Iterator pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 4/26 Motivation •Objectville diner and Objectville pancake

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

Page 4: POLA-POLA PERANCANGAN (PPP) - WordPress.com · Bahan Kuliah PPP - Iterator pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 4/26 Motivation •Objectville diner and Objectville pancake

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

Page 5: POLA-POLA PERANCANGAN (PPP) - WordPress.com · Bahan Kuliah PPP - Iterator pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 4/26 Motivation •Objectville diner and Objectville pancake

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

Motivation (1)

Page 6: POLA-POLA PERANCANGAN (PPP) - WordPress.com · Bahan Kuliah PPP - Iterator pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 4/26 Motivation •Objectville diner and Objectville pancake

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

Motivation (2)

Page 7: POLA-POLA PERANCANGAN (PPP) - WordPress.com · Bahan Kuliah PPP - Iterator pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 4/26 Motivation •Objectville diner and Objectville pancake

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

Motivation (3)

Page 8: POLA-POLA PERANCANGAN (PPP) - WordPress.com · Bahan Kuliah PPP - Iterator pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 4/26 Motivation •Objectville diner and Objectville pancake

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

Page 9: POLA-POLA PERANCANGAN (PPP) - WordPress.com · Bahan Kuliah PPP - Iterator pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 4/26 Motivation •Objectville diner and Objectville pancake

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)

Page 10: POLA-POLA PERANCANGAN (PPP) - WordPress.com · Bahan Kuliah PPP - Iterator pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 4/26 Motivation •Objectville diner and Objectville pancake

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

Page 11: POLA-POLA PERANCANGAN (PPP) - WordPress.com · Bahan Kuliah PPP - Iterator pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 4/26 Motivation •Objectville diner and Objectville pancake

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

Page 12: POLA-POLA PERANCANGAN (PPP) - WordPress.com · Bahan Kuliah PPP - Iterator pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 4/26 Motivation •Objectville diner and Objectville pancake

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

Motivation (8)

Page 13: POLA-POLA PERANCANGAN (PPP) - WordPress.com · Bahan Kuliah PPP - Iterator pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 4/26 Motivation •Objectville diner and Objectville pancake

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

Motivation (9)

Page 14: POLA-POLA PERANCANGAN (PPP) - WordPress.com · Bahan Kuliah PPP - Iterator pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 4/26 Motivation •Objectville diner and Objectville pancake

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

Structure

Page 15: POLA-POLA PERANCANGAN (PPP) - WordPress.com · Bahan Kuliah PPP - Iterator pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 4/26 Motivation •Objectville diner and Objectville pancake

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

Page 16: POLA-POLA PERANCANGAN (PPP) - WordPress.com · Bahan Kuliah PPP - Iterator pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 4/26 Motivation •Objectville diner and Objectville pancake

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

Sample code – first version

Page 17: POLA-POLA PERANCANGAN (PPP) - WordPress.com · Bahan Kuliah PPP - Iterator pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 4/26 Motivation •Objectville diner and Objectville pancake

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

Sample code – first version (1)

Page 18: POLA-POLA PERANCANGAN (PPP) - WordPress.com · Bahan Kuliah PPP - Iterator pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 4/26 Motivation •Objectville diner and Objectville pancake

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

Sample code – first version (2)

Page 19: POLA-POLA PERANCANGAN (PPP) - WordPress.com · Bahan Kuliah PPP - Iterator pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 4/26 Motivation •Objectville diner and Objectville pancake

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

Sample code – first version (3)

Page 20: POLA-POLA PERANCANGAN (PPP) - WordPress.com · Bahan Kuliah PPP - Iterator pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 4/26 Motivation •Objectville diner and Objectville pancake

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

Sample code – first version (4)

Page 21: POLA-POLA PERANCANGAN (PPP) - WordPress.com · Bahan Kuliah PPP - Iterator pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 4/26 Motivation •Objectville diner and Objectville pancake

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

Sample code – first version (5)

Page 22: POLA-POLA PERANCANGAN (PPP) - WordPress.com · Bahan Kuliah PPP - Iterator pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 4/26 Motivation •Objectville diner and Objectville pancake

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

Sample code – first version (6)

Page 23: POLA-POLA PERANCANGAN (PPP) - WordPress.com · Bahan Kuliah PPP - Iterator pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 4/26 Motivation •Objectville diner and Objectville pancake

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

Sample code – using java.util.Iterator

Page 24: POLA-POLA PERANCANGAN (PPP) - WordPress.com · Bahan Kuliah PPP - Iterator pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 4/26 Motivation •Objectville diner and Objectville pancake

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

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

Page 25: POLA-POLA PERANCANGAN (PPP) - WordPress.com · Bahan Kuliah PPP - Iterator pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 4/26 Motivation •Objectville diner and Objectville pancake

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

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

Page 26: POLA-POLA PERANCANGAN (PPP) - WordPress.com · Bahan Kuliah PPP - Iterator pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 4/26 Motivation •Objectville diner and Objectville pancake

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