Setelah jumatan ada kuliah... waktu nya tidur siang si sebenarna.. tapi karena memang sudah kewajiban untuk mengikuti mata kuliah ke PGRI-an.. ya diikuti ajalah, skalian ane juga mau update lagi tentag design pattern yang rencana nya ane akhiri semua pembahasan design pattern hari ini.. semoga aja bisa... hehehee.. oke kali ini tentang pattern observer yang akan ane bahas dan penerapannya di Java,C# dan PHP .. pattern ini termasuk dalam kategori behaviour yang menangani kelakuan sebuah program... dan juga termasuk sering dipakai, dan juga sering kita temui...
Contoh real nya yaitu seperti notifikasi di facebook... konsep kerja pattern ini adalah melaporkan secara otomatis ke server jika ada sebuah data/proses baru dan si server melaporkan ke semua client yang telah didaftarkan sebelumnya... sekarang ane akan mencoba untuk mengimplementasikannya di Java, C# dan PHPJava
package ObServer; import java.util.*; public class MainClass { public static void main(String[] args) { SubjectConcrete subject = new SubjectConcrete(); subject.Add(new User(subject, "Rizal")); subject.Add(new User(subject, "Afani")); subject.SetSubjectStatus("Selamat Pagi"); subject.Notivy(); } } interface Subject{ public void Add(ObServer observer); public void Remove(ObServer observer); public void Notivy(); } interface ObServer{ public void DoUpdate(); } class SubjectConcrete implements Subject{ private List<ObServer> list_observer = new ArrayList<ObServer>(); private String SubjectStatus; public void SetSubjectStatus(String status){ this.SubjectStatus = status; } public String GetSubjectStatus(){ return this.SubjectStatus; } @Override public void Add(ObServer observer) { list_observer.add(observer); } @Override public void Remove(ObServer observer) { list_observer.remove(observer); } @Override public void Notivy() { for(ObServer list : list_observer){ list.DoUpdate(); } } } class User implements ObServer{ private String name; private SubjectConcrete subject; public User(SubjectConcrete subject,String name) { this.subject = subject; this.name = name; } public void SetName(String name){ this.name = name; } public String GetName(){ return this.name; } public void SetSubject(SubjectConcrete subject){ this.subject = subject; } public SubjectConcrete GetSubject(){ return this.subject; } @Override public void DoUpdate() { System.out.println(this.name+" membaca status Baru '"+this.subject.GetSubjectStatus()+"'"); } }
C#
using System; using System.Collections.Generic; namespace Observer { class Program { public static void Main(string[] args) { SubjectConcrete subject = new SubjectConcrete(); subject.Add(new User(subject, "Rizal")); subject.Add(new User(subject, "Afani")); subject.SetSubjectStatus("Selamat Pagi"); subject.Notivy(); Console.ReadKey(true); } } interface Subject{ void Add(ObServer observer); void Remove(ObServer observer); void Notivy(); } interface ObServer{ void DoUpdate(); } class SubjectConcrete : Subject{ private List<ObServer> list_observer = new List<ObServer>(); private string SubjectStatus; public void SetSubjectStatus(string status){ this.SubjectStatus = status; } public string GetSubjectStatus(){ return this.SubjectStatus; } public void Add(ObServer observer) { list_observer.Add(observer); } public void Remove(ObServer observer) { list_observer.Remove(observer); } public void Notivy() { foreach(ObServer list in list_observer){ list.DoUpdate(); } } } class User : ObServer{ private string name; private SubjectConcrete subject; public User(SubjectConcrete subject,string name) { this.subject = subject; this.name = name; } public void SetName(string name){ this.name = name; } public string GetName(){ return this.name; } public void SetSubject(SubjectConcrete subject){ this.subject = subject; } public SubjectConcrete GetSubject(){ return this.subject; } public void DoUpdate() { Console.WriteLine(this.name+" membaca status Baru '"+this.subject.GetSubjectStatus()+"'"); } } }
PHP
<?php interface Subject{ function Add($index,$observer); function Remove($index); function Notivy(); } interface ObServer{ function DoUpdate(); } class SubjectConcrete implements Subject{ private $list_observer = Array(); private $SubjectStatus; function SetSubjectStatus($status){ $this->SubjectStatus = $status; } function GetSubjectStatus(){ return $this->SubjectStatus; } function Add($index,$observer) { $this->list_observer[$index] = $observer; } function Remove($index) { unset($this->list_observer[$index]); } function Notivy() { foreach($this->list_observer as $list){ $list->DoUpdate(); } } } class User implements ObServer{ private $name; private $subject; function __construct($subject,$name) { $this->subject = $subject; $this->name = $name; } function SetName($name){ $this->name = $name; } function GetName(){ return $this->name; } function SetSubject($subject){ $this->subject = $subject; } function GetSubject(){ return $this->subject; } function DoUpdate() { echo($this->name." membaca status Baru '".$this->subject->GetSubjectStatus()."'< br />"); } } $subject = new SubjectConcrete(); $subject->Add('Rizal',new User($subject, "Rizal")); $subject->Add('Afani',new User($subject, "Afani")); $subject->SetSubjectStatus("Selamat Pagi"); $subject->Notivy(); ?>
No comments:
Post a Comment