Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dec 2005 MSc Slide 1 Pattern that are most specifically concerned with communication between objects Cat 3: Behavioral Patterns.

Similar presentations


Presentation on theme: "Dec 2005 MSc Slide 1 Pattern that are most specifically concerned with communication between objects Cat 3: Behavioral Patterns."— Presentation transcript:

1 Dec 2005 MSc Slide 1 Pattern that are most specifically concerned with communication between objects Cat 3: Behavioral Patterns

2 Dec 2005 MSc Slide 2 Pattern that are most specifically concerned with communication between objects Cat 3: Behavioral Patterns

3 Dec 2005 MSc Slide 3 - How multiple Objects can be notified of a change - Two Independent, Loosely coupled Objects changed in harmony with each other Data and Views seperated Views my subscribe to receive notice of Data updates Often referred to as: Model - View - Controller Observer Pattern

4 Dec 2005 MSc Slide 4 Data void change(){ } View1 void update(){ } > Controller setChanged() notifyObservers() addOserver(view) Update()

5 Dec 2005 MSc Slide 5 Observable +addObserver() +notifyObservers() #setChanged() > Observer +update()

6 Dec 2005 MSc Slide 6 Example 1

7 Dec 2005 MSc Slide 7 If value incremented up to 10

8 Dec 2005 MSc Slide 8 using System; using System.Drawing; using System.Collections; using System.Windows.Forms; Namespaces to

9 Dec 2005 MSc Slide 9 class Counter : Observable{ private int value; public Counter(int v){ value=v;} public void increment(){ value++; setChanged(); notifyObservers("valueChanged");} public void decrement(){ value--; setChanged(); notifyObservers("valueChanged");} public int getValue(){ return value;} } Model(Subject)

10 Dec 2005 MSc Slide 10 public class Lab : Form, Observer{ private Label jl=new Label(); public Lab():base(){ jl.Text="Value= 1"; this.Text=" Label Observer"; this.Size=new Size(150,150); Controls.Add(jl); jl.SetBounds(10,10,jl.Size.Width,jl.Size.Height); this.Refresh();} : View

11 Dec 2005 MSc Slide 11 : public void update(Observable o,Object arg){ int v=((Counter)o).getValue(); jl.Text= "Value= "+v+" "; Refresh();} } View

12 public class Alarm : Form, Observer{ private Label jl=new Label(); private int limit; public Alarm(int l):base(){ limit=l; this.Size=new Size(150,150); this.Text=" Alarm Observer"; jl.Text="No Alarm"; Controls.Add(jl); jl.SetBounds(10,10,jl.Size.Width,jl.Size.Height); this.Refresh();} : View2

13 : public void update(Observable o,Object arg){ int v=((Counter)o).getValue(); if (v>=limit) jl.Text= "Limit Reached"; else jl.Text ="No Alarm ";} } View2

14 class Obs1 : Form{ private Label jl1=new Label(); private Label jl2=new Label(); private Button jb1=new Button(); private Button jb2=new Button(); Counter count=new Counter(1); : Controller

15 public Obs1():base(){ this.Text="Observer Test"; jl1.Text="Observer Test"; jl2.Text=" Value = 1"; jb1.Text="Increment"; jb2.Text="Decrement"; Controls.Add(jl1);Controls.Add(jl2); Controls.Add(jb1);Controls.Add(jb2); jl1.SetBounds(10,10,jl1.Size.Width,jl1.Size.Height); jl2.SetBounds(20,40,jl2.Size.Width,jl2.Size.Height); jb1.SetBounds(10,70,jb1.Size.Width,jb1.Size.Height); jb2.SetBounds(10,100,jb2.Size.Width,jb2.Size.Height); :

16 jb1.Click += new EventHandler(this.jb1_Click); jb2.Click += new EventHandler(this.jb2_Click); Lab l=new Lab(); Alarm a=new Alarm(10); count.addObserver(l); count.addObserver(a); l.Show();a.Show(); Refresh(); }

17 private void jb1_Click(object sender, EventArgs e){ count.increment(); int v=count.getValue(); jl2.Text=" Value = "+v; } private void jb2_Click(object sender, EventArgs e){ count.decrement(); int v=count.getValue(); jl2.Text = " Value = "+v; } } Controller

18 public class Test92{ public static void Main(string[] args){ Application.Run(new Obs1()); } Start Application

19 Dec 2005 MSc Slide 19 Observer > Observable Form Obj1 Alarm Counter Lab

20 Dec 2005 MSc Slide 20

21

22 Dec 2005 MSc Slide 22 Observer interface and Observable class are predefined in Java In C# we must write these ourselves

23 Dec 2005 MSc Slide 23 public interface Observer{ void update(Observable o, Object arg);} Observer

24 Dec 2005 MSc Slide 24 public class Observable{ private ArrayList list=new ArrayList(); private bool flag=false; public void addObserver(Observer o){ list.Add(o);} public void setChanged(){flag=true;} public void notifyObservers(Object msg){ if (flag==true){ flag=false; foreach(Observer ob in list){ ob.update(this,msg);} }// end if }} Observable

25 Dec 2005 MSc Slide 25 This implementation is similar to the Java implementation Event Handling is an example of the Observer Pattern In C# Event Handling uses Delegates instead of addObserver Therefore in Appendix 1 we have the same example done again using delegates

26 Dec 2005 MSc Slide 26 Observer is an interface but Observable is a class which must be extended We can’t extend 2 classes but can use Composition See next Example

27 Dec 2005 MSc Slide 27 Example 2

28 Dec 2005 MSc Slide 28 After Clicking “Red”

29 Dec 2005 MSc Slide 29 class ColorFrame : Form, Observer{ public ColorFrame():base(){ this.Text=" Color Observer "; this.BackColor=Color.White; this.Size=new Size(200,100); this.Show(); } public void update(Observable o,Object arg){ if (arg.Equals("Red")) this.BackColor=Color.Red; if (arg.Equals("Green")) this.BackColor=Color.Green; if (arg.Equals("Blue")) this.BackColor=Color.Blue; Refresh();} }

30 class ColorName : Form, Observer{ private Label jl=new Label(); private Font f=new Font("Times New Roman",18, FontStyle.Bold); public ColorName():base(){ this.Text=" Color Name "; jl.Text=" White "; Controls.Add(jl); jl.Font=f; jl.SetBounds(20,20,jl.Size.Width,jl.Size.Height); this.Size=new Size(200,100); this.Show(); } :

31 public void update(Observable o,Object arg){ if (arg.Equals("Red")) jl.Text=" Red "; if (arg.Equals("Green")) jl.Text=" Green "; if (arg.Equals("Blue")) jl.Text = " Blue "; Refresh();}

32 class Obs2 : Form { private Label jl1=new Label(); private Button jb1=new Button(); private Button jb2=new Button(); private Button jb3=new Button(); private Observable o=new Observable(); private Font f=new Font("Times New Roman", 16,FontStyle.Bold); ColorFrame cf; ColorName cn; :

33 public Obs2():base(){ this.Text="Observer Test"; jl1.Font=f; jl1.Text="Observer Test"; jb1.Text="Red"; jb2.Text="Blue"; jb3.Text="Green"; Controls.Add(jl1); Controls.Add(jb1); Controls.Add(jb2); Controls.Add(jb3); jl1.SetBounds(20,20,jl1.Size.Width,jl1.Size.Height); jb1.SetBounds(30,60,jb1.Size.Width,jb1.Size.Height); jb2.SetBounds(30,100,jb2.Size.Width,jb2.Size.Height); jb3.SetBounds(30,140,jb3.Size.Width,jb3.Size.Height); :

34 jb1.Click += new EventHandler(this.jb1_Click); jb2.Click += new EventHandler(this.jb2_Click); jb3.Click += new EventHandler(this.jb3_Click); cf=new ColorFrame(); cn=new ColorName(); o.addObserver(cf); o.addObserver(cn); this.Size=new Size(100,230); Refresh();}

35 private void jb1_Click(object sender, EventArgs e){ o.setChanged(); o.notifyObservers("Red");} private void jb2_Click(object sender, EventArgs e){ o.setChanged(); o.notifyObservers("Blue");} private void jb3_Click(object sender, EventArgs e){ o.setChanged(); o.notifyObservers("Green");} }

36 public class Test92{ public static void Main(string[] args){ Application.Run(new Obs2()); }

37 Dec 2005 MSc Slide 37 Controller View Data Model Model-View Controller Event Handling in Java

38 Dec 2005 MSc Slide 38

39 Dec 2005 MSc Slide 39 class Value{ private int val; public Value(int v){val=v;} public void increment(){val++;} public int readval(){ return val;} } Data Model

40 Dec 2005 MSc Slide 40 class GFrame:Form { private Label jl1=new Label(); private Button jb1=new Button(); private Value v=new Value(1); public GFrame():base(){ jl1.Text=" Value = 1"; jb1.Text="Next"; jl1.SetBounds(10,10,jl1.Size.Width,jl1.Size.Height); jb1.SetBounds(10,50,jl1.Size.Width-15,jl1.Size.Height); Controls.Add(jl1); Controls.Add(jb1); EHandler eh=new EHandler(this); jb1.Click += new EventHandler(eh.jb1_Click); Refresh();} View

41 Dec 2005 MSc Slide 41 : public void setLabel(){ jl1.Text=" Value = "+v.readval(); } public void incrValue(){ v.increment(); } View continued

42 Dec 2005 MSc Slide 42 class EHandler{ private GFrame gf; public EHandler(GFrame g){gf=g;} public void jb1_Click(object sender, EventArgs e){ gf.incrValue(); gf.setLabel();} } public class Test92{ public static void Main(string[] args){ Application.Run(new GFrame()); } Controller Inner Class

43 Dec 2005 MSc Slide 43 Therefore Event Handling is an example of the Observer Pattern In C# Event Handling uses Delegates instead of addObserver Therefore in Appendix 1 we have the same example done again using delegates

44 Dec 2005 MSc Slide 44 Ex 1: Modify Counter application so it has a 3rd Observer

45 Dec 2005 MSc Slide 45 Ex1 continued If a value is decremented below 0 an alarm is raised

46 Dec 2005 MSc Slide 46 Ex2: Modify the Color Observable Class so the Input is via a TextField

47 Dec 2005 MSc Slide 47 Appendix 1 First Example Completely rewritten Using Delegates

48 Dec 2005 MSc Slide 48 Example 1

49 Dec 2005 MSc Slide 49 public delegate void ObserverHandler(Observable o,Object arg); public class Observable{ private ObserverHandler list=null; private bool flag=false; public ObserverHandler LIST{ get { return list;} set { list=value;} // ‘value’ a keyword } protected void setChanged(){flag=true;} public void notifyObservers(Object msg){ if (flag==true){ flag=false; list(this,msg);} }}

50 Dec 2005 MSc Slide 50 public interface Observer{ void update(Observable o, Object arg);} No Change

51 Dec 2005 MSc Slide 51 class Counter : Observable{ private int value; public Counter(int v){ value=v;} public void increment(){ value++; setChanged(); notifyObservers("valueChanged");} public void decrement(){ value--; setChanged(); notifyObservers("valueChanged");} public int getValue(){ return value;} } No Change

52 Dec 2005 MSc Slide 52 public class Lab : Form, Observer{ private Label jl=new Label(); public Lab():base(){ jl.Text="Value= 1"; this.Text=" Label Observer"; this.Size=new Size(200,200); Controls.Add(jl); jl.SetBounds(10,50,jl.Size.Width,jl.Size.Height); this.Refresh();} public void update(Observable o,Object arg){ int v=((Counter)o).getValue(); jl.Text= "Value= "+v+" "; Refresh(); } No Change

53 Dec 2005 MSc Slide 53 class Obs1 : Form{ private Label jl1=new Label(); : jb1.Click += new EventHandler(this.jb1_Click); jb2.Click += new EventHandler(this.jb2_Click); Lab l=new Lab(); Alarm a=new Alarm(10); count.LIST+= new ObserverHandler(l.update); count.LIST+= new ObserverHandler(a.update); l.Show();a.Show(); Refresh(); }

54 Dec 2005 MSc Slide 54 : jb1.Click += new EventHandler(this.jb1_Click); jb2.Click += new EventHandler(this.jb2_Click); Lab l=new Lab(); Alarm a=new Alarm(10); count.addObserver(l); count.addObserver(a); l.Show();a.Show(); Refresh(); } As opposed to


Download ppt "Dec 2005 MSc Slide 1 Pattern that are most specifically concerned with communication between objects Cat 3: Behavioral Patterns."

Similar presentations


Ads by Google