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)

Roti Telur, anyone ?

Wednesday, January 04, 2006 | rizan

Assalamu'alaikum,

Disebabkan first post cerita psl mamak, so kita ambik contoh makanan yg mamak buat, Roti Canai. Memang roti canai ni takkan terdapat di mana2 dlm dunia ni kecuali Malaysia. Originated from Malaysia. Berkat hasil usaha org kita (especially mamak) dlm menghasilkan variety roti canai, terhasillah contoh ni.

abstract class Roti {
String description = "Unknown Roti"; //tak tau lagi roti apa ni...
public String getDescription() {
return description;
}
public abstract double cost();
}

class RotiKosong extends Roti {
public RotiKosong() {
description = "Roti";
}
public double cost() {
return 0.5;
}
}

abstract class RasaDecorator extends Roti {
public abstract String getDescription();
}

class Telur extends RasaDecorator {
Roti roti;

public Telur(Roti roti) {
this.roti = roti;
}

public String getDescription() {
return roti.getDescription() + " Telur";
}

public double cost() {
return .30 + roti.cost();
}
}

class Bawang extends RasaDecorator {
Roti roti;

public Bawang(Roti roti) {
this.roti = roti;
}

public String getDescription() {
return roti.getDescription() + " Bawang";
}

public double cost() {
return .20 + roti.cost();
}
}

class Planta extends RasaDecorator {
Roti roti;

public Planta(Roti roti) {
this.roti = roti;
}

public String getDescription() {
return roti.getDescription() + " Planta";
}

public double cost() {
return .30 + roti.cost();
}
}


class Pisang extends RasaDecorator {
Roti roti;

public Pisang(Roti roti) {
this.roti = roti;
}

public String getDescription() {
return roti.getDescription() + " Pisang";
}

public double cost() {
return .40 + roti.cost();
}
}

public class RotiDemo {
public static void main(String[] args) {
//roti telur saja
Roti roti = new RotiKosong();
roti = new Telur(roti);
System.out.println(roti.getDescription()
+ " RM" + roti.cost());

//my favourite, roti pisang
roti = new RotiKosong();
roti = new Pisang(roti);
System.out.println(roti.getDescription()
+ " RM" + roti.cost());

//roti telur campur bawang goreng dgn planta :-)
roti = new RotiKosong();
roti = new Bawang(roti);
roti = new Telur(roti);
roti = new Planta(roti); //goreng dgn planta
System.out.println(roti.getDescription()
+ " RM " + roti.cost());
}
}

Contoh ni menggunakan Decorator Pattern, kalau ikut Huston,
"Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality."

Senang cerita, kita menggunakan decorator utk menambah sifat kepada sesuatu object (secara dinamik). Kalau ikut contoh di atas, cost adalah bergantung kpd setiap bahan yg digunakan. Maknanya, kalau kita ada tambahan bahan nak bg sedap lg roti, kita boleh tambah lg dan cost overall roti bertambah automatically. Kalau harga bahan2 naik, cthnya pisang, kita hanya ubah Pisang saja.

Demikianlah adanya.

« Older Entries Newer Entries »