260 likes | 380 Views
Decorator Pattern. - Another Example of a Structural Pattern Alternative way of adding Functionality to an existing class (alternative to a derived class) Favors Composition over Inheritance Often referred to as a Wrapper Advantage: No need for several levels of Inheritence
E N D
Decorator Pattern - Another Example of a Structural Pattern Alternative way of adding Functionality to an existing class (alternative to a derived class) Favors Composition over Inheritance Often referred to as a Wrapper Advantage: No need for several levels of Inheritence (reduces Complexity) Nov 2005 MSc Slide
Decorator Pattern Instead of: X Y Z p = new Z(); Z X Y Z Z p=new Z(new Y(new X())); Can add functionality by adding more Objects Nov 2005 MSc Slide
Decorator Pattern Typically used to change appearance of a Visual Component One big advantage is that you can dynamically change appearance/functionality of a component Nov 2005 MSc Slide
Decorator Pattern Want to decorate a Button Nov 2005 MSc Slide
using System; using System.Windows.Forms; using System.Drawing; abstract class Decorator : Panel { protected Control ctrl; public Decorator(Control c) { this.Size=c.Size; ctrl=c; Controls.Add(c); c.Paint += new PaintEventHandler( paint); } virtual public void paint(object sender, PaintEventArgs pe){} } Nov 2005 MSc Slide
class SlashDecorator : Decorator { public SlashDecorator(Control c):base(c) {} override public void paint(object sender, PaintEventArgs pe){ Pen aPen = new Pen(Color.Black , 1); Graphics g = pe.Graphics; int x2 = this.Size.Width; int y2 = this.Size.Height; g.DrawLine(aPen,0,0,x2,y2); } } Nov 2005 MSc Slide
class GFrame:Form { private Button cbutton=new Button(); private Button dbutton=new Button(); private SlashDecorator d; public GFrame():base(){ cbutton.Text="cbutton"; dbutton.Text="dbutton"; d=new SlashDecorator(dbutton); cbutton.SetBounds(10,10,cbutton.Size.Width, cbutton.Size.Height); d.SetBounds(100,10,d.Size.Width,d.Size.Height); Controls.Add(cbutton); Controls.Add(d); }} Nov 2005 MSc Slide
public class Test92{ public static void Main(string[] args){ Application.Run(new GFrame()); } } Nov 2005 MSc Slide
Example 2: This time we have a Decorated Label Nov 2005 MSc Slide
using System; using System.Windows.Forms; using System.Drawing; abstract class Decorator : Panel { protected Control ctrl; public Decorator(Control c) { this.Size=c.Size; ctrl=c; Controls.Add(c); c.Paint += new PaintEventHandler( paint); } virtual public void paint(object sender, PaintEventArgs pe){} } Nov 2005 MSc Slide As before
class CoolDecorator : Decorator { public CoolDecorator(Control c):base(c) { } override public void paint(object sender, PaintEventArgs pe){ Pen aPen = new Pen(Color.Red , 2); Graphics g = pe.Graphics; int x2 = this.Size.Width; int y2 = this.Size.Height; g.DrawRectangle(aPen,0,0,x2,y2); } } Draw a Rectangle Nov 2005 MSc Slide
class GFrame:Form { private Label l=new Label(); private CoolDecorator c; public GFrame():base(){ l.Text="MyLabel"; c=new CoolDecorator(l); c.SetBounds(10,10,c.Size.Width, c.Size.Height-5); Controls.Add(c); } } Nov 2005 MSc Slide
public class Test92{ public static void Main(string[] args){ Application.Run(new GFrame()); } } Nov 2005 MSc Slide
Now to add Dynamic Behaviour, change Colour to Blue Nov 2005 MSc Slide
class CoolDecorator : Decorator { bool mouse_over=false; public CoolDecorator(Control c):base(c) { EventHandler evh = new EventHandler(mouseEnter); ctrl.MouseEnter+= evh; ctrl.MouseLeave += new EventHandler(mouseLeave); } public void mouseEnter(object sender, EventArgs e){ mouse_over = true; ctrl.Refresh (); } public void mouseLeave(object sender, EventArgs e){ mouse_over = false; this.Refresh (); } Nov 2005 MSc Slide
override public void paint(object sender, PaintEventArgs pe){ Pen aPen = new Pen(Color.Red , 2); Pen bPen = new Pen(Color.Blue , 2); Graphics g = pe.Graphics; int x2 = this.Size.Width; int y2 = this.Size.Height; g.DrawRectangle(aPen,0,0,x2,y2); if (mouse_over == true){ g.DrawRectangle(bPen,0,0,x2,y2);} } } Nov 2005 MSc Slide
UML Class Diagram Control Decorator Control SlashDecorator CoolDecorator GFrame Panel Nov 2005 MSc Slide
Big Advantage of Decorator(Wrapper) is that you can add functionality by adding Objects Label lab=new Label(); lab.Text="Example"; SlashDecorator c=new SlashDecorator(lab); Controls.Add(c); Nov 2005 MSc Slide
Combining Decorators Normally: c=new CoolDecorator(b); c=new SlashDecorator(new CoolDecorator(b)); Want: Nov 2005 MSc Slide
Combining Decorators In practice new a ref to parent and underlying control: c=new CoolDecorator(null, b); Normal Wrapper: Embedded Wrapper: sd=new SlashDecorator(new CoolDecorator(null,b),b); Nov 2005 MSc Slide
abstract class Decorator : Panel { protected Control ctrl; public Decorator(Control c) { this.Size=c.Size; ctrl=c; Controls.Add(c); c.Paint += new PaintEventHandler( paint); } virtual public void paint(object sender, PaintEventArgs pe){} } As before Nov 2005 MSc Slide
class CoolDecorator : Decorator { bool mouse_over=false; SlashDecorator sdec; public CoolDecorator(SlashDecorator sdec,Control c) :base(c) { this.sdec = sdec; if (sdec !=null) c.Paint += new PaintEventHandler( sdec.paint); EventHandler evh = new EventHandler(mouseEnter); ctrl.MouseEnter+= evh; MouseEventHandler(mouseMove); ctrl.MouseLeave += new EventHandler(mouseLeave); } override public void paint(object sender, … Nov 2005 MSc Slide As before
class SlashDecorator : Decorator { private CoolDecorator cd=null; public SlashDecorator(CoolDecorator cd,Control c) :base(c) { this.cd=cd; if (cd !=null) c.Paint += new PaintEventHandler( cd.paint); } override public void paint(object sender, … As before Nov 2005 MSc Slide
class GFrame:Form { private Label l=new Label(); private SlashDecorator sd; public GFrame():base(){ l.Text="MyLabel"; sd=new SlashDecorator(new CoolDecorator(null,l),l); sd.SetBounds(100,10,sd.Size.Width,sd.Size.Height); Controls.Add(sd); this.Refresh();} Nov 2005 MSc Slide
Exercise 0: Modify the first Decorator (test0.java) so its in the following format Nov 2005 MSc Slide
Exercise 1: Modify Exercise 0 to include some dynamic Behaviour Red X Nov 2005 MSc Slide