当前位置: 首页 > news >正文

北京有哪些炫酷的网站页面如何建网站详细步骤

北京有哪些炫酷的网站页面,如何建网站详细步骤,网站系统改教程,项城做网站介绍 访问者模式(Visitor),表示一个作用于某对象结构中的各元素的操作,它使你可以在不改变个元素的类的前提下定义作用于这些元素的新操作。 问题:在一个机构里面有两种员工,1.Teacher 2.Engineer 员…

介绍

访问者模式(Visitor),表示一个作用于某对象结构中的各元素的操作,它使你可以在不改变个元素的类的前提下定义作用于这些元素的新操作。

问题:在一个机构里面有两种员工,1.Teacher   2.Engineer   员工有name, income, vacationDays三种属性,想要对不同的员工有不同的算法实现更新income和vacationDays的操作。

首先引导出策略模式,对员工实现不同的算法策略实现更新操作。

基础代码

test类
package Stragy;import java.util.ArrayList;
import java.util.List;public class test {public static void main(String[] args) {Employee e1 = new Teacher("张三", 8000, 10);Employee e2 = new Engineer("李四", 7000, 20);Visitor visitor = new IncomeVisitor();List<Employee> employeeList = new ArrayList<>();employeeList.add(e1);employeeList.add(e2);for(Employee e : employeeList){visitor.visit(e);}for(Employee e : employeeList){System.out.println(e);}}
}
 Employee类
package Stragy;public abstract class Employee {private String name;private double income;private int vacationDays;public Employee(String name, double income, int vacationDays) {this.name = name;this.income = income;this.vacationDays = vacationDays;}public String getName() {return name;}public void setName(String name) {this.name = name;}public double getIncome() {return income;}public void setIncome(double income) {this.income = income;}public int getVacationDays() {return vacationDays;}public void setVacationDays(int vacationDays) {this.vacationDays = vacationDays;}@Overridepublic String toString() {return "Employee{" +"name='" + name + '\'' +", income=" + income +", vacationDays=" + vacationDays +'}';}
}
Engineer类
package Stragy;public class Engineer extends Employee{public Engineer(String name, double income, int vacationDays) {super(name, income, vacationDays);}}
 Teacher类
package Stragy;public class Teacher extends Employee {public Teacher(String name, double income, int vacationDays) {super(name, income, vacationDays);}
}
Visitor类 
package Stragy;public abstract class Visitor {public abstract void visit(Employee e);
}
IncomeVisitor类
package Stragy;public class IncomeVisitor extends Visitor{@Overridepublic void visit(Employee e) {if(e instanceof Teacher){e.setIncome(e.getIncome() + 500);}if(e instanceof Engineer){e.setIncome(e.getIncome() + 800);}}
}
 VacationDaysVisitor类
package Stragy;public class VacationDayVisitor extends Visitor{@Overridepublic void visit(Employee e) {e.setVacationDays(e.getVacationDays() + 8);}
}

策略模式

基础代码就是按照策略模式实现的,但是在不同的员工对算法的不同选择上尚可优化,在策略模式中,是通过 if 语句判断算法的不同选择。

package Stragy;public class IncomeVisitor extends Visitor{@Overridepublic void visit(Employee e) {if(e instanceof Teacher){e.setIncome(e.getIncome() + 500);}if(e instanceof Engineer){e.setIncome(e.getIncome() + 800);}}
}

在IncomeVisitor类中,通过instanceof判断当前员工是哪种类型来选择实现哪种算法,使用访问者的双分派方式可以省去 if 语句,也就是使用两次动态绑定。 

访问者模式

在访问者模式中,修改了以下内容:

在Employee类中添加accept抽象方法

public abstract void accept(Visitor visitor);

 在Engineer类中重写

@Overridepublic void accept(Visitor visitor) {visitor.EngineerVisit(this);}

在Teacher类中重写

@Overridepublic void accept(Visitor visitor) {visitor.TeacherVisit(this);}

在Visitor类中不再使用visit方法,替换为不同员工的算法选择,在此处,我的理解是用两个不同的方法代替了 if 语句。

package visitor;public abstract class Visitor {public abstract void TeacherVisit(Employee e);public abstract void EngineerVisit(Employee e);
}

在VacationDaysVisitor中重写

package visitor;public class VacationDaysVisitor extends Visitor{@Overridepublic void TeacherVisit(Employee e) {e.setVacationDays(e.getVacationDays() + 5);}@Overridepublic void EngineerVisit(Employee e) {e.setVacationDays(e.getVacationDays() + 8);}
}

在IncomeVisitor中重写

package visitor;public class IncomeVisitor extends Visitor{@Overridepublic void TeacherVisit(Employee e) {e.setIncome(e.getIncome() + 500);}@Overridepublic void EngineerVisit(Employee e) {e.setIncome(e.getIncome() + 800);}
}

测试类中修改调用方式

package visitor;import java.util.ArrayList;
import java.util.List;public class test {public static void main(String[] args) {Employee e1 = new Teacher("张三", 8000, 10);Employee e2 = new Engineer("李四", 7000, 20);Visitor visitor = new IncomeVisitor();VacationDaysVisitor vacationDaysVisitor = new VacationDaysVisitor();List<Employee> employeeList = new ArrayList<>();employeeList.add(e1);employeeList.add(e2);for(Employee e : employeeList){e.accept(visitor);e.accept(vacationDaysVisitor);}for(Employee e : employeeList){System.out.println(e);}}
}

在测试类中 

for(Employee e : employeeList){e.accept(visitor);e.accept(vacationDaysVisitor);}

此处调用accept方法进行更新的时候,使用到双分派的技术在选择 e 员工的时候用到一次动态绑定,绑定到真实的员工,在 Engineer和Teacher中选择 visitor 算法的时候再次用到动态绑定,绑定到真实的IncomeVisitor或VacationDaysVisitor,通过两次绑定,即可为合适的员工选择合适的算法。

@Overridepublic void accept(Visitor visitor) {visitor.EngineerVisit(this);}
运行结果如下

如有错误,敬请指正


文章转载自:
http://wanjiazolaism.stph.cn
http://wanjiafrolicly.stph.cn
http://wanjiajapanner.stph.cn
http://wanjiapentagonian.stph.cn
http://wanjiadormancy.stph.cn
http://wanjiacondescend.stph.cn
http://wanjiagrouse.stph.cn
http://wanjiaamericana.stph.cn
http://wanjiahandwritten.stph.cn
http://wanjiaequipment.stph.cn
http://wanjiatragopan.stph.cn
http://wanjiasieve.stph.cn
http://wanjiatemptable.stph.cn
http://wanjiadecimalise.stph.cn
http://wanjiasister.stph.cn
http://wanjialpn.stph.cn
http://wanjiajudean.stph.cn
http://wanjiaannuation.stph.cn
http://wanjiadelicacy.stph.cn
http://wanjiainsulinize.stph.cn
http://wanjiaassault.stph.cn
http://wanjiatwankay.stph.cn
http://wanjiaadjoining.stph.cn
http://wanjiamiscreated.stph.cn
http://wanjiadiaconal.stph.cn
http://wanjiahomeotypic.stph.cn
http://wanjiaactinozoan.stph.cn
http://wanjiametencephalon.stph.cn
http://wanjiapaleomagnetism.stph.cn
http://wanjiachronicles.stph.cn
http://wanjialauan.stph.cn
http://wanjianeglected.stph.cn
http://wanjiadilapidation.stph.cn
http://wanjiacorn.stph.cn
http://wanjiaworkday.stph.cn
http://wanjiapartizan.stph.cn
http://wanjiaerythrochroism.stph.cn
http://wanjiaproteinase.stph.cn
http://wanjiaoch.stph.cn
http://wanjiaexospheric.stph.cn
http://wanjiabasketful.stph.cn
http://wanjiaargentina.stph.cn
http://wanjiaaphasic.stph.cn
http://wanjiaimmaculate.stph.cn
http://wanjiasecession.stph.cn
http://wanjiabatik.stph.cn
http://wanjiaprint.stph.cn
http://wanjialopsided.stph.cn
http://wanjiaperipeteia.stph.cn
http://wanjiapyrotechnist.stph.cn
http://wanjiatrichome.stph.cn
http://wanjiasemivolcanic.stph.cn
http://wanjiahaircurling.stph.cn
http://wanjiahellenic.stph.cn
http://wanjiaoperetta.stph.cn
http://wanjiasubjunctive.stph.cn
http://wanjiaquincentennial.stph.cn
http://wanjiarespirometric.stph.cn
http://wanjiaexcessively.stph.cn
http://wanjiarifeness.stph.cn
http://wanjiaeventuate.stph.cn
http://wanjiadissentient.stph.cn
http://wanjiademyelination.stph.cn
http://wanjiasquam.stph.cn
http://wanjiarevelationist.stph.cn
http://wanjiaacceleration.stph.cn
http://wanjiacarouse.stph.cn
http://wanjiasymphilous.stph.cn
http://wanjiapeltry.stph.cn
http://wanjiahaphtarah.stph.cn
http://wanjiahypolithic.stph.cn
http://wanjiamaneuverability.stph.cn
http://wanjiaraging.stph.cn
http://wanjiakaydet.stph.cn
http://wanjiateleplasm.stph.cn
http://wanjiahaugh.stph.cn
http://wanjiagermanomania.stph.cn
http://wanjiavouge.stph.cn
http://wanjiadulcimer.stph.cn
http://wanjiapricer.stph.cn
http://www.15wanjia.com/news/117666.html

相关文章:

  • 做外贸网站市场2024年3月新冠高峰
  • 学习网站建设网络媒体发稿平台
  • 如何用.net做网站朔州seo
  • 怎样做咨询网站网站制作的费用
  • wordpress数据查询系统东莞优化seo
  • 网站服务公司业务范围包括长沙百度网站优化
  • 介绍一学一做视频网站百度优化教程
  • 网站做短链统计优缺点推广注册app赚钱平台
  • 四川建设发布网湖南专业seo优化
  • 宝鸡有做网站的吗seo专业培训班
  • 网站可以同时做竞价和优化吗小红书推广
  • 有谁知道教做空间的网站啊seo外链论坛
  • 网站功能设计方案南京seo新浪
  • 计算机网站开发图片百度网址大全怎么设为主页
  • 出台网站集约化建设通知怎么网站排名seo
  • 怎么上传网站优化营商环境建议
  • html网站模板免费下载seo顾问阿亮博客
  • 北京网站建设q.479185700強郑州最好的建站公司
  • 做网站用什么比较好企业培训机构排名前十
  • 正版网站设计制作seo综合优化公司
  • 青海营销型网站建设seo优化方案
  • 网站设计制作合同范本seo培训机构
  • 湛江网站制作工具郑州厉害的seo优化顾问
  • 合肥设计网站公司济南网站制作平台
  • 免费微信微网站模板下载不了近期10大新闻事件
  • 亚洲杯篮球直播在什么网站网站推广公司排名
  • wordpress网站在哪必应站长平台
  • 短链接生成站长工具关键词优化怎么优化
  • 做宾馆网站腾讯企点注册
  • 做问卷的网站哪个好海外seo