学用状态机模式,写的报销流程,请指教

刚学编程不久,请前辈们指点。
需求变化点:不同的金额,和不同的角色走不同的报销流程

using System;

static class Factory
{
    
public static Employee CreateEmployee(string name)
    {
        Employee employee 
= null;
        
switch (name)
        {
            
case "Sales":
                employee 
= new Sales();
                
break;
            
case "ProjectManager":
                employee 
= new ProjectManager();
                
break;
        }
        
if (employee == nullthrow new Exception("传入的名字非法!");
        
return employee;
    }
}

abstract class Employee
{
    
public int Money { getset; }
    
public State State { getset; }

    
public void Sign()
    {
        State.ChangeState(
this);
    }
}

class Sales : Employee
{
    
public Sales()
    {
        State 
= new ProjectManagerState();
    }
}

class ProjectManager : Employee
{
    
public ProjectManager()
    {
        State 
= new DepartmentManagerState();
    }
}

abstract class State
{
    
public abstract void ChangeState(Employee employee);

    
public virtual void ChangeState(Employee employee, string sign, State state)
    {
        Console.WriteLine(sign);
        employee.State 
= state;
        employee.Sign();
    }

    
public virtual void FinishState(Employee employee, string sign)
    {
        Console.WriteLine(sign);
        employee.State 
= new FinishedState();
        employee.Sign();
    }
}

class ProjectManagerState : State
{
    
public override void ChangeState(Employee employee)
    {
        
if (employee.Money < 200)
        {
            
string sign = string.Format("{0}元,项目经理签字报销", employee.Money);
            FinishState(employee, sign);
        }
        
else
        {
            
string sign = string.Format("{0}元,项目经理签字后,给部门经理", employee.Money);
            ChangeState(employee, sign, 
new DepartmentManagerState());
        }
    }
}

class DepartmentManagerState : State
{
    
public override void ChangeState(Employee employee)
    {
        
if (employee.Money < 1000)
        {
            
string sign = string.Format("{0}元,部门经理签字报销", employee.Money);
            FinishState(employee, sign);
        }
        
else
        {
            
string sign = string.Format("{0}元,部门经理签字后,给总经理", employee.Money);
            ChangeState(employee, sign, 
new GeneralManagerState());
        }
    }
}

class GeneralManagerState : State
{
    
public override void ChangeState(Employee employee)
    {
        
string sign = string.Format("{0}元,总经理签字报销", employee.Money);
        FinishState(employee, sign);
    }
}

class FinishedState : State
{
    
public override void ChangeState(Employee employee)
    {
        Console.WriteLine(
"签字完成,找财务报销{0}元", employee.Money);
    }
}

class Test
{
    
static void Main()
    {
        Employee employee 
= Factory.CreateEmployee("Sales");
        employee.Money 
= 1150;
        employee.Sign();

        employee 
= Factory.CreateEmployee("ProjectManager");
        employee.Money 
= 2150;
        employee.Sign();
    }
}

posted @ 2008-05-25 14:16  rapid  阅读(1626)  评论(2编辑  收藏  举报