06 设计模式教学课件

上传人:n**** 文档编号:80110233 上传时间:2019-02-18 格式:PPT 页数:47 大小:294KB
返回 下载 相关 举报
06 设计模式教学课件_第1页
第1页 / 共47页
06 设计模式教学课件_第2页
第2页 / 共47页
06 设计模式教学课件_第3页
第3页 / 共47页
06 设计模式教学课件_第4页
第4页 / 共47页
06 设计模式教学课件_第5页
第5页 / 共47页
点击查看更多>>
资源描述

《06 设计模式教学课件》由会员分享,可在线阅读,更多相关《06 设计模式教学课件(47页珍藏版)》请在金锄头文库上搜索。

1、装饰(Decorator)模式,循序渐进,从穿衣谈起: 写一个可以给人搭配不同服饰的程序,循序渐进,class Person private string name; public Person(string name) this.name = name; public void WearTShirts() Console.Write(“大T恤 “); ,public void WearBigTrouser() Console.Write(“垮裤 “); public void WearSneakers() Console.Write(“破球鞋 “); ,public void WearSui

2、t() Console.Write(“西装 “); public void WearTie() Console.Write(“领带 “); ,public void WearLeatherShoes() Console.Write(“皮鞋 “); public void Show() Console.WriteLine(“装扮的0“, name); ,客户端,static void Main(string args) Person xc = new Person(“小菜“); Console.WriteLine(“n第一种装扮:“); xc.WearTShirts(); xc.WearBigT

3、rouser(); xc.WearSneakers(); xc.Show();,Console.WriteLine(“n第二种装扮:“); xc.WearSuit(); xc.WearTie(); xc.WearLeatherShoes(); xc.Show(); Console.Read(); ,缺点,若要增加其它装扮,需要修改Person类,违背了开放封闭原则 试着将这些服饰写成子类,Person类,class Person private string name; public Person(string name) this.name = name; public void Show(

4、) Console.WriteLine(“装扮的0“, name); ,抽象类Finery,abstract class Finery public abstract void Show(); ,class TShirts : Finery public override void Show() Console.Write(“大T恤 “); ,class BigTrouser : Finery public override void Show() Console.Write(“垮裤 “); ,。,客户端,static void Main(string args) Person xc = ne

5、w Person(“小菜“); Console.WriteLine(“n第一种装扮:“); Finery dtx = new TShirts(); Finery kk = new BigTrouser(); Finery pqx = new Sneakers(); dtx.Show(); kk.Show(); pqx.Show(); xc.Show();,Console.WriteLine(“n第二种装扮:“); Finery xz = new Suit(); Finery ld = new Tie(); Finery px = new LeatherShoes(); xz.Show(); l

6、d.Show(); px.Show(); xc.Show(); Console.Read(); ,试着将装饰在内部组装完毕,Decorator,装饰(Decorator)模式又名包装(Wrapper)模式GOF95。 装饰模式以对客户端透明的方式扩展对象的功能,是继承关系的一个替代方案。,abstract class Component public abstract void Show(); ,class Person: Component public Person() private string name; public Person(string name) this.name =

7、name; public override void Show() Console.WriteLine(“装扮的0“, name); ,class Finery : Component protected Component component; public void Decorate(Component component) ponent = component; public override void Show() if (component != null) component.Show(); ,class TShirts : Finery public override void

8、Show() Console.Write(“大T恤 “); base.Show(); class BigTrouser : Finery public override void Show() Console.Write(“垮裤 “); base.Show(); ,class Sneakers : Finery public override void Show() Console.Write(“破球鞋 “); base.Show(); class Suit : Finery public override void Show() Console.Write(“西装 “); base.Show

9、(); ,class Tie : Finery public override void Show() Console.Write(“领带 “); base.Show(); class LeatherShoes : Finery public override void Show() Console.Write(“皮鞋 “); base.Show(); ,static void main(string args) Person xc = new Person(“小菜“); Console.WriteLine(“n第一种装扮:“); Sneakers pqx = new Sneakers();

10、BigTrouser kk = new BigTrouser(); TShirts dtx = new TShirts(); pqx.Decorate(xc); kk.Decorate(pqx); dtx.Decorate(kk); dtx.Show();,Console.WriteLine(“n第二种装扮:“); LeatherShoes px = new LeatherShoes(); Tie ld = new Tie(); Suit xz = new Suit(); px.Decorate(xc); ld.Decorate(px); xz.Decorate(ld); xz.Show();

11、,Console.WriteLine(“n第三种装扮:“); Sneakers pqx2 = new Sneakers(); LeatherShoes px2 = new LeatherShoes(); BigTrouser kk2 = new BigTrouser(); Tie ld2 = new Tie(); pqx2.Decorate(xc); px2.Decorate(pqx2); kk2.Decorate(px2); ld2.Decorate(kk2); ld2.Show(); Console.Read(); ,另一种写法,abstract class Component publi

12、c abstract void Show(); ,class Person: Component public Person() private string name; public Person(string name) this.name = name; public override void Show() Console.WriteLine(“装扮的0“, name); ,class Finery : Component protected Component component; public void Finery(Component component) ponent = co

13、mponent; public override void Show() if (component != null) component.Show(); ,class TShirts : Finery public TShirts(Component component):base(component) public override void Show() Console.Write(“大T恤 “); base.Show(); ,static void main(string args) Component xc = new Person(“小菜“); Console.WriteLine(

14、“n第一种装扮:“); Finery pqx = new Sneakers(xc); Finery kk = new BigTrouser(pqx); Finery dtx = new TShirts(kk); dtx.Show(); ,上面的代码在执行装饰时是通过Decorate方法实现的 装饰模式常常被称为包裹模式,就是因为每一个具体装饰类都将下一个具体装饰类或者具体构件类包裹起来。,抽象构件(Component)角色:给出一个抽象接口,以规范准备接收附加责任的对象。 具体构件(Concrete Component)角色:实现组件对象接口,通常就是被装饰器装饰的对象。 装饰(Decorat

15、or)角色:持有一个构件(Component)对象的实例,并定义一个与抽象构件接口一致的接口。 具体装饰(Concrete Decorator)角色:负责给构件对象“贴上“附加的责任。,装饰模式为对象添加额外责任的方式就像做蛋糕一样,一圈一圈的加上去,中间的面包是核心,是被装饰的对象(Person),是核心任务,外围的都是装饰对象。 这就是说装饰模式包含两部分内容,即装饰对象和被装饰对象。,按照GOF的说法,Decorator模式的意图是:动态的给一个对象添加一些额外的职责。就增加功能来说,Decorator模式相比生成子类更为灵活。 这个被装饰的对象不一定是最原始的那个对象,也可能是被其他装饰器装饰过的对象,反正都是实现的同一接口,也就是同一类型。 Decorator模式的效果是:让我们可以创建以decorator对象-负责新的功能的对象-开始的一条对象“链”,并结束于最初的对象。,Decorator 链,继承和多态,装饰模式耍了这么绕,其实就用了两招,继承和多态。 所有组件都是Component 的子类,由Component 规划对象的行为(如,public void show();),装饰类Decorator 与 被装饰类P

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 中学教育 > 其它中学文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号