设计模式访问者模式Visitor.pptx
《设计模式访问者模式Visitor.pptx》由会员分享,可在线阅读,更多相关《设计模式访问者模式Visitor.pptx(32页珍藏版)》请在三一办公上搜索。
1、设计模式(Design Pattern),张凯 副教授计算机学院 软件工程系,问题(Problem),学校有教师和工程师,教师 Teacher,工程师 Engineering,问题(Problem),学校有教师和工程师,问题(Problem),abstract class Employee private string name;private double income;private int vacationDays;public string Name get return name;set name=value;public double Income get return incom
2、e;set income=value;public int VacationDays get return vacationDays;set vacationDays=value;,问题(Problem),class Teacher:Employee public Teacher(string name,double income,int vacationDays)Name=name;Income=income;VacationDays=vacationDays;,class Engineer:Employee public Engineer(string name,double income
3、,int vacationDays)Name=name;Income=income;VacationDays=vacationDays;,问题(Problem),static void Main(string args)Teacher zhangsan=new Teacher(张三,2500.0,14);Engineer lisi=new Engineer(李四,3500.0,16);Console.WriteLine(zhangsan.Name+的收入是+zhangsan.Income+,假期是+zhangsan.VacationDays);Console.WriteLine(lisi.Na
4、me+的收入是+lisi.Income+,假期是+lisi.VacationDays);Console.ReadKey();,如何批量给员工加薪,加假期?,主要内容,访问者模式(Visitor),模式动机模式名称:访问者模式(Visitor)对于系统中的某些对象,它们存储在同一个集合中,且具有不同的类型,而且对于该集合中的对象,可以接受一类称为访问者的对象来访问,而且不同的访问者其访问方式有所不同,访问者模式为解决这类问题而诞生。,访问者模式(Visitor),模式动机在实际使用时,对同一集合对象的操作并不是唯一的,对相同的元素对象可能存在多种不同的操作方式。而且这些操作方式并不稳定,可能还需
5、要增加新的操作,以满足新的业务需求。此时,访问者模式就是一个值得考虑的解决方案。,访问者模式(Visitor),模式定义 访问者模式(Visitor Pattern):表示一个作用于某对象结构中的各元素的操作。它使我们可以在不改变各元素的类的前提下定义作用于这些元素的新操作。访问者模式是一种对象行为型模式。,访问者模式(Visitor),模式结构,访问者模式(Visitor),参与者 Vistor:抽象访问者 ConcreteVisitor:具体访问者 Element:抽象元素 ConcreteElement:具体元素 ObjectStructure:对象结构,访问者模式(Visitor),问
6、题(Problem),abstract class Employee private string name;private double income;private int vacationDays;public string Name get return name;set name=value;public double Income get return income;set income=value;public int VacationDays get return vacationDays;set vacationDays=value;abstract public void
7、Accept(Visitor visitor);,问题(Problem),class Teacher:Employee public Teacher(string name,double income,int vacationDays)Name=name;Income=income;VacationDays=vacationDays;public override void Accept(Visitor visitor)visitor.Visit(this);,class Engineer:Employee public Engineer(string name,double income,i
8、nt vacationDays)Name=name;Income=income;VacationDays=vacationDays;public override void Accept(Visitor visitor)visitor.Visit(this);,访问者模式(Visitor),abstract class Visitor abstract public void Visit(Employee element);class IncomeVisitor:Visitor public override void Visit(Employee element)element.Income
9、*=1.10;Console.WriteLine(element.Name+的新工资是:+element.Income);class VacationVisitor:Visitor public override void Visit(Employee element)element.VacationDays+=3;Console.WriteLine(element.Name+的新假期是+element.VacationDays);,访问者模式(Visitor),class Employees private ArrayList employees=new ArrayList();public
10、 void Attach(Employee employee)employees.Add(employee);public void Detach(Employee employee)employees.Remove(employee);public void Accept(Visitor visitor)foreach(Employee e in employees)e.Accept(visitor);,访问者模式(Visitor),static void Main(string args)Teacher zhangsan=new Teacher(张三,2500.0,14);Engineer
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 设计 模式 访问者 Visitor

链接地址:https://www.31ppt.com/p-6607377.html