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

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

  • Upload
    lamkien

  • View
    216

  • Download
    1

Embed Size (px)

Citation preview

POLA-POLA

PERANCANGAN (PPP)

Structural pattern: Composite

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

Tujuan perkuliahan

• Memahami structural pattern: Composite

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

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

Motivation

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

Motivation (1)

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

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

Structure

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

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

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

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

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

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

Sample code

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

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

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

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

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

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

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

Wait a sec….

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

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

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

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

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

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

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