Abstract & Interface GOF Design Pattern

Post on 02-Feb-2016

50 views 0 download

description

Abstract & Interface GOF Design Pattern. Tim Asisten Praktikum PSBO 08/09. Once upon a time. Buat program tentang simulasi hewan untuk anak sayaa!. Boss besar. Yawda, desain dulu aja. Aha! Gimana kalo bikin hewan-hewan jadi objek?. 1. Lihat atribut dan behaviour!. Atribut : picture - PowerPoint PPT Presentation

Transcript of Abstract & Interface GOF Design Pattern

Abstract & InterfaceGOF Design Pattern

Tim Asisten Praktikum PSBO 08/09

Once upon a time..

Buat program tentang simulasi

hewan untuk anak sayaa!Boss besar

Yawda, desain

dulu aja..

Aha! Gimana kalo bikin hewan-hewan jadi objek?

1. Lihat atribut dan behaviour!

• Atribut :– picture– food– hunger– boundaries– location

• Behaviour– makeNoise()– eat()– sleep()– roam()

2. Buat classSUPERCLAS

S

SUBCLASS

Do all animals eat the same

way?

3. Subclass memiliki behaviour yang lebih spesifik.

Jadi,Kedua method ini akan dioverride, sementara dua method yang lain akan tetap generic..

4. Lihat kesamaan behaviour yang lain..

Class Hierarchy

???

?

?

?

.. Some classes just should not be instantiated!

Abstract Class

• Tidak dapat diinstantiasi -> superclass• Class yang berisi satu atau lebih abstract

method– Jika suatu kelas memiliki abstract method,

maka kelas tersebut harus didefinisikan sebagai abstract class

• Subclass harus mengimplementasikan abstract method superclassnya (abstract class)

Abstract Method

• Method yang tidak memiliki body– Harus di override oleh subclassnya

• Constructor dan static method tidak boleh abstract

• Harus diimplementasikan oleh method dari subclass

Sooo.. Abstract..

Shape

draw{abstract}() : void

Segitiga

draw() : void

SegiEmpat

draw() : void

SegiLima

draw() : void

Sintaks

• Abstract Class– [ClassModifier] abstract class [ClassName]– contoh :public abstract class Shape{}

• Abstract Method– [Modifier] abstract [ReturnType][MethodName]

([parameter])– contoh :public abstract void draw();

Sintaks

• Subclass (pengguna abstract)– [ClassModifier] class [ClassName] extends

[AbstractClass]– contoh :public class Segitiga extends Shape{}

Another case..

What if..

..semua anggota abstract class adalah abstract method?

Interface

• Interface adalah abstract class yang seluruh methodnya adalah abstract method.

• Variabel dari interface selalu static dan final.• Bukan class, tapi prototype untuk class (yang

nanti akan mengimplementasikan)• Sebuah class dapat mengimplementasikan lebih

dari satu interface.

Interface sebagai Tipe Data

• Dapat digunakan sebagai variabel sebuah class• Dapat digunakan sebagai parameter

Sintaks

• Membuat interface :– interface [InterfaceName]– Contoh :

interface Animal

• Menggunakan interface :– [ClassModifier] class [ClassName] implements

[InterfaceName]– Contoh :

public class Cat implements Animal

Pewarisan antar interface

• Interface dapat mewarisi interface lainnya• Contoh :

interface Animal{

/*some code*/ }

interface Mammal extends Animal {

/*some code*/ }

Class Diagram

Latihan

• Refer to labwork_05.pdf

Rational Objectory

• Software Engineering Process• Its goal is to ensure :

– the production of high-quality software– meeting the needs of its end-users– within a predictable schedule and budget

Objectory

• Objectory adalah suatu proses iterasi atau bisa juga diartikan sebagai suatu proses terkontrol

• Jadi objectory adalah proses iterasi terkontrol• Objectory focus pada arsitektur dari sistem• Aktifitas pengembangan objectory diarahkan

oleh use case, object oriented proses, penggunaan UML sebagai pemodelan dan bisa di konfigurasi untuk menentukan ruang lingkup suatu project

Objectory (cont)

• Objectory proses dibagi menjadi 4 phase yang membentuk pola pengembangan cycle– Inception phase/ fase permulaan– Elaboration phase/ fase perluasan– Construction phase/ fase pembentukan– Transition phase/ fase transisi

Objectory (cont)

Setiap fase di tutup oleh suatu milestone, yaitu suatu keadaan di mana beberapa keputusan critical harus dibentuk

Once upon a time.. (AGAIN)

Sekarang....tolong bikinin

simulai bebek2an buat

anak kedua saya..

Boss besar

Bagus juga

desain Animalmu

!

What do you think about this design?

Design Pattern

• Muncul masalah ketika membuat program OOP :– Handle Object– Perubahan pada sistem

• Bagaimana biasanya programmer mengatasi masalah?– Some say, “Pengalaman! Semakin banyak pengalaman

seorang designer software, semakin elegant, simple dan flexible-lah code-nya. ”

• New problem : TIME

Design Pattern

• Solusi yang sama untuk problem yang sama yang berulang-ulang

• Learning curve yang hanya bisa didapat dari pengalaman dapat di kurangi

GoF Design Pattern

• Gank of Four (GoF) :Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides

• “Design Patterns: Elements of Reusable Object-Oriented Software”

GoF Design Pattern

Creational (C) • Factory Method, Abstract Factory, Builder, Singleton,

PrototypeStructural (S) • Decorator, Composite, Proxy, Adapter, Bridge, Flyweight,

Façade Behavioral (B) • Strategy, Iterator, Template Method, Mediator, Observer,

Chain of Responsibility, Memento, Command, State, Visitor, Interpreter

Strategy Pattern

Motivation• "Define a family of algorithms, encapsulate each one, and

make them interchangeable. Strategy lets the algorithm vary independently from the clients that use it." [Gamma, p315]

• Capture the abstraction in an interface, bury implementation details in derived classes

Structure

Contoh

GOF Design Pattern

..to be continue..