Template Method (Roti lagi...)

Tuesday, January 10, 2006 | rizan

Assalamu'alaikum,
Salam Ei'dul Adha utk semua muslimin/muslimat, selamat bercuti utk yg bukan muslim.

Finished this chapter during the holiday, from this book

From Huston
Define the skeleton of an algorithm in an operation, deferring some steps to client subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure.

Lebih kurang sama jer definition.

Kita ada steps dlm sesuatu tugas/kerja, tapi mcm ada yg sama prosedur utk membuat sesuatu. Selalu jugak lepak kedai mamak usyar (tengok) depa ni buat roti. Most of it, sama jer step dia, so came out with this :

abstract class Roti {
final void siapOrder() {
uliTepung();
tebar();
tambahPerisa();
goreng();
letakDlmPinggan();
}

void uliTepung() {
System.out.println("Tgh uli tepung");
}

void tebar() {
System.out.println("Tebar lebar2");
}

abstract void tambahPerisa();

void goreng() {
System.out.println("Letak minyak dan goreng");
}

void letakDlmPinggan() {
System.out.println("Potong & letak dlm pinggan yg BERSIH");
}

}

class RotiPisang extends Roti {
public RotiPisang() {
System.out.println("\n\nSedia Roti Pisang");
}

void tambahPerisa() {
System.out.println("Potong pisang nipis2 dan tambah");
}
}

class RotiSardin extends Roti {
public RotiSardin() {
System.out.println("\n\nSedia Roti Sardin");
}

void tambahPerisa() {
System.out.println("Tambah sardin");
}

}

public class RotiTemplateDemo {
public static void main(String[] args) {
RotiPisang rotiPisang = new RotiPisang();
rotiPisang.siapOrder();

RotiSardin rotiSardin = new RotiSardin();
rotiSardin.siapOrder();
}
}

Yang membezakan antara Roti Pisang & Roti Sardin, perisa dia saja. So kita asingkan perisa dia dlm concrete class tetapi steps dia sampai siap roti tu, sama. Sebab tu la kita letak final utk stress kan yg prosedur tak boleh ubah.

Wassalam.
p/s : next mungkin kita boleh letak hook (utk bezakan pilihan customer)

3 Responses to "Template Method (Roti lagi...)"


Anonymous says:

Hi , bila nak update??
dah setahun dah...

5:40 PM

Anonymous says:

Hi , bila nak update??
dah setahun dah...

5:41 PM

Anonymous says:

Bila Nak Update?

7:14 PM

« Older Entries Newer Entries »