Top Banner
POLA-POLA PERANCANGAN (PPP) Structural pattern: Composite
27

POLA-POLA PERANCANGAN (PPP) - WordPress.com · Bahan Kuliah PPP - Composite pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 3/27 Intent • Compose objects into tree structures to represent

Aug 16, 2019

Download

Documents

lamkien
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: POLA-POLA PERANCANGAN (PPP) - WordPress.com · Bahan Kuliah PPP - Composite pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 3/27 Intent • Compose objects into tree structures to represent

POLA-POLA

PERANCANGAN (PPP)

Structural pattern: Composite

Page 2: POLA-POLA PERANCANGAN (PPP) - WordPress.com · Bahan Kuliah PPP - Composite pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 3/27 Intent • Compose objects into tree structures to represent

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

Tujuan perkuliahan

• Memahami structural pattern: Composite

Page 3: POLA-POLA PERANCANGAN (PPP) - WordPress.com · Bahan Kuliah PPP - Composite pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 3/27 Intent • Compose objects into tree structures to represent

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

Intent• Compose objects into tree structures to

represent part-whole hierarchies

• Composite lets clients treat individual

objects and compositions of objects

uniformly

Page 4: POLA-POLA PERANCANGAN (PPP) - WordPress.com · Bahan Kuliah PPP - Composite pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 3/27 Intent • Compose objects into tree structures to represent

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

Motivation

Page 5: POLA-POLA PERANCANGAN (PPP) - WordPress.com · Bahan Kuliah PPP - Composite pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 3/27 Intent • Compose objects into tree structures to represent

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

Motivation (1)

Page 6: POLA-POLA PERANCANGAN (PPP) - WordPress.com · Bahan Kuliah PPP - Composite pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 3/27 Intent • Compose objects into tree structures to represent

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

Applicability

• You want to represent part-whole

hierarchies of objects

• You want clients to be able to ignore the

difference between compositions of objects

and individual objects clients will treat all

objects in the composite structure

uniformly

Page 7: POLA-POLA PERANCANGAN (PPP) - WordPress.com · Bahan Kuliah PPP - Composite pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 3/27 Intent • Compose objects into tree structures to represent

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

Structure

Page 8: POLA-POLA PERANCANGAN (PPP) - WordPress.com · Bahan Kuliah PPP - Composite pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 3/27 Intent • Compose objects into tree structures to represent

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

Page 9: POLA-POLA PERANCANGAN (PPP) - WordPress.com · Bahan Kuliah PPP - Composite pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 3/27 Intent • Compose objects into tree structures to represent

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

Participants

• Component– declares the interface for objects in the composition

– implements default behavior for the interface common to all classes, as appropriate

– declares an interface for accessing and managing its child components

– (optional) defines an interface for accessing a component's parent in the recursive structure, and implements it if that's appropriate

• Leaf– represents leaf objects in the composition A leaf has no

children

– defines behavior for primitive objects in the composition

Page 10: POLA-POLA PERANCANGAN (PPP) - WordPress.com · Bahan Kuliah PPP - Composite pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 3/27 Intent • Compose objects into tree structures to represent

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

Participants (1)

• Composite– defines behavior for components having

children

– stores child components

– implements child-related operations in the Component interface

• Client– manipulates objects in the composition through

the Component interface

Page 11: POLA-POLA PERANCANGAN (PPP) - WordPress.com · Bahan Kuliah PPP - Composite pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 3/27 Intent • Compose objects into tree structures to represent

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

Collaborations

• Client use the Component class interface to interact with objects in the composite structure:

– If the recipient is a Leaf, then the request is handled directly

– If not, then it usually forwards requests to its child components, possibly performing additional operations before and/or after forwarding

Page 12: POLA-POLA PERANCANGAN (PPP) - WordPress.com · Bahan Kuliah PPP - Composite pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 3/27 Intent • Compose objects into tree structures to represent

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

Consequences

• Defines class hierarchies consisting of primitive objects and composite objects

• Makes the client simple do not know whether a leaf or a composite to deal with

• Makes it easier to add new kinds of components Client do not need to change

• (disadvantage) Can make your design overly general

Page 13: POLA-POLA PERANCANGAN (PPP) - WordPress.com · Bahan Kuliah PPP - Composite pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 3/27 Intent • Compose objects into tree structures to represent

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

Sample code

Page 14: POLA-POLA PERANCANGAN (PPP) - WordPress.com · Bahan Kuliah PPP - Composite pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 3/27 Intent • Compose objects into tree structures to represent

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

Page 15: POLA-POLA PERANCANGAN (PPP) - WordPress.com · Bahan Kuliah PPP - Composite pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 3/27 Intent • Compose objects into tree structures to represent

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

Page 16: POLA-POLA PERANCANGAN (PPP) - WordPress.com · Bahan Kuliah PPP - Composite pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 3/27 Intent • Compose objects into tree structures to represent

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

Page 17: POLA-POLA PERANCANGAN (PPP) - WordPress.com · Bahan Kuliah PPP - Composite pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 3/27 Intent • Compose objects into tree structures to represent

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

Page 18: POLA-POLA PERANCANGAN (PPP) - WordPress.com · Bahan Kuliah PPP - Composite pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 3/27 Intent • Compose objects into tree structures to represent

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

Page 19: POLA-POLA PERANCANGAN (PPP) - WordPress.com · Bahan Kuliah PPP - Composite pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 3/27 Intent • Compose objects into tree structures to represent

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

Page 20: POLA-POLA PERANCANGAN (PPP) - WordPress.com · Bahan Kuliah PPP - Composite pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 3/27 Intent • Compose objects into tree structures to represent

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

Wait a sec….

Page 21: POLA-POLA PERANCANGAN (PPP) - WordPress.com · Bahan Kuliah PPP - Composite pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 3/27 Intent • Compose objects into tree structures to represent

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

Page 22: POLA-POLA PERANCANGAN (PPP) - WordPress.com · Bahan Kuliah PPP - Composite pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 3/27 Intent • Compose objects into tree structures to represent

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

Page 23: POLA-POLA PERANCANGAN (PPP) - WordPress.com · Bahan Kuliah PPP - Composite pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 3/27 Intent • Compose objects into tree structures to represent

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

Page 24: POLA-POLA PERANCANGAN (PPP) - WordPress.com · Bahan Kuliah PPP - Composite pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 3/27 Intent • Compose objects into tree structures to represent

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

Page 25: POLA-POLA PERANCANGAN (PPP) - WordPress.com · Bahan Kuliah PPP - Composite pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 3/27 Intent • Compose objects into tree structures to represent

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

Page 26: POLA-POLA PERANCANGAN (PPP) - WordPress.com · Bahan Kuliah PPP - Composite pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 3/27 Intent • Compose objects into tree structures to represent

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

Page 27: POLA-POLA PERANCANGAN (PPP) - WordPress.com · Bahan Kuliah PPP - Composite pattern | Tri A. Kurniawan, S.T, M.T, Ph.D 3/27 Intent • Compose objects into tree structures to represent

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