» 您尚未 登录   注册 | 社区服务 | FTP中心 | 帮助 | 社区 | 无图版 | 测试百科  | 测试Blog 
软件测试基地论坛 -> 测试开发技术 -> Visitor模式应用
 XML   RSS 2.0   WAP 

--> 本页主题: Visitor模式应用 加为IE收藏 | 收藏主题 | 上一主题 | 下一主题
Fastpoint


该用户目前不在线
级别: 总版主
精华: 44
发帖: 5033
基地声望: 390 点
基地币: 1689 Bug
基地贡献: 0 点
好评度: 15 点
在线时间:818(小时)
注册时间:2005-10-08
最后登录:2008-07-22
查看作者资料 发送短消息 推荐此帖 引用回复这个帖子

Visitor模式应用

/*
The Design Patterns Java Companion


Copyright (C) 1998, by James W. Cooper

IBM Thomas J. Watson Research Center

*/

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Vector;

import javax.swing.AbstractListModel;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class VacationDisplay extends JFrame implements ActionListener {
  JawtList empList;

  JTextField total, btotal;

  JButton Vac;

  Employee[] employees;

  public VacationDisplay() {
    super("Vacation Display");
    addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });

    JPanel jp = new JPanel();
    getContentPane().add(jp);
    jp.setLayout(new GridLayout(12));
    empList = new JawtList(10);
    jp.add(empList);

    createEmployees();



可不可不要这么样徘徊在目光内
你会察觉到我根本寂寞难耐
即使千多百个深夜曾在梦境内
我有吻过你这毕竟并没存在

人声车声开始消和逝
无声挣扎有个情感奴隶
是我多么的想她
但我偏偏只得无尽叹谓

其实每次见你我也着迷
无奈你我各有角色范围
就算在寂寞梦内超出好友关系
唯在暗里爱你暗里着迷
无谓要你惹上各种问题
共我道别吧别让空虚使我越轨
[楼 主] | Posted: 2006-08-20 20:59 顶端
Fastpoint


该用户目前不在线
级别: 总版主
精华: 44
发帖: 5033
基地声望: 390 点
基地币: 1689 Bug
基地贡献: 0 点
好评度: 15 点
在线时间:818(小时)
注册时间:2005-10-08
最后登录:2008-07-22
查看作者资料 发送短消息 推荐此帖 引用回复这个帖子



Box box = Box.createVerticalBox();
    jp.add(box);
    total = new JTextField(5);
    total.setHorizontalAlignment(JTextField.CENTER);
    box.add(total);
    box.add(Box.createVerticalStrut(10));
    btotal = new JTextField(5);
    btotal.setHorizontalAlignment(JTextField.CENTER);
    box.add(btotal);
    box.add(Box.createVerticalStrut(10));
    Vac = new JButton("Vacations");
    box.add(Vac);
    Vac.addActionListener(this);
    setSize(300200);
    setVisible(true);
    total.setText("  ");
    total.setBackground(Color.white);

  }

  public void createEmployees() {
    employees = new Employee[7];
    int i = 0;
    employees[i++new Employee("Susan Bear"55000121);
    employees[i++new Employee("Adam Gehr"15000090);
    employees[i++new Employee("Fred Harris"50000152);
    employees[i++new Employee("David Oakley"57000122);
    employees[i++new Employee("Larry Thomas"100000206);
    Boss b = new Boss("Leslie Susi"175000164);
    b.setBonusDays(12);
    Boss b1 = new Boss("Laurence Byerly"35000176);
    b1.setBonusDays(17);
    employees[i++= b;
    employees[i++= b1;

    for (i = 0; i < employees.length; i++) {
      empList.add(employees[i].getName());
    }
  }

  public void actionPerformed(ActionEvent e) {
    VacationVisitor vac = new VacationVisitor();
    bVacationVisitor bvac = new bVacationVisitor();
    for (int i = 0; i < employees.length; i++) {
      employees[i].accept(vac);
      employees[i].accept(bvac);
    }
    total.setText(new Integer(vac.getTotalDays()).toString());
    btotal.setText(new Integer(bvac.getTotalDays()).toString());
  }

  public static void main(String argv[]) {
    new VacationDisplay();
  }
}

class Employee {
  int sickDays, vacDays;

  float Salary;

  String Name;

  public Employee(String name, float salary, int vacdays, int sickdays) {
    vacDays = vacdays;
    sickDays = sickdays;
    Salary = salary;
    Name = name;
  }

  public String getName() {
    return Name;
  }

  public int getSickdays() {
    return sickDays;
  }

  public int getVacDays() {
    return vacDays;
  }

  public float getSalary() {
    return Salary;
  }

  public void accept(Visitor v) {
    v.visit(this);
  }
}



可不可不要这么样徘徊在目光内
你会察觉到我根本寂寞难耐
即使千多百个深夜曾在梦境内
我有吻过你这毕竟并没存在

人声车声开始消和逝
无声挣扎有个情感奴隶
是我多么的想她
但我偏偏只得无尽叹谓

其实每次见你我也着迷
无奈你我各有角色范围
就算在寂寞梦内超出好友关系
唯在暗里爱你暗里着迷
无谓要你惹上各种问题
共我道别吧别让空虚使我越轨
[1 楼] | Posted: 2006-08-20 21:00 顶端
Fastpoint


该用户目前不在线
级别: 总版主
精华: 44
发帖: 5033
基地声望: 390 点
基地币: 1689 Bug
基地贡献: 0 点
好评度: 15 点
在线时间:818(小时)
注册时间:2005-10-08
最后登录:2008-07-22
查看作者资料 发送短消息 推荐此帖 引用回复这个帖子



abstract class Visitor {
  public abstract void visit(Employee emp);

  public abstract void visit(Boss emp);
}

class Boss extends Employee {
  private int bonusDays;

  public Boss(String name, float salary, int vacdays, int sickdays) {
    super(name, salary, vacdays, sickdays);
  }

  public void setBonusDays(int bonus) {
    bonusDays = bonus;
  }

  public int getBonusDays() {
    return bonusDays;
  }

  public void accept(Visitor v) {
    v.visit(this);
  }
}

class VacationVisitor extends Visitor {
  protected int total_days;

  public VacationVisitor() {
    total_days = 0;
  }

  public void visit(Employee emp) {
    total_days += emp.getVacDays();
  }

  public void visit(Boss boss) {
    total_days += boss.getVacDays();
  }

  public int getTotalDays() {
    return total_days;
  }
}

class bVacationVisitor extends Visitor {
  int total_days;

  public bVacationVisitor() {
    total_days = 0;
  }

  public int getTotalDays() {
    return total_days;
  }

  public void visit(Boss boss) {
    total_days += boss.getVacDays();
    total_days += boss.getBonusDays();
  }

  public void visit(Employee emp) {
    total_days += emp.getVacDays();
  }
}



可不可不要这么样徘徊在目光内
你会察觉到我根本寂寞难耐
即使千多百个深夜曾在梦境内
我有吻过你这毕竟并没存在

人声车声开始消和逝
无声挣扎有个情感奴隶
是我多么的想她
但我偏偏只得无尽叹谓

其实每次见你我也着迷
无奈你我各有角色范围
就算在寂寞梦内超出好友关系
唯在暗里爱你暗里着迷
无谓要你惹上各种问题
共我道别吧别让空虚使我越轨
[2 楼] | Posted: 2006-08-20 21:01 顶端
Fastpoint


该用户目前不在线
级别: 总版主
精华: 44
发帖: 5033
基地声望: 390 点
基地币: 1689 Bug
基地贡献: 0 点
好评度: 15 点
在线时间:818(小时)
注册时间:2005-10-08
最后登录:2008-07-22
查看作者资料 发送短消息 推荐此帖 引用回复这个帖子



//this is a simple adapter class to
//convert List awt methods to Swing methods


class JawtList extends JScrollPane implements ListSelectionListener, awtList {
  private JList listWindow;

  private JListData listContents;

  public JawtList(int rows) {
    listContents = new JListData();
    listWindow = new JList(listContents);
    listWindow.setPrototypeCellValue("Abcdefg Hijkmnop");
    getViewport().add(listWindow);

  }

  public void add(String s) {
    listContents.addElement(s);
  }

  public void remove(String s) {
    listContents.removeElement(s);
  }

  public void clear() {
    listContents.clear();
  }

  public String[] getSelectedItems() {
    Object[] obj = listWindow.getSelectedValues();
    String[] s = new String[obj.length];
    for (int i = 0; i < obj.length; i++)
      s[i= obj[i].toString();
    return s;
  }

  public void valueChanged(ListSelectionEvent e) {
  }

}

class JListData extends AbstractListModel {
  private Vector data;

  public JListData() {
    data = new Vector();
  }

  public int getSize() {
    return data.size();
  }

  public Object getElementAt(int index) {
    return data.elementAt(index);
  }

  public void addElement(String s) {
    data.addElement(s);
    fireIntervalAdded(this, data.size() 1, data.size());
  }

  public void removeElement(String s) {
    data.removeElement(s);
    fireIntervalRemoved(this, 0, data.size());
  }

  public void clear() {
    int size = data.size();
    data = new Vector();
    fireIntervalRemoved(this, 0, size);
  }
}

interface awtList {
  public void add(String s);

  public void remove(String s);

  public String[] getSelectedItems();

}


可不可不要这么样徘徊在目光内
你会察觉到我根本寂寞难耐
即使千多百个深夜曾在梦境内
我有吻过你这毕竟并没存在

人声车声开始消和逝
无声挣扎有个情感奴隶
是我多么的想她
但我偏偏只得无尽叹谓

其实每次见你我也着迷
无奈你我各有角色范围
就算在寂寞梦内超出好友关系
唯在暗里爱你暗里着迷
无谓要你惹上各种问题
共我道别吧别让空虚使我越轨
[3 楼] | Posted: 2006-08-20 21:02 顶端

软件测试基地论坛 -> 测试开发技术




软件测试基地上海测仕信息技术有限公司旗下网站
Copyright © 2005-2007 Cntesting.com, All Rights Reserved
沪ICP备06057721号

Powered by PHPWind Code © 2003-06 PHPWind
Total 0.129658(s) query 5, Time now is:12-05 09:57, Gzip disabled
You can contact us


每日一句:Loading...