Chain of Responsiblity (CoR)

Wednesday, December 28, 2005 | rizan

Assalamu'alaikum,

Dah lama jugak tak update sejak 1st post dulu, sibuk sesangat sejak masuk company baru ni.
Banyak benda kena belajaq :-)

Apa dia CoR ni ? Salah satu pattern yg dlm category behavioral.
"Satu object handle specific request tanpa mengetahui apa yg dibuat oleh object lain. Kalau object tu tak blh handle, dia akan pass kpd object lain sampai jumpa"

Cth code:
abstract class Mamak {
Mamak mamak;
public void assignMamak(Mamak mamak) {
this.mamak = mamak;
}
public abstract void buatKerja();
public abstract void ambikOrder(String order);
}

class MamakRotiCanai extends Mamak {
public void buatKerja() {
System.out.println("roti canai siap !!!");
}
public void ambikOrder(String order) {
if(order.equals("roti canai"))
this.buatKerja();
else
if(mamak != null)
mamak.ambikOrder(order);
}
}


class MamakTehTarik extends Mamak {
public void buatKerja() {
System.out.println("teh tarik siap !!!");
}
public void ambikOrder(String order) {
if(order.equals("teh tarik"))
this.buatKerja();
else
if(mamak != null)
mamak.ambikOrder(order);
}
}

public class MamakDemo {
public static void main( String[] args ) {
MamakTehTarik mamakTehTarik = new MamakTehTarik();
MamakRotiCanai mamakRotiCanai = new MamakRotiCanai();
//tgh buat rantaian...
mamakTehTarik.assignMamak(mamakRotiCanai);

String[] order = new String[] {"roti canai", "teh tarik", "pasembor"};//pasembor tak akan jumpa specific mamak
for(int i = 0; i < order.length; i++) {
String o = order[i];
System.out.println("order " + o);
mamakTehTarik.ambikOrder(o);
}
}
}

p/s : Terima Kasih kpd en Hamdi di atas cth di atas

« Older Entries Newer Entries »