设计模式-结构型模式-访问者

行为模式涉及到算法和对象间职责的分配。
访问者模式:表示一个作用于某对象结构中的各元素的操作。它使你可以在不改变各元素的类的前提下定义作用于这些元素的新操作。

 

package behaviour.visitor;
public interface awtList
{
     public void add(String s);
     public void remove(String s);
     public String[] getSelectedItems();

}

package behaviour.visitor;
public 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);
   }
}

package behaviour.visitor;
public 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();
 }
}

package behaviour.visitor;
public 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);
   }
}

package behaviour.visitor;
public 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;
 }
}

package behaviour.visitor;
public abstract class Visitor
{
  public abstract void visit(Employee emp);
  public abstract void visit(Boss emp);
}

你可能感兴趣的:(设计模式,算法,String,Class,float,interface)