java设计模式之访问者模式一

来源:互联网 发布:手机淘宝链接转换 编辑:程序博客网 时间:2024/05/20 02:22

访问者模式是行为性模式的一种,可以方便我们遍历不同的类型,执行不同的操作,下面要演示的程序循环打印学生的信息

public abstract class Student {/** * 考试分数 */private int score;private String name;private int sex;public int getScore() {return score;}public void setScore(int score) {this.score = score;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getSex() {return sex;}public void setSex(int sex) {this.sex = sex;}public void prints(){String info ="姓名:"+this.name;info += "性别:"+(this.sex==0?"男":"女");info += "分数" +this.score;info += this.otherinfos();System.out.println(info);}protected abstract String otherinfos();}

public class Mstudent extends Student {/** * 男同学 */@Overrideprotected String otherinfos() {// TODO Auto-generated method stubreturn "家庭住址";}}

public class Fstudent extends Student {/** * 女同学 */@Overrideprotected String otherinfos() {// TODO Auto-generated method stubreturn "家庭住址";}}

public class test {public static void main(String[] args) {List<Student> ls = new ArrayList<Student>();Student s = new Mstudent();s.setName("张三");s.setScore(60);s.setSex(0);ls.add(s);Student ss = new Fstudent();ss.setName("小花");ss.setScore(90);ss.setSex(1);ls.add(ss);for (Student t:ls) {t.prints();}}}

上面的程序会输出如下内容

姓名:张三性别:男分数60家庭住址姓名:小花性别:女分数90家庭住址

这可能是我们一时想要的输出结果,现在我们的要求变了,我只要输出姓名分数和家庭住址,不要性别了,类似的我们可能会根据需求频繁的修改输出的信息(这里的内容少还看不出来),修改student类肯定不是好的设计(根据开闭原则),于是就可以利用访问者模式

public abstract class Student {/** * 考试分数 */private int score;private String name;private int sex;public int getScore() {return score;}public void setScore(int score) {this.score = score;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getSex() {return sex;}public void setSex(int sex) {this.sex = sex;}/*public void prints(){String info ="姓名:"+this.name;info += "性别:"+(this.sex==0?"男":"女");info += "分数" +this.score;info += this.otherinfos();System.out.println(info);}*/public abstract void accept(IVisitor v);protected abstract String otherinfos();}

public class Fstudent extends Student {/** * 女同学 */@Overrideprotected String otherinfos() {// TODO Auto-generated method stubreturn "家庭住址";}@Overridepublic void accept(IVisitor v) {// TODO Auto-generated method stubv.visit(this);}}

public class Mstudent extends Student {/** * 男同学 */@Overrideprotected String otherinfos() {// TODO Auto-generated method stubreturn "家庭住址";}@Overridepublic void accept(IVisitor v) {// TODO Auto-generated method stubv.visit(this);}}

public interface IVisitor {public void visit(Fstudent fs); public void visit(Mstudent ms); }

public class Visitor implements IVisitor {@Overridepublic void visit(Fstudent fs) {String info ="姓名:"+fs.getName();info += "性别:"+(fs.getSex()==0?"男":"女");info += "分数" +fs.getScore();info += fs.otherinfos();System.out.println(info);}@Overridepublic void visit(Mstudent fs) {String info ="姓名:"+fs.getName();info += "性别:"+(fs.getSex()==0?"男":"女");info += "分数" +fs.getScore();info += fs.otherinfos();System.out.println(info);}}

public class test {public static void main(String[] args) {List<Student> ls = new ArrayList<Student>();Student s = new Mstudent();s.setName("张三");s.setScore(60);s.setSex(0);ls.add(s);Student ss = new Fstudent();ss.setName("小花");ss.setScore(90);ss.setSex(1);ls.add(ss);for (Student t:ls) {t.accept(new Visitor());;}}}

输出的结果和我们第一种实现是一样的,但是使用访问者模式,在需要改变输出的时候,我们不用去修改student机器子类,只要在实现一个实现IVisitor接口的类就可以了。这一节先到这里。。。。明天继续

0 0
原创粉丝点击